码迷,mamicode.com
首页 > 其他好文 > 详细

2019年7月31日(基本功练习1)

时间:2019-08-07 09:16:32      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:合并   ring   push   math   define   sub   mod   ons   while   

状态不好!!……

prob1&prob2,傻逼题,都是基础操作的练习,练手的,一个练循环,一个练\(sort\)和循环,垃圾,上代码:
\(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;
}

\(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

傻逼并查集,结果还调了老子半天,\(fuck\),不说了,上代码:

#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

傻逼二分,二分最长长度,判断可否,特判一即可,注意当最长长度为\(k\)时在一段长度为\(len\)\(0\)\(1\)中的最优划分策略为修改\(len/(k+1)\)天:

#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

30分部分分为暴力,在此不介绍。

正解数学推式子,将给定的式子拆开:设目前扫到的\(B\)序列的数为\(Y_p\),将\(A\)\(C\)序列\(sort\)后,小于等于\(Y_p\)的为\(X_{1-n}\)\(Z_{1-m}\),则我们要求的为\(\sum_{i=1}^{n}\sum_{j=1}^{m}(X_i+Y_p)* (Z_j+Y_p)\).考虑化简(因式分解之合并同类项),先提公因式\((X_i+Y_p)\),则式子化为\(\sum_{i=1}^{n}((X_i+Y_p)* (\sum_{j=1}^{m}Z_j+m* Y_p))\),同理,最后化简为\((\sum_{i=1}^{n}X_i+n* Y_p)* (\sum_{j=1}^{m}Z_j+m* Y_p)\).于是,确定方案为\(sort\),再扫,逐个\(Y\)进行求解。至此,游戏结束:

#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

树型\(DP\),数组\(f[i]\)\(w[i]\)分别记录\(i\)子树中的最大收益与点权和

转移方程:\(f[i]=max(\sum f[son[i][j]],-w[i]-x)\)

目标:\(w[1]+f[1]\)

两遍\(dfs\),游戏结束:

#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\):以后考试要调整好心态,而且最好少\(dabai\)

2019年7月31日(基本功练习1)

标签:合并   ring   push   math   define   sub   mod   ons   while   

原文地址:https://www.cnblogs.com/ALANALLEN21LOVE28/p/11313008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!