July 31, 2019 (basic skills Exercise 1)

bad mood! ! ......

prob1 & prob2, sucker questions, exercises are basic operations, practiced hand, a training cycle, a practice \ (sort \) and recycling, trash, on the code:
\ (T1 \) :

#include<iostream>
#include<cstdio>
using namespace std;
#define in read()
#define fur(i,a,b) for(int i=a;i<=b;i++)
#define int long long
inline int read()
{
    int x=0,f=1;char ch=getchar();
    for(;!isalnum(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
int ans[2];
signed main()
{
    int t=in;
    while(t--)
    {
        int n=in;
        ans[0]=ans[1]=0;
        fur(i,1,n) ans[i%2]+=in;
        fur(i,1,n) ans[(i%2)^1]+=in;
        printf("%lld\n",ans[0]>ans[1]?ans[1]:ans[0]);
    }
    return 0;
}

\ ( The T2 \) :

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define in read()
#define fur(i,a,b) for(int i=a;i<=b;i++)
#define jiba signed
const int xx=1e5+10;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    for(;!isalnum(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
int a[xx];
jiba main()
{
    int t=in;
    while(t--)
    {
        int n=in;
        fur(i,1,n) a[i]=in;
        sort(a+1,a+n+1);
        if(a[1]<a[2]-1)
        {
            printf("%d\n",a[1]);
            continue;
        }
        if(a[n]>a[n-1]+1)
        {
            printf("%d\n",a[n]);
            continue;
        }
        fur(i,1,n)
        {
            if(a[i]==a[i+1])
            {
                printf("%d\n",a[i]);
                break;
            }
        }
    }
    return 0;
}

prob5:Dish Owner

Sucker disjoint-set, the results also tuneFatherHalf-day, \ (Fuck \) , do not say, on the code:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define in read()
#define fur(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
inline int read()
{
    int x=0,f=1;char ch=getchar();
    for(;!isalnum(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
const int xx=1e4+10;
int fa[xx],s[xx];
inline int find(int i){return i==fa[i]?i:(fa[i]=find(fa[i]));}
int main()
{
    int t=in;
    while(t--)
    {
        int n=in;
        fur(i,1,n) fa[i]=i,s[i]=in;
        int m=in;
        while(m--)
        {
            int op=in;
            if(op==0)
            {
                int x=in,y=in;
                int u=find(x),v=find(y);
                if(u==v)
                {
                    puts("Invalid query!");
                    continue;
                }
                if(s[u]>s[v]) fa[v]=u;
                else if(s[u]<s[v]) fa[u]=v;
            }
            else
            {
                int x=in;
                printf("%d\n",find(x));
            }
        }
    }
    return 0;
}

prob3:Cooking Schedule

Sucker-half, Binary maximum length, determines whether, to a Japanese sentence, note that when the maximum length \ (K \) in a length of time \ (len \) a \ (0 \) or \ (1 \) of optimal partitioning strategy to modify the \ (len / (k + 1 ) \) days:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define in read()
#define fur(i,a,b) for(int i=a;i<=b;++i)
#define jiba signed
inline int read()
{
    int x=0,f=1;char ch=getchar();
    for(;!isalnum(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
const int xx=1e6+10;
int len[xx],all,need,ans1,ans2;
char op[xx];
inline bool check(int k)
{
    int now=0;
    fur(i,1,all)
    {
        if(len[i]>k)
        {
            int u=len[i];
            now+=u/(k+1);
            if(now>need) return false;
        }
    }
    return true;
}
jiba main()
{
    int t=in;
    while(t--)
    {
        fur(i,1,all) len[i]=0;
        int n=in,hd=2,tl=0;
        all=0;need=in;
        ans1=0;ans2=0;
        scanf("%s",op);
        len[++all]++;
        op[0]-='0';
        if(op[0]!=0) ans1++;
        else ans2++;
        fur(i,1,n-1)
        {
            op[i]=op[i]-'0';
            if(op[i-1]!=op[i]) ++all;
            if(op[i]!=i%2) ans1++;
            else ans2++;
            ++len[all];
            tl=max(tl,len[all]);
        }
        if(ans1<=need||ans2<=need)
        {
            puts("1");
            continue;
        }
        int ans=tl;
        while(hd<=tl)
        {
            int mid=(hd+tl)>>1;
            if(check(mid)) tl=mid-1,ans=mid;
            else hd=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

prob6:Triplets

Part is divided into 30 sub-violence, not in this introduction.

Positive mathematical solution to push equation, equation given open: to set the current sweep \ (B \) sequence number is \ (Y_p \) , the \ (A \) and \ (C \) sequence \ ( Sort \) , the less \ (Y_p \) of \ (X_ {1-n} \) and \ (Z_ {. 1-m} \) , then we require as \ (\ sum_ {i = 1 } n-} {^ \ sum_ {J} = ^ {m}. 1 (Y_p X_i +) * (+ Y_p Z_j) \) . consider simplification (merging factorization of similar items), to mention common factor \ (( + Y_p x_i) \) , into the expression \ (\ sum_ {i = 1 } ^ {n} ((x_i + Y_p) * (\ sum_ {j = 1} ^ {m} Z_j + m * Y_p) ) \) . Similarly, finally reduces to \ ((\ sum_ {i = 1} ^ {n} X_i + n * Y_p) * (\ sum_ {j = 1} ^ {m} Z_j + m * Y_p) \) . Thus, the program is determined as \ (Sort \) , then sweep, one by one \ (the Y \) is solved. At this point, the game is over:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define in read()
#define fur(i,a,b) for(int i=a;i<=b;++i)
#define int long long
#define jiba signed
inline int read()
{
    int x=0,f=1;char ch=getchar();
    for(;!isalnum(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
const int xx=1e5+10;
const int mod=1e9+7;
int a[xx],c[xx],b[xx];
jiba main()
{
    int t=in+1;
    while(--t)
    {
        int n=in,m=in,r=in;
        fur(i,1,n) a[i]=in;sort(a+1,a+n+1);
        fur(j,1,m) b[j]=in;sort(b+1,b+m+1);
        fur(k,1,r) c[k]=in;sort(c+1,c+r+1);
        int tmp1=0,tmp2=0,ans=0;
        for(int i=1,j=1,k=1;j<=m;++j)
        {
            while(a[i]<=b[j]&&i<=n) tmp1=(tmp1+a[i])%mod,++i;
            while(c[k]<=b[j]&&k<=r) tmp2=(tmp2+c[k])%mod,++k;
            ans=(ans+(tmp1+(i-1)*b[j]%mod)%mod*(tmp2+(k-1)*b[j]%mod)%mod)%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

prob4:CodeChef-SUBREM

Tree \ (the DP \) , the array \ (f [i] \) and \ (w [i] \) were recorded \ (I \) the maximum point of return and the right subtree, and

Transfer equation: \ (F [I] = max (\ F SUM [Son [I] [J]], - W [I] the -X-) \)

Target: \ (W [. 1] + F [. 1] \)

Twice \ (the DFS \) , the game is over:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
#define in read()
#define fur(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define jiba signed
inline int read()
{
    int x=0,f=1;char ch=getchar();
    for(;!isalnum(ch);ch=getchar()) if(ch=='-') f=-1;
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
const int xx=1e5+10;
int f[xx],w[xx],x,fa[xx];
bool vis[xx];
vector<int>e[xx];
inline void dfs1(int g)
{
    fur(i,0,(int)e[g].size()-1)
    {
        if(vis[e[g][i]]) continue;      
        vis[e[g][i]]=true;
        fa[e[g][i]]=g;
        dfs1(e[g][i]);
        w[g]+=w[e[g][i]];
        
    }
}
inline void dfs2(int g)
{
    f[g]=0;
    fur(i,0,(int)e[g].size()-1)
    {
        if(e[g][i]==fa[g]) continue;
        dfs2(e[g][i]);
        f[g]+=f[e[g][i]];
    }
    f[g]=max(f[g],-w[g]-x);
}
inline void init(int n)
{
    fur(i,1,n)
    {
        vis[i]=false;
        e[i].clear();
        fa[i]=0;
    }
}
jiba main()
{
    int t=in+1;
    while(--t)
    {
        int n=in;x=in;
        fur(i,1,n) w[i]=in;
        fur(i,1,n-1)
        {
            int x=in,y=in;
            e[x].push_back(y);
            e[y].push_back(x);
        }
        vis[1]=true;
        dfs1(1);
        dfs2(1);
        printf("%lld\n",f[1]+w[1]);
        init(n);
    }
    return 0;
}

\ (PS \) : After the exam should change their attitude, and preferably less \ (dabai \)

Guess you like

Origin www.cnblogs.com/ALANALLEN21LOVE28/p/11313008.html