zoukankan      html  css  js  c++  java
  • codeforces411div.2

    每日CF:

    411div2

    Solved   A CodeForces 805A Fake NP
    Solved   B CodeForces 805B 3-palindrome
    Solved   C CodeForces 805C Find Amir
    Solved   D CodeForces 805D Minimum number of steps

    Attempted

      E CodeForces 805E Ice cream coloring
        F CodeForces 805F Expected diameter of a tree

    点题号阅读题面

    ---------

    A

    题意:

    给a,b,求[a,b]中所有整数的所有除数中,最多的一个

    分析:

    水题,直接看代码

     1 /**********************
     2 *@Name:
     3 *
     4 *@Author: Nervending
     5 *@Describtion:
     6 *@DateTime: 
     7 ***********************/
     8 #include <bits/stdc++.h>
     9 using namespace std;
    10 const int maxn=1e6+10;
    11 const int INF=0x3f3f3f3f;
    12 int n,m,k;
    13 int main(){
    14 //#define test
    15 #ifdef test
    16     freopen("in.txt","r",stdin);
    17     freopen("out.txt","w",stdout);
    18 #endif
    19     
    20     cin>>n>>m;
    21     if(m==n) cout<<m;
    22     else cout<<2;
    23     
    24     
    25 #ifdef test
    26     fclose(stdin);
    27   fclose(stdout);
    28     system("out.txt");
    29 #endif
    30     return 0;
    31 }
    View Code

    ---------

    B

    题意:

    求一个长度为n的字符串,由a,b,c三个字符组成

    要求不能出现长度为3的回文串,且c的出现次数最低

    分析:

    是不出现长度''恰好''好为3的回文串

    经过简单的思考和实验,

    显然aabb的无限重复,只会出现长度为偶数的回文串,符合题意

     1 /**********************
     2 *@Name:
     3 *
     4 *@Author: Nervending
     5 *@Describtion:20min
     6 *@DateTime:2018-02-02 16:16:38
     7 ***********************/
     8 #include <bits/stdc++.h>
     9 using namespace std;
    10 const int maxn=1e6+10;
    11 const int INF=0x3f3f3f3f;
    12 int n,m,k;
    13 
    14 int main(){
    15 //#define test
    16 #ifdef test
    17     freopen("in.txt","r",stdin);
    18     freopen("out.txt","w",stdout);
    19 #endif
    20     cin>>n;
    21     for(int i=1;i<=n;i++){
    22         if(i%4==1||i%4==2)printf("a");
    23         else printf("b");
    24     }
    25 #ifdef test
    26     fclose(stdin);
    27   fclose(stdout);
    28     system("out.txt");
    29 #endif
    30     return 0;
    31 }
    View Code

    -----

    C

    题意:

    1-n一共n个节点,两个节点之间的距离等于两个节点编号之和对n+1取模,每次访问完两个节点,两个节点的距离花费为0

    求遍历所有点的最小花费

    分析:

    显然,i+j==n+1时最优,次优为i+j==n+2

    具体看代码

     1 /**********************
     2 *@Name:
     3 *
     4 *@Author: Nervending
     5 *@Describtion:
     6 *@DateTime:2018-02-02 16:16:38
     7 ***********************/
     8 #include <bits/stdc++.h>
     9 using namespace std;
    10 const int maxn=1e6+10;
    11 const int INF=0x3f3f3f3f;
    12 int n,m,k;
    13 
    14 int main(){
    15 //#define test
    16 #ifdef test
    17     freopen("in.txt","r",stdin);
    18     freopen("out.txt","w",stdout);
    19 #endif
    20     cin>>n;
    21     if(!(n&1)) cout<<n/2-1;
    22     else cout<<n/2;
    23 #ifdef test
    24     fclose(stdin);
    25   fclose(stdout);
    26     system("out.txt");
    27 #endif
    28     return 0;
    29 }
    View Code

    -----

    D

    题意:

    给一个由a,b组成的字符串,进行如下操作:

      把所有"ab"子串,换为"bba"

    求最少的操作次数,答案对1e9+7取余

    分析:

    递推题

    如果是简单的"ab"换为"ba",很明显答案就是整个字符串进行冒泡排序的次数

    但这里换成的"bba",显然,本质上依然是冒泡排序

    但是每次交换会多出来一个b,也就是简单的递推

    预打表操作n次后的冒泡次数

    再预处理每个位置之前a的个数

    求和即可

     1 /**********************
     2 *@Name:
     3 *
     4 *@Author: Nervending
     5 *@Describtion:
     6 *@DateTime:2018-02-02 16:16:38
     7 ***********************/
     8 #include <bits/stdc++.h>
     9 using namespace std;
    10 const int maxn=1e6+10;
    11 const int mod=1e9+7;
    12 const int INF=0x3f3f3f3f;
    13 int n,m,k;
    14 char s[maxn];
    15 int numa[maxn];
    16 int ans[maxn];
    17 int main(){
    18 //#define test
    19 #ifdef test
    20     freopen("in.txt","r",stdin);
    21     freopen("out.txt","w",stdout);
    22 #endif
    23     ans[1]=1;
    24     for(int i=2;i<=maxn;i++){
    25         ans[i]=(((ans[i-1]<<1)%mod)+1)%mod;
    26     }
    27     scanf("%s",s+1);
    28     n=strlen(s+1);
    29     for(int i=1;i<=n;i++){
    30         numa[i]=numa[i-1];
    31         if(s[i]=='a')numa[i]++;
    32     }
    33     long long time=0;
    34     for(int i=1;i<=n;i++){
    35         if(s[i]=='b')time=(time+ans[numa[i]])%mod;
    36     }
    37     cout<<time<<endl;
    38 #ifdef test
    39     fclose(stdin);
    40   fclose(stdout);
    41     system("out.txt");
    42 #endif
    43     return 0;
    44 }
    View Code

    -----

    E

    没写完E...题意比较麻烦,读错了

    题意:

    给一个树,每个节点包含一个数字集合,其中有相同元素的节点一定组成一个连通子图

    然后建立一个新图,把树上所有有相同元素的连边,组成一个完全子图

    接下来进行图上的染色,最终保证所有相邻点没有一样的颜色

    求所有颜色和一种染色方式

    分析:

    (出来看了题解,写出来的)

    图论思维题,树上的dfs

    所有相邻点一定构成一个连通子图,就意味着所有一样颜色的点一定在同样一个子树上

    然后新图中连边,显然所有有一样元素的节点一定是相邻的

    具体过程采用dfs,由于原图中颜色一样就在一个子树上,所以直接dfs不会出现问题

    注意,可能会出现节点是空集合,注意避免

      1 /**********************
      2 *@Name:
      3 *
      4 *@Author: Nervending
      5 *@Describtion:
      6 *@DateTime: 2018-02-02 17:15:12
      7 ***********************/
      8 #include <bits/stdc++.h>
      9 using namespace std;
     10 const int maxn=1e6+10;
     11 const int maxm=1e6+10;
     12 const int mod=1e9+7;
     13 const int INF=0x3f3f3f3f;
     14 typedef set<int>::iterator sit;
     15 int n,m,k;
     16 set<int>s[maxn];
     17 vector<int>g[maxn];
     18 int ans[maxn];
     19 int head[maxn],nume;
     20 int cnt;
     21 struct node{
     22     int to,next;
     23 }e[maxm];
     24 void add(int a,int b){
     25     e[nume]=(node){b,head[a]};
     26     head[a]=nume++;
     27 }
     28 void dfs(int now,int pre){
     29     vector<int>vis;
     30     sit end=s[now].end();
     31     if(pre==-1){
     32         for(sit i=s[now].begin();i!=end;i++){
     33             ans[(*i)]=++cnt;
     34         }
     35     }else {
     36         for(sit i=s[now].begin();i!=end;i++){
     37             if(ans[(*i)]){
     38                 vis.push_back(ans[(*i)]);
     39             }
     40         }
     41         sort(vis.begin(),vis.end());
     42         int num=vis.size();
     43         int j=0,son=1;
     44         for(sit i=s[now].begin();i!=end;i++){
     45             if(!ans[(*i)]){
     46                 while(j<num){
     47                     if(son==vis[j]){
     48                         j++,son++;
     49                     }
     50                     else break;
     51                 }
     52                 ans[(*i)]=son++;
     53             }
     54             cnt=max(cnt,son-1);
     55         }
     56     }
     57     for(int i=head[now];~i;i=e[i].next){
     58         int to=e[i].to;
     59         if(to!=pre){
     60             dfs(to,now);
     61         }
     62     }
     63 }
     64 
     65 
     66 int main(){
     67 //#define test
     68 #ifdef test
     69     freopen("in.txt","r",stdin);
     70     freopen("out.txt","w",stdout);
     71 #endif
     72     int n,m;
     73     memset(head,-1,sizeof head);
     74     scanf("%d%d",&n,&m);
     75     for(int i=1;i<=n;i++){
     76         int a;
     77         scanf("%d",&a);
     78         for(int j=0;j<a;j++){
     79             int b; 
     80             scanf("%d",&b);
     81             s[i].insert(b);
     82         }
     83     }
     84     for(int i=0;i<n-1;i++){
     85         int a,b;
     86         scanf("%d%d",&a,&b);
     87         add(a,b);
     88         add(b,a);
     89     }
     90     memset(ans,0,sizeof ans);
     91     cnt=0;
     92     dfs(1,-1);
     93     printf("%d
    ",max(1,cnt));
     94     for(int i=1;i<=m;i++){
     95         if(ans[i])printf("%d ",ans[i]);
     96         else printf("1 ");
     97     }
     98 #ifdef test
     99     fclose(stdin);
    100   fclose(stdout);
    101     system("out.txt");
    102 #endif
    103     return 0;
    104 }
    View Code
  • 相关阅读:
    .NET 并行编程——数据并行
    .NET Unity XML 配置文件(2)
    Java 判断字符串第一位和最后一位,并截取
    Python的datetime模块分析
    springMVC自定义注解实现用户行为验证
    python hex() oct() bin() math 内置函数
    excel表格 xls、xlsx 读取
    Java中对List集合内的元素进行顺序、倒序、随机排序的示例代码
    node服务器中打开html文件的两种方法
    利用 Express 托管静态文件
  • 原文地址:https://www.cnblogs.com/nervendnig/p/8405635.html
Copyright © 2011-2022 走看看