Summer Training Session 1 4

T1 Waka eats melons (simulation)Insert picture description here

Insert picture description here
Insert picture description here
1
5 5
AWaDa!

If it’s over in one shot, because it is a game of two melons, the first number is even, then -1, and the second is odd –1

#include<bits/stdc++.h>
using namespace std;
int a,b;
int t;
int main()
{
    
    
    scanf("%d",&t);
    while(t--)
    {
    
    
        scanf("%d%d",&a,&b);
        if(a%2==0)a--;if(b%2==1)b--; //比自己最多吃的那局的瓜多出一,没有用
        if(a>b)puts("AWaDa!"); 
        else puts("AKTang!");
    }
    return 0;
 } 


T2 Grilled Lezi Fried Bridge (greedy)

Insert picture description here
Insert picture description here
After reading the question, you can find that the c in the question is not negligible. Then simplify the topic to see how many bridges do not overlap. Sort the bridge tails from small to large, if the next bridge head is larger than the bridge tail, then there is no overlap, bomb++

#include<bits/stdc++.h>
using namespace std;
struct node
{
    
    
    int l;
    int r;
}a[150000];
bool cmp(node x,node y)//从小到大
{
    
    
    if(x.r==y.r)return x.l<y.r;
    return x.r<y.r;
}
int main()
{
    
    
    int n;cin>>n;
    int q,ans=0;
    for(int i=1;i<=n;i++) cin>>a[i].l>>a[i].r>>q;
    sort(a+1,a+1+n,cmp);
    q=0;
    for(int i=1;i<=n;i++)
    if(a[i].l>q)q=a[i].r,ans++;//没有重叠
    cout<<ans;
    return 0;
}


T3 different aesthetic (dp)

Title description
Because xz is so handsome, handsome, charming, suave, everyone loves, flowers bloom, cars see cars. There is a group of MM queuing to watch xz. Each MM has its own unique style. Because xz has a tolerant heart, he likes any style of MM... However, xz has a special requirement. He doesn't want to always see the same style MM, what is more special is that if two MM styles are exactly the same, xz will not have any opinions. Now, xz hopes to remove some MMs from his MM, so that the difference (absolute value) of the style values ​​of two adjacent MMs is not 1. Naturally, xz hopes to remove as little MM as possible.

Input
an integer N in the first line; lines
2~N+1, an integer Ci in each line, representing the style value of the i-th MM

Output
Output a number, indicating the minimum number of MMs to be removed.

The setup state dp[i] indicates that the first mm can leave at most several mm. The second recycle starts from the first mm and goes to i-1 mm. If the abs value of two mm is not one, the i-th mm can be repeated by adding 1 to the j-th mm answer.

#include<bits/stdc++.h>
using namespace std;
int n,a[1500],dp[1500],b[1500];
int main()
{
    
    
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i],dp[i]=1;
     
 
    for(int i=1;i<=n;i++)
    for(int j=1;j<i;j++)
    if(abs(a[i]-a[j])!=1)
    dp[i]=max(dp[i],dp[j]+1);//状态转移
     
    cout<<n-dp[n];
    return 0;
}

T4 Waka Travel (DFS)

Insert picture description here

Insert picture description here

Two times of dfs, the first time from the beginning to find the largest known point of the road connected with 1 and then from the found point, and then run to the side, even once the point that has not been connected

#include<bits/stdc++.h>
#define maxn 150000 
using namespace std;
int a[maxn],head[maxn],tot,n,m,fa[maxn],ans[maxn];
struct node
{
    
    
    int u;
    int v;
}edge[maxn];
void add(int x,int y)
{
    
    
    ++tot;
    edge[tot].u=head[x];
    edge[tot].v=y;
    head[x]=tot;
}
void dfs(int x)
{
    
    
    fa[x]=x;
    for(int i=head[x];i;i=edge[i].u)
    {
    
    
        if(fa[edge[i].v]==0)//有没有来过
        {
    
    
            dfs(edge[i].v);
            if(ans[edge[i].v]>ans[x]){
    
    
                ans[x]=ans[edge[i].v];//更新最大ans
                fa[x]=fa[edge[i].v];
            }
        }
    }
    ans[x]+=a[x];
}
int read()
{
    
    
    int x=0;char c=getchar();
    while(c>'9'||c<'0')c=getchar();
    while(c<='9'&&c>='0')
    {
    
    
        x=x*10+c-'0';
        c=getchar();
    }
    return x;
}
int main()
{
    
    
    n=read();m=read();
    for(int i=1;i<=n;i++)a[i]=read();
    for(int i=1;i<=m;i++)
    {
    
    
        int x,y;x=read();y=read();
        add(x,y);
        add(y,x);
    }
     
    dfs(1);
    int q=fa[1];找到那个点,从那里开始
    memset(fa,0,sizeof(fa));
    memset(ans,0,sizeof(ans));
     
    dfs(q);
    cout<<ans[q];
    return 0;
}

Guess you like

Origin blog.csdn.net/yhhy666/article/details/108236863