Codeforces Round #237 (Div. 2) A~D

来源:互联网 发布:女朋友很强体验知乎 编辑:程序博客网 时间:2024/04/27 14:08


A. Valera and X
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera is a little boy. Yesterday he got a huge Math hometask at school, so Valera didn't have enough time to properly learn the English alphabet for his English lesson. Unfortunately, the English teacher decided to have a test on alphabet today. At the test Valera got a square piece of squared paper. The length of the side equals n squares (n is an odd number) and each unit square contains some small letter of the English alphabet.

Valera needs to know if the letters written on the square piece of paper form letter "X". Valera's teacher thinks that the letters on the piece of paper form an "X", if:

  • on both diagonals of the square paper all letters are the same;
  • all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals.

Help Valera, write the program that completes the described task for him.

Input

The first line contains integer n (3 ≤ n < 300n is odd). Each of the next n lines contains n small English letters — the description of Valera's paper.

Output

Print string "YES", if the letters on the paper form letter "X". Otherwise, print string "NO". Print the strings without quotes.

Sample test(s)
input
5xoooxoxoxosoxoooxoxoxooox
output
NO
input
3wswswswsw
output
YES
input
3xpxpxpxpe
output
NO

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n;char str[500][500];bool flag=true;int main(){cin>>n;for(int i=0;i<n;i++) scanf("%s",str[i]);char sg=str[0][0];for(int i=0;i<n;i++){if(str[i][i]!=sg||str[i][n-1-i]!=sg){flag=false; break;}}if(!flag||sg==str[0][1]) {puts("NO");return 0;}sg=str[0][1];for(int i=0;i<n;i++){for(int j=0;j<n;j++){if(i==j||j==n-1-i) continue;if(str[i][j]!=sg){flag=false;break;}}}if(flag) puts("YES"); else puts("NO");return 0;}

B. Marathon
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0, 0) and the length of the side equals a meters. The sides of the square are parallel to coordinate axes.

As the length of the marathon race is very long, Valera needs to have extra drink during the race. The coach gives Valera a bottle of drink each d meters of the path. We know that Valera starts at the point with coordinates (0, 0) and runs counter-clockwise. That is, when Valera covers a meters, he reaches the point with coordinates (a, 0). We also know that the length of the marathon race equalsnd + 0.5 meters.

Help Valera's coach determine where he should be located to help Valera. Specifically, determine the coordinates of Valera's positions when he covers d, 2·d, ..., n·d meters.

Input

The first line contains two space-separated real numbers a and d (1 ≤ a, d ≤ 105), given with precision till 4 decimal digits after the decimal point. Number a denotes the length of the square's side that describes the stadium. Number d shows that after each d meters Valera gets an extra drink.

The second line contains integer n (1 ≤ n ≤ 105) showing that Valera needs an extra drink n times.

Output

Print n lines, each line should contain two real numbers xi and yi, separated by a space. Numbers xi and yi in the i-th line mean that Valera is at point with coordinates (xi, yi) after he covers i·d meters. Your solution will be considered correct if the absolute or relative error doesn't exceed 10 - 4.

Note, that this problem have huge amount of output data. Please, do not use cout stream for output in this problem.

Sample test(s)
input
2 52
output
1.0000000000 2.00000000002.0000000000 0.0000000000
input
4.147 2.88196
output
2.8819000000 0.00000000004.1470000000 1.61680000003.7953000000 4.14700000000.9134000000 4.14700000000.0000000000 2.17850000000.7034000000 0.0000000000
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;long long i,k,n;double a,d,e,l;int main(){    scanf("%lf%lf%I64d",&a,&d,&n);    for(i=1;i<=n;i++)    {        l=d*i;        k=(long long)(l/a);        e=l-k*a;        k=k%4;        if(k==0)        {            printf("%.8lf %.8lf\n",e,0.);        }        else if(k==1)        {            printf("%.8lf %.8lf\n",a,e);        }        else if(k==2)        {            printf("%.8lf %.8lf\n",a-e,a);        }        else if(k==3)        {            printf("%.8lf %.8lf\n",0.,a-e);        }    }    return 0;}


C. Restore Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera had an undirected connected graph without self-loops and multiple edges consisting of n vertices. The graph had an interesting property: there were at most k edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to n.

One day Valera counted the shortest distances from one of the graph vertices to all other ones and wrote them out in array d. Thus, element d[i] of the array shows the shortest distance from the vertex Valera chose to vertex number i.

Then something irreparable terrible happened. Valera lost the initial graph. However, he still has the array d. Help him restore the lost graph.

Input

The first line contains two space-separated integers n and k (1 ≤ k < n ≤ 105). Number n shows the number of vertices in the original graph. Number k shows that at most k edges were adjacent to each vertex in the original graph.

The second line contains space-separated integers d[1], d[2], ..., d[n] (0 ≤ d[i] < n). Number d[i] shows the shortest distance from the vertex Valera chose to the vertex number i.

Output

If Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer m (0 ≤ m ≤ 106) — the number of edges in the found graph.

