cf刷水题一周记录

来源:互联网 发布:java socket心跳检测 编辑:程序博客网 时间:2024/04/29 02:45

1a

#include <iostream>//2017.8.28 acusing namespace std;int main(){    long long m,n,a;    while(cin>>m>>n>>a)    {        long long m1,n1;        m1=m/a;        n1=n/a;        if(m1*a<m)m1++;        if(n1*a<n)n1++;        cout<<m1*n1;    }    return 0;}

3a

//2017.8.29 ac#include<bits/stdc++.h>using namespace std;int main(){    //cout << "Goodbye world!" << endl;    int ky,dy;    char kx,dx;//以右上为正方向    char dr1,dr2,dr3;    while(cin>>kx>>ky>>dx>>dy)    {        int xl=dx-kx,yl=dy-ky;        int cross=min(abs(xl),abs(yl));        int horiz=max(abs(xl),abs(yl))-cross;        if(xl>0)dr1='R';else dr1='L';        if(yl>0)dr2='U';else dr2='D';        if(abs(xl)>abs(yl))dr3=dr1;else dr3=dr2;        cout<<cross+horiz<<endl;        while(cross--)cout<<dr1<<dr2<<endl;        while(horiz--)cout<<dr3<<endl;    }    return 0;}
4a,没用的水题

#include <iostream>//2017.8.29 ac//useless problemusing namespace std;int main(){    //cout << "Hello world!" << endl;    long long k;    while(cin>>k)    {        if(k>2&&k%2==0)cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }    return 0;}
71a,简单的字符串处理

#include<bits/stdc++.h>//acusing namespace std;int main(){    string a,cur;    int n;    while(cin>>n)    {        while(n--)        {            cin>>a;            cur.clear();            if(a.length()>10)            {                cur.push_back(a[a.length()-1]);                int c=a.length()-2;                char cc;                while(c)                {                    cc=c%10+'0';                    c/=10;                    cur.push_back(cc);                }                cur.push_back(a[0]);                reverse(cur.begin(),cur.end());            }            else cur=a;            cout<<cur<<endl;        }    }    return 0;}
115a,dfs,简单题,一次ac

#include<bits/stdc++.h>//ac//105个测试数据,跑一年using namespace std;long dfs(pair<long,long> a[],long i){    if(a[i].second!=-10)return a[i].second;    if(a[i].second==-10){a[i].second=1+dfs(a,a[i].first);return a[i].second;}}int main(){    pair<long,long> a[50000];//first前驱 second深度    long n,t,cur;    while(cin>>n)    {        memset(a,0,sizeof(a));        for(int i=0;i<n;i++)        {            cin>>cur;            a[i].first=cur-1;            if(cur==-1)                a[i].second=1;            else a[i].second=-10;//-10代表inf        }     //   for(int i=0;i<n;i++){cout<<a[i].first<<" "<<a[i].second<<endl;}        long maxdepth=0;        long j;        for(int i=0;i<n;i++)        {j=dfs(a,i);            if(maxdepth<j)maxdepth=j;        }        cout<<maxdepth<<endl;    }    return 0;}



377a dfs


理论上来说不应该是难题 但是卡了很长时间

题意为,在一个n*m的矩阵里面‘.’表示空'#‘表示墙   输入内'.'会形成一片联通(上下左右)区域  要求将k个'.'换成墙  使余下的'.'仍然联通

思路很简单 dfs一遍'.'  按逆序一个个把坑填了

然而代码底力太挤吧搓了  又是wa又是tle的 debug一年。。。

Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side.

Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any other one. Pavel doesn't like it when his maze has too little walls. He wants to turn exactlyk empty cells into walls so that all the remaining cells still formed a connected area. Help him.

Input

The first line contains three integers n,m,k (1 ≤ n, m ≤ 500,0 ≤ k < s), wheren and m are the maze's height and width, correspondingly,k is the number of walls Pavel wants to add and letters represents the number of empty cells in the original maze.

Each of the next n lines contains m characters. They describe the original maze. If a character on a line equals ".", then the corresponding cell is empty and if the character equals "#", then the cell is a wall.

Output

Print n lines containing m characters each: the new maze that fits Pavel's requirements. Mark the empty cells that you transformed into walls as "X", the other cells must be left without changes (that is, "." and "#").

It is guaranteed that a solution exists. If there are multiple solutions you can output any of them.

Examples
Input
3 4 2#..#..#.#...
Output
#.X#X.#.#...
Input
5 4 5#...#.#..#.....#.#.#
Output
#XXX#X#.X#.....#.#.#

