• Codeforces Round #453 (Div. 2)


    Codeforces Round #453 (Div. 2)

    题目链接:http://codeforces.com/contest/902.

    A题题意:每次给X,Y表示可以到达X-Y上的任意点。询问最后是否可以到达终点。

    A题题解:将X-Y写成一个前闭后开的区间,将区间的点标记,最后遍历从0-N的所有点,都标记即可到达终点。

    AC代码:

     1 #include<iostream>
     2 #include<algorithm>
     3 using namespace std;
     4 const int maxn =107;
     5 int vis[maxn];
     6 int n,m;
     7 int main()
     8 {
     9     cin>>n>>m;
    10     for(int i=1;i<=n;i++)
    11     {
    12         int x,y;
    13         cin>>x>>y;
    14         for(int i=x;i<y;i++)
    15         {
    16             vis[i]++;
    17         }
    18         if(y==m)vis[y]++;
    19     }
    20     int ok = 0;
    21     for(int i=0;i<=m;i++)
    22     {
    23         if(vis[i]==0)ok = 1;
    24     }
    25     if(ok)cout<<"NO"<<endl;
    26     else cout<<"YES"<<endl;
    27 }

    B题题意:一棵树有N个节点,下一行是每个结点的父结点。第三行是每个结点应该的涂色,父结点涂色后他的所有子结点都是父结点的颜色,求符合要求的最小次数。

    B题题解:贪心的思想,必须从父结点开始涂,然后往下面的子结点走,不相同涂色次数就+1。

    AC代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 const int maxn = 10007;
     5 int fa[maxn],color[maxn],n,x,y;
     6 int main()
     7 {
     8     scanf("%d",&n);
     9     for(int i=2;i<=n;i++)
    10     {
    11         scanf("%d",&fa[i]);
    12     }
    13     fa[1]=1;
    14     for(int i=1;i<=n;i++)
    15     {
    16         scanf("%d",&color[i]);
    17     }
    18     int sum = 1;
    19     for(int i=1;i<=n;i++)
    20     {
    21         if(color[i]!=color[fa[i]])sum++;
    22     }
    23     cout<<sum<<endl;
    24 }

    C题题意:给定一棵树的层数和每层的结点数,问是否存在不同的两棵树,存在输出ambiguous和任意两个符合条件的树。

    C题题解:容易看出只有有两个连续的层数的结点大于1才能存在不同的树,这个时候输出连续的两层中的第二层父结点和父结点-1即可。

    AC代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 const int maxn = 100007;
     5 int a[maxn];
     6 int n;
     7 int main()
     8 {
     9     scanf("%d",&n);
    10     int sum = 0;
    11     for(int i=0;i<=n;i++)
    12     {
    13         cin>>a[i];
    14         sum+=a[i];
    15     }
    16     int k = 0;
    17     int ok = 0;
    18     int num = 0;
    19     for(int i=0;i<=n;i++)
    20     {
    21         if(a[i]>1)ok++;
    22         else ok = 0;
    23         if(ok==2)num = i;
    24     }
    25     if(!num)cout<<"perfect"<<endl;
    26     else
    27     {
    28         cout<<"ambiguous"<<endl;
    29         for(int i=0;i<=n;i++)
    30         {
    31                 for(int j=1;j<=a[i];j++)
    32                 {
    33                     cout<<k<<" ";
    34                 }
    35             k+=a[i];
    36         }
    37         cout<<endl;
    38         k = 0;
    39         for(int i=0;i<=n;i++)
    40         {
    41             if(i==num)
    42             {
    43                 for(int j=1;j<a[i];j++)cout<<k-1<<" ";
    44                 cout<<k<<" ";
    45             }
    46             else
    47             {
    48                 for(int j=1;j<=a[i];j++)
    49                 {
    50                     cout<<k<<" ";
    51                 }
    52             }
    53             k+=a[i];
    54         }
    55         cout<<endl;
    56     }
    57 }

     

     

  • 相关阅读:
    TP中模型实例化
    PHP中面向对象编程思想的3个特征
    static关键字的新用法
    PHP中与类有关的运算符
    PHP中与类和对象有关的几个系统函数
    PHP中与类有关的几个魔术常量
    PHP与类有关的几个魔术方法
    PHP中类型约束
    PHP对象类型转换
    ubuntu MySQL拒绝远程连接(10061)
  • 原文地址:https://www.cnblogs.com/sortmin/p/8088822.html
Copyright © 2020-2023  润新知