In each of the next m lines print two space-separated integers ai and bi (1 ≤ ai, bi ≤ nai ≠ bi), denoting the edge that connects vertices with numbers ai and bi. The graph shouldn't contain self-loops and multiple edges. If there are multiple possible answers, print any of them.

Sample test(s)
input
3 20 1 1
output
31 21 33 2
input
4 22 0 1 3
output
31 31 42 3
input
3 10 0 0
output
-1
卡int。。。

#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <cmath>using namespace std;const int maxn=110000;const int INF=0x3f3f3f3f;int n,k,d[maxn],maxd=-INF,cnt[maxn];vector<int> g[maxn];struct Edge{    int to,next;}E[maxn];int Size,Adj[maxn];void init(){    Size=0;    memset(Adj,-1,sizeof(Adj));    memset(cnt,0,sizeof(cnt));}void Add_Edge(int u,int v){    E[Size].to=v;E[Size].next=Adj[u];Adj[u]=Size++;}int main(){    scanf("%d%d",&n,&k);    int src=-1,D=0;    for(int i=1;i<=n;i++)    {        scanf("%d",&d[i]);        maxd=max(maxd,d[i]);        if(d[i]==0) src=i;        g[d[i]].push_back(i);    }    bool flag=true;    if(g[0].size()!=1)    {        puts("-1"); return 0;    }    for(int i=1;i<=maxd;i++)    {        if(g[i].size()==0)        {            flag=false; break;        }        if(i==1)        {            if(g[i].size()>k)            {                flag=false;                break;            }        }        else        {            if((long long int )(g[i].size())>(long long int )(g[i-1].size())*(k-1))            {                flag=false;                break;            }        }    }    if(flag==false)    {        puts("-1"); return 0;    }    init();    ///......build......    for(int i=0;i<g[1].size();i++)    {        Add_Edge(src,g[1][i]);        D++;    }    for(int i=1;i<maxd;i++)    {        int sz=g[i+1].size();        int u=g[i][0],pos=1;        for(int j=0;j<sz;j++)        {            int v=g[i+1][j];            if(cnt[u]<k-1)            {                cnt[u]++;                Add_Edge(u,v);                D++;            }            else            {                u=g[i][pos++];                cnt[u]++;                Add_Edge(u,v);                D++;            }        }    }    printf("%d\n",D);    for(int i=1;i<=n;i++)    {        for(int j=Adj[i];~j;j=E[j].next)        {            printf("%d %d\n",i,E[j].to);        }    }    return 0;}

D. Minesweeper 1D
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares.

For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs.

Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.

Input

The first line contains sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square.

Output

Print a single integer — the number of ways Valera can fill the empty cells and get a correct field.

As the answer can be rather large, print it modulo 1000000007 (109 + 7).

Sample test(s)
input
?01???
output
4
input
?
output
2
input
**12
output
0
input
1
output
0
Note

In the first test sample you can get the following correct fields: 001**1001***001*2*001*10.



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const long long int MOD=1000000007;const int maxn=1100000;char str[maxn];long long int dp[maxn][2][2];int n;int main(){    scanf("%s",&str);    n=strlen(str);    dp[0][0][1]=dp[0][0][0]=1;    for(int i=1;i<=n;i++)    {        if(str[i-1]=='?')        {            ///str[i-1]=='*'            dp[i][1][0]=(dp[i][1][0]+dp[i-1][0][1])%MOD;            dp[i][1][0]=(dp[i][1][0]+dp[i-1][1][1])%MOD;            dp[i][1][1]=(dp[i][1][1]+dp[i-1][0][1])%MOD;            dp[i][1][1]=(dp[i][1][1]+dp[i-1][1][1])%MOD;            ///str[i-1]=='0'            dp[i][0][0]=(dp[i][0][0]+dp[i-1][0][0])%MOD;            ///str[i-1]=='1'            dp[i][0][1]=(dp[i][0][1]+dp[i-1][0][0])%MOD;            dp[i][0][0]=(dp[i][0][0]+dp[i-1][1][0])%MOD;            ///str[i-1]=='2'            dp[i][0][1]=(dp[i][0][1]+dp[i-1][1][0])%MOD;        }        else        {            if(str[i-1]=='*')            {                dp[i][1][0]=(dp[i][1][0]+dp[i-1][0][1])%MOD;                dp[i][1][0]=(dp[i][1][0]+dp[i-1][1][1])%MOD;                dp[i][1][1]=(dp[i][1][1]+dp[i-1][0][1])%MOD;                dp[i][1][1]=(dp[i][1][1]+dp[i-1][1][1])%MOD;            }            else if(str[i-1]=='0')            {                dp[i][0][0]=(dp[i][0][0]+dp[i-1][0][0])%MOD;            }            else if(str[i-1]=='1')            {                dp[i][0][1]=(dp[i][0][1]+dp[i-1][0][0])%MOD;                dp[i][0][0]=(dp[i][0][0]+dp[i-1][1][0])%MOD;            }            else if(str[i-1]=='2')            {                dp[i][0][1]=(dp[i][0][1]+dp[i-1][1][0])%MOD;            }        }    }    printf("%I64d\n",(dp[n][1][0]+dp[n][0][0])%MOD);    return 0;}






1 0
原创粉丝点击