#include<bits/stdc++.h>//tle//2017.8.31using namespace std;pair<char,int> a[505][505];long int k,m,n;int rem;void dfs(int i,int j){   // cout<<",,"<<k<<endl;    if(k==0)return;    int flag=0;    a[i][j].second=rem+1;    if(a[i+1][j].first=='.'&&a[i+1][j].second==rem) {flag++;a[i+1][j].second=rem+1;dfs(i+1,j);}    if(a[i][j+1].first=='.'&&a[i][j+1].second==rem) {flag++;a[i][j+1].second=rem+1;dfs(i,j+1);}    if(a[i-1][j].first=='.'&&a[i-1][j].second==rem) {flag++;a[i-1][j].second=rem+1;dfs(i-1,j);}    if(a[i][j-1].first=='.'&&a[i][j-1].second==rem) {flag++;a[i][j-1].second=rem+1;dfs(i,j-1);}    if(k==0)return;else{k--;a[i][j].first='X';return;}    //if(a[i][j].second==1){coun++;a[i][j].first='X';}}int main(){    //pair<char,int> a[500][500];  //  int m,n;    while(cin>>n>>m>>k)    {        rem=0;        memset(a,0,sizeof(a));        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)                {                    cin>>a[i][j].first;                }        }        bool flag=false;        while(k!=0)        {for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)                {                    if(a[i][j].first=='.')                    {dfs(i,j);                    flag=true;                    break;}                }                if(flag)break;        }   //     rem++;        }    /*    for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)                {                    cout<<a[i][j].second;                }                cout<<endl;        }*/        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)                {                    cout<<a[i][j].first;                }                cout<<endl;        }    }    return 0;}

500a dfs(不

题意  一个向量 每个位子上的值代表这个位置和它往后数多少位的位置有一条单向边(从这个位置往后指) 现在从一号位开始 问能不能仅靠这些单向边遍历到指定位置

分类里面有个dfs就写了个递归  然而一个循环就能完成呀

New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 ton, as a1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.

So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought ofn - 1 positive integersa1, a2, ..., an - 1. For every integeri where1 ≤ i ≤ n - 1 the condition1 ≤ ai ≤ n - i holds. Next, he maden - 1 portals, numbered by integers from 1 ton - 1. The i-th (1 ≤ i ≤ n - 1) portal connects celli and cell(i + ai), and one can travel from celli to cell(i + ai) using thei-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell(i + ai) to celli using the i-th portal. It is easy to see that because of condition1 ≤ ai ≤ n - i one can't leave the Line World using portals.

Currently, I am standing at cell 1, and I want to go to cellt. However, I don't know whether it is possible to go there. Please determine whether I can go to cellt by only using the construted transportation system.

Input

The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) andt (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.

The second line contains n - 1 space-separated integersa1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.

Output

If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".

Examples
Input
8 41 2 1 2 1 2 1
Output
YES
Input
8 51 2 1 2 1 1 1
Output
NO
Note

In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell4.

In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit.

#include<bits/stdc++.h>//住手这tm根本不是dfs//acusing namespace std;bool dfs(long a[],long i,long t){    if(i>t)return false;    if(i==t)return true;    //cout<<i<<endl;    return dfs(a,a[i],t);}int main(){    long a[50000];//传送门    long n,t,cur;    while(cin>>n>>t)    {        memset(a,0,sizeof(a));        for(int i=0;i<n-1;i++)        {            cin>>cur;            a[i]=cur+i;            //cout<<a[i]<<endl;        }        a[n]=0x3f3f3f;        if(dfs(a,0,t-1))cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }    return 0;}


第一次打contest 出了两道签到题  居然加了25分

842a

#include<bits/stdc++.h>using namespace std;long long m,n;pair<long long,int>a[150000];long long dfs(long long i,int k,long long sc){    cout<<i<<" "<<k<<endl;    if(i==n)return k;    if(i<0||i>2*n)return 0x3f3f3f;     long long b,a ;     b=dfs(i*2,k+1,i);     if(i>=sc)a=dfs(i-1,k+1,sc);    if(a<b)return a;else return b;}int main(){    while(cin>>m>>n)    {        memset(a,0,sizeof(a));        cout<<dfs(m,0,0);    }    return 0;}

842b

#include<bits/stdc++.h>using namespace std;int main(){    long long l,d,r,ri,n,coun,x,y;    while(cin>>r>>d>>n)    {        coun=0;        while(n--)        {            cin>>x>>y>>ri;            if((r-d+ri)*(r-d+ri)<=(x*x+y*y)&&(r-ri)*(r-ri)>=(x*x+y*y))coun++;        }        cout<<coun<<endl;    }    return 0;}

849a

#include<bits/stdc++.h>//acusing namespace std;int a[150],total;bool dfs(int f,int l){    bool can=false;    if((l-f)%2==1){can=true;total++;}    else    for(int i=f;i<l-1;i++)    {        if(a[i]%2==1&&a[i+1]%2==1)        {            if(dfs(f,i+1)&&dfs(i+1,l)){can=true;break;}        }    }    return can;}int main(){    int n;    bool flag;    while(cin>>n)    {        memset(a,0,sizeof(a));        total=0;        for(int i=0;i<n;i++)        {            cin>>a[i];        }        if(a[0]%2==0||a[n-1]%2==0)flag=false;        else        {            flag=dfs(0,n);        }        if(flag&&total%2==1)cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }    return 0;}
//todo:520b 2a 5a 849b 842d



原创粉丝点击