Codeforces Round #660 (Div. 2)

A - Captain Flint and Crew Recruitment

刚开始还想筛法求质数,最后发现是个脑筋急转弯

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define debug(x) cout<<#x<<": "<<x<<" "
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    IO;
    int T;
    cin>>T;
    while(T--)
    {
        int x;
        cin>>x;
        if(x>30) 
        {
            cout<<"YES"<<endl;
            if(x==36||x==40||x==44) cout<<6<<" "<<10<<" "<<15<<" "<<x-31<<endl;
            else cout<<6<<" "<<10<<" "<<14<<" "<<x-30<<endl;
        }
        else cout<<"NO"<<endl;
    }
    return 0;
}

B - Captain Flint and a Long Voyage

9 9 9的二进制是 1001 1001 1001 8 8 8的二进制是 1000 1000 1000,想要删去后剩下数最大只能让二进制位数最长,因此只能用9和8拼凑,分析可知到剩下数最大时原数最小是 ⌈ n 4 ⌉ \lceil \frac{n}{4} \rceil 4n 8 8 8 n − ⌈ n 4 ⌉ n-\lceil \frac{n}{4} \rceil n4n个9组成。

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define debug(x) cout<<#x<<": "<<x<<" "
#include<iostream>
#include<algorithm>
int main()
{
    IO;
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        int a=(n+3)/4,b=n-a;
        while(b--) cout<<9;
        while(a--) cout<<8;
        cout<<endl;
    }
    return 0;
}

刚开始做完不敢提交,怕掉分,结果做了两个感觉不会掉分了就全交了(已经过了50min了)tcl,最终做了两个还是掉分了,曲折是最美的???

C - Uncle Bogdan and Country Happiness

Happiness detectors are installed in each city to monitor the happiness of each person who visits the city.
这题以为不定时测幸福值,结果上面那句话是每个人只要经过该城市就会当作幸福值的测试数据。我哭了!!!
翻译一下就是每个点测的幸福值总人数是子树的总人数
那么分析可知good[u]+bad[u]=sz[u]good[u]-bad[u]=happy[u],即good[u]=happy[u]+sz[u]>>1bad[u]=sz[u]-happy[u]
不矛盾由以下条件

  • good[u]人数不能小于0并且不是小数即①good[u]>=0happy[u]+sz[u]必须是偶数
  • bad[u]人数不能小于0
  • 由于心情只能变差不能编号,那么子树中心情好的人数一定小于子树祖宗节点心情好的人数即sgood<=good[u]
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define debug(x) cout<<#x<<": "<<x<<" "
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=200010;
int h[N],e[N],ne[N],idx;
int happy[N],cnt[N];
ll sz[N],good[N],bad[N];
int n,m;
bool flag;
void add(int a,int b)
{
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
void dfs(int u,int fa)
{
    int sgood=0;
    sz[u]=cnt[u];
    for(int i=h[u];i!=-1;i=ne[i])
    {
        int j=e[i];
        if(j==fa) continue;
        dfs(j,u);
        sz[u]+=sz[j];
        sgood+=good[j];
    }
    good[u]=sz[u]+happy[u];
    if(good[u]&1||good[u]<0) flag=0;
    good[u]>>=1;
    if(good[u]<sgood) flag=0;
    bad[u]=sz[u]-happy[u];
    if(bad[u]<0) flag=0;
}
int main()
{
    IO;
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++) h[i]=-1;
        idx=0;
        for(int i=1;i<=n;i++) cin>>cnt[i];
        for(int i=1;i<=n;i++) cin>>happy[i];
        for(int i=1;i<n;i++)
        {
            int a,b;
            cin>>a>>b;
            add(a,b),add(b,a);
        }
        flag=1;
        dfs(1,-1);
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

这题由于没看清题,给自己造成了非常大的难度,希望以后认真看题,挖掘更多条件。

D -Captain Flint and Treasure

题中暗示说有向无环图,那么基本就是拓扑排序了,如果该位置的值大于0那么肯定先加上他,它会使后面加的值变大。输出就是如果值大于0说明对后面有影响先输出正拓扑序输出。如果值小于0那么它不能先输出否则会对后面有影响因此逆拓扑序输出。

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define debug(x) cout<<#x<<": "<<x<<" "
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=200010;
int h[N],e[N],ne[N],idx;
int din[N],q[N];
ll a[N];
int n;
void add(int a,int b)
{
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
void topsort()
{
    int tt=-1,hh=0;
    for(int i=1;i<=n;i++)
        if(din[i]==0) q[++tt]=i;
    while(tt>=hh)
    {
        int u=q[hh++];
        for(int i=h[u];i!=-1;i=ne[i])
        {
            int j=e[i];
            if(a[u]>0) a[j]+=a[u];
            if(--din[j]==0) q[++tt]=j;
        }
    }
}
int main()
{
    IO;
    memset(h,-1,sizeof h);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) 
    {
        int b;
        cin>>b;
        if(b!=-1)
        {
            add(i,b);
            din[b]++;
        }
    }
    topsort();
    ll res=0;
    for(int i=1;i<=n;i++) res+=a[i];
    cout<<res<<endl;
    for(int i=0;i<n;i++)
        if(a[q[i]]>0) cout<<q[i]<<" ";
    for(int i=n-1;i>=0;i--) 
        if(a[q[i]]<=0) cout<<q[i]<<" ";
    cout<<endl;
    return 0;
}

要加油哦~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值