zoukankan      html  css  js  c++  java
  • Codeforces Round #442 (Div. 2)

                                  A. Alex and broken contest

    One day Alex was creating a contest about his friends, but accidentally deleted it. Fortunately, all the problems were saved, but now he needs to find them among other problems.

    But there are too many problems, to do it manually. Alex asks you to write a program, which will determine if a problem is from this contest by its name.

    It is known, that problem is from this contest if and only if its name contains one of Alex's friends' name exactly once. His friends' names are "Danil", "Olya", "Slava", "Ann" and "Nikita".

    Names are case sensitive.

    Input

    The only line contains string from lowercase and uppercase letters and "_" symbols of length, not more than 100 — the name of the problem.

    Output

    Print "YES", if problem is from this contest, and "NO" otherwise.

    Examples
    Input
    Alex_and_broken_contest
    Output
    NO
    Input
    NikitaAndString
    Output
    YES
    Input
    Danil_and_Olya
    Output
    NO
    题解:数据太小了,用不上kmp,直接暴力。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 string S;
     5 string a[5]={"Danil","Olya","Slava","Ann","Nikita"};
     6 
     7 int main()
     8 {   cin>>S;
     9     int n=S.size(),cnt=0;
    10     for(int i=0;i<n;i++){
    11         string temp="";
    12         for(int j=i;j<n;j++){
    13             temp+=S[j];
    14             for(int k=0;k<5;k++) if(temp==a[k]) cnt++;
    15         }
    16     }
    17     if(cnt==0||cnt>1) cout<<"NO"<<endl;
    18     else cout<<"YES"<<endl;
    19 }

                                B. Nikita and string

    One day Nikita found the string containing letters "a" and "b" only.

    Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".

    Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?

    Input

    The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".

    Output

    Print a single integer — the maximum possible size of beautiful string Nikita can get.

    Examples
    Input
    abba
    Output
    4
    Input
    bab
    Output
    2
    Note

    It the first sample the string is already beautiful.

    In the second sample he needs to delete one of "b" to make it beautiful

    题解:5000的长度,直接暴力枚举中间段的位置,用两个数组存下左边需要删除的b的个数,右边需要删除的b的个数。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=5005;
     5 
     6 string S;
     7 int n,a[maxn],b[maxn],c[maxn];
     8 
     9 void Inite(){
    10     int cnt=0;
    11     for(int i=1;i<=n;i++){
    12         if(S[i]=='b') cnt++;
    13         a[i]=cnt;
    14     }
    15     cnt=0;
    16     for(int i=n;i>=1;i--){
    17         if(S[i]=='b') cnt++;
    18         b[i]=cnt;
    19     }
    20     cnt=0;
    21     for(int i=1;i<=n;i++){
    22         if(S[i]=='a') cnt++;
    23         c[i]=cnt;
    24     }
    25 }
    26 
    27 
    28 void Solve(){
    29     int ans=0;
    30     for(int i=1;i<=n;i++){
    31         for(int j=i;j<=n;j++)
    32             ans=max(ans,n-a[i-1]-b[j+1]-(c[j]-c[i]));
    33     }
    34     cout<<ans<<endl;
    35 }
    36 
    37 int main()
    38 {   cin>>S;
    39     n=S.size();
    40     S='#'+S+'@';
    41     Inite();
    42     Solve();
    43     
    44 }

                               C. Slava and tanks

    Slava plays his favorite game "Peace Lightning". Now he is flying a bomber on a very specific map.

    Formally, map is a checkered field of size 1 × n, the cells of which are numbered from 1 to n, in each cell there can be one or several tanks. Slava doesn't know the number of tanks and their positions, because he flies very high, but he can drop a bomb in any cell. All tanks in this cell will be damaged.

    If a tank takes damage for the first time, it instantly moves to one of the neighboring cells (a tank in the cell n can only move to the cell n - 1, a tank in the cell 1 can only move to the cell 2). If a tank takes damage for the second time, it's counted as destroyed and never moves again. The tanks move only when they are damaged for the first time, they do not move by themselves.

    Help Slava to destroy all tanks using as few bombs as possible.

    Input

    The first line contains a single integer n (2 ≤ n ≤ 100 000) — the size of the map.

    Output

    In the first line print m — the minimum number of bombs Slava needs to destroy all tanks.

    In the second line print m integers k1, k2, ..., km. The number ki means that the i-th bomb should be dropped at the cell ki.

    If there are multiple answers, you can print any of them.

    Examples
    Input
    2
    Output
    3
    2 1 2
    Input
    3
    Output
    4
    2 1 3 2
    题解:是一道有趣的题,先炸偶数位,再炸奇数位,回头再炸偶数位(根据案例来说,也是先炸偶数位),如果先炸奇数位,可能仅多1次。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int maxn=2e5;
     5 
     6 int n,ans;
     7 int a[maxn];
     8 
     9 int main()
    10 {   cin>>n;
    11     ans=n+n/2;
    12     cout<<ans<<endl;
    13     int cnt=0;
    14     for(int i=2;i<=n;i+=2) a[cnt++]=i;
    15     for(int i=1;i<=n;i+=2) a[cnt++]=i;
    16     for(int i=2;i<=n;i+=2) a[cnt++]=i;
    17     for(int i=0;i<cnt;i++) printf("%d%c",a[i],i==cnt-1?'
    ':' '); 
    18 }

                                 D. Olya and Energy Drinks

    Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.

    Formally, her room can be represented as a field of n × m cells, each cell of which is empty or littered with cans.

    Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.

    Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?

    It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.

    Input

    The first line contains three integers n, m and k (1 ≤ n, m, k ≤ 1000) — the sizes of the room and Olya's speed.

    Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.

    The last line contains four integers x1, y1, x2, y2 (1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m) — the coordinates of the first and the last cells.

    Output

    Print a single integer — the minimum time it will take Olya to get from (x1, y1) to (x2, y2).

    If it's impossible to get from (x1, y1) to (x2, y2), print -1.

    Examples
    Input
    3 4 4
    ....
    ###.
    ....
    1 1 3 1
    Output
    3
    Input
    3 4 1
    ....
    ###.
    ....
    1 1 3 1
    Output
    8
    Input
    2 2 1
    .#
    #.
    1 1 2 2
    Output
    -1
    Note

    In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.

    In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.

    Olya does not recommend drinking energy drinks and generally believes that this is bad

    题解:经过R总和各位大佬的帮助,算是暴力的过了这道题。普通的BFS,只是要学会剪枝,还要判断相同的点是否能多次经过(wa了多次)

      1 #include<queue>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<iostream>
      5 #include<algorithm>
      6 using namespace std;
      7 typedef pair<int,int> P;
      8 
      9 const int maxn=1005;
     10 
     11 int n,m,k,sx,sy,ex,ey;
     12 int d[maxn][maxn];
     13 int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
     14 char map[maxn][maxn];
     15 
     16 int BFS(int a,int b){
     17     memset(d,-1,sizeof(d));
     18     queue<P> q;
     19     q.push(P(a,b));
     20     
     21     d[a][b]=0;
     22     while(!q.empty()){
     23         P p=q.front();
     24         q.pop();
     25         
     26         if(p.first==ex-1&&p.second==ey-1) return d[p.first][p.second];
     27         for(int i=0;i<4;i++){
     28             int mx=p.first,my=p.second;
     29             for(int j=1;j<=k;j++){
     30                 mx=mx+dx[i];
     31                 my=my+dy[i];
     32                 if(mx<0||mx>=n||my<0||my>=m||map[mx][my]=='#') break;
     33                 if(d[mx][my]>=0&&d[mx][my]<=d[p.first][p.second]) break;    //剪枝,说明(mx,my)被访问过,且有较短路径到这个点。
     34                 if(d[mx][my]<=d[p.first][p.second]) q.push(P(mx,my));       //剪枝,可以得到d[mx][my]=-1,既未被访问。
     35                 d[mx][my]=d[p.first][p.second]+1;
     36             }
     37         }
     38     }
     39     return -1;
     40 }
     41 
     42 int main()
     43 {   cin>>n>>m>>k;
     44     for(int i=0;i<n;i++) scanf("%s",map[i]);
     45     cin>>sx>>sy>>ex>>ey;
     46     cout<<BFS(sx-1,sy-1)<<endl;
     47 }
     48 
     49 /*  错误的标记方式
     50 #include<queue>
     51 #include<cstdio>
     52 #include<cstring>
     53 #include<iostream>
     54 #include<algorithm>
     55 using namespace std;
     56 
     57 struct point{
     58     int x,y,step;
     59     point(int x,int y,int step):x(x),y(y),step(step){}; 
     60 };
     61 
     62 const int maxn=1005;
     63 
     64 int n,m,k,sx,sy,ex,ey;
     65 int vis[maxn][maxn],dis[maxn][maxn];
     66 int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
     67 char map[maxn][maxn];
     68 
     69 int BFS(int a,int b){
     70     memset(vis,0,sizeof(vis));
     71     queue<point> q;
     72     vis[a][b]=true;
     73     q.push(point(a,b,0));
     74     
     75     while(!q.empty()){
     76         point p=q.front();
     77         q.pop();
     78         
     79         if(p.x==ex-1&&p.y==ey-1) return p.step;
     80         for(int i=0;i<4;i++){
     81             int mx=p.x,my=p.y;
     82             for(int j=1;j<=k;j++){
     83                 mx=mx+dx[i];
     84                 my=my+dy[i];
     85                 if(mx<0||mx>=n||my<0||my>=m||vis[mx][my]||map[mx][my]=='#') break;
     86                 vis[mx][my]=true;
     87                 q.push(point(mx,my,p.step+1));
     88             }
     89         }
     90     }
     91     return -1;
     92 }
     93 
     94 int main()
     95 {   cin>>n>>m>>k;
     96     for(int i=0;i<n;i++) scanf("%s",map[i]);
     97     cin>>sx>>sy>>ex>>ey;
     98     cout<<BFS(sx-1,sy-1)<<endl;
     99 }
    100 */
  • 相关阅读:
    IP是什么 DNS 域名与IP有什么不同
    空间、域名与IP之间的关系?
    杨学明老师为深圳某上市企业提供《软件测试技术》内训服务!
    共创力与某上市企业合作的第三期咨询项目正式启动!
    2017.7.28~29,热烈庆祝杨学明老师《研发项目管理》杭州公开课成功举办!
    2017年7月22日~23日,深圳市共创力为某上市企业提供整机设计工程内训服务!
    深圳市共创力咨询第二期“总裁直通车”成功举办!
    阿里巴巴产品需求工程师的三个层次
    共创力咨询推出“总裁直通车”服务!
    2017年7月7日~8日,杨学明老师为深圳蛇口某企业内训课程服务!
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7724504.html
Copyright © 2011-2022 走看看