Codeforces Round #237 (Div. 2) 总结

11 篇文章 0 订阅
4 篇文章 0 订阅

    周三晚上的比赛,掉渣渣了。本来打算紫的,结果挂了两题,掉100分。

    现在把题目都A了,发现了一些问题,算是收获吧。

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
5
xooox
oxoxo
soxoo
oxoxo
xooox
output
NO
input
3
wsw
sws
wsw
output
YES
input
3
xpx
pxp
xpe
output
NO

    A很简单,判断是不是x形。唯一要注意的是字母不能全部都一样,我在这里挂了一次。简单来说就是要好好看看题意。附代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;

char str[1000];

int main()
{
    int n;
    scanf("%d",&n);

    char a=0,b=0;

    bool flag = true;
    for(int i=0;i<n;i++)
    {
        scanf("%s",str);
        if(a==0 && b==0)
            a=str[0],b=str[1];

        if(a==b) flag=false;

        for(int j=0;j<n;j++)
        {
            if(j==i || i+j==n-1)
            {
                if(str[j]!=a) flag=false;
            }
            else if(str[j]!=b)
            {
                flag=false;
            }
        }
    }

    puts(flag?"YES":"NO");
}


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 equals nd + 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 5
2
output
1.0000000000 2.0000000000
2.0000000000 0.0000000000
input
4.147 2.8819
6
output
2.8819000000 0.0000000000
4.1470000000 1.6168000000
3.7953000000 4.1470000000
0.9134000000 4.1470000000
0.0000000000 2.1785000000
0.7034000000 0.0000000000


    B题。正方形跑道,问你每经过d距离后的坐标。很像浮点数取模,不过直接除然后转int再减是不行的,精度问题。可以保留每次在一圈中经过的距离,每次加上d来计算。

    附代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;

int main()
{
    double a,d;
    int n;

    scanf("%lf%lf%d",&a,&d,&n);

    double remain = 0;
    for(int i=1;i<=n;i++)
    {
        double total = remain + d;
        int circal = total/(a*4);
        remain = total - a*4*circal;
        double dis = remain - a*((int)(remain/a));

        double x, y;
        switch((int)(remain/a))
        {
            case 0:x=dis,y=0;break;
            case 1:x=a,y=dis;break;
            case 2:x=a-dis,y=a;break;
            case 3:x=0,y=a-dis;break;
        }

        printf("%.9lf %.9lf\n", x,y);
    }
}

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 2
0 1 1
output
3
1 2
1 3
3 2
input
4 2
2 0 1 3
output
3
1 3
1 4
2 3
input
3 1
0 0 0
output
-1

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
int dis[1000010];
int first[1000010],nxt[1000010],vv[1000010];

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);

    int e=2;
    int maxL=0;
    for(int i=1;i<=n;i++)
    {
        int d;
        scanf("%d",&d);
        maxL = max(maxL, d);
        dis[d]++;
        nxt[e]=first[d],vv[e]=i,first[d]=e++;
    }

    if(dis[0]!=1 || (n>1 && dis[1]>k))
    {
        puts("-1");
        return 0;
    }

    for(int i=2;i<=maxL;i++)
    {
        if(dis[i]>(long long)(k-1)*dis[i-1])
        {
            puts("-1");
            return 0;
        }
    }

    printf("%d\n", n-1);
    for(int ee=first[1];ee;ee=nxt[ee])
    {
        printf("%d %d\n", vv[first[0]], vv[ee]);
    }

    for(int i=2;i<=maxL;i++)
    {
        int aa=first[i-1];
        int num=0;
        for(int bb=first[i];bb;bb=nxt[bb])
        {
            printf("%d %d\n", vv[aa], vv[bb]);
            num++;
            if(num==k-1) num=0,aa=nxt[aa];
        }
    }
}

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.


    D题题意就是扫雷。每个位置可能是0,1,2,*。可以保存当前点的三种状态:放地雷:2,不放地雷且下一格必须放地雷:1,不放地雷且下一格不能放地雷:0。

    初始条件 dp[0][1] = dp[0][0] = 1。一路dp即可。附代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
const int mod = 1000000007;
const int maxn = 1111111;
LL dp[maxn][3];
char str[maxn];

int main()
{
    scanf("%s",str+1);
    int len = strlen(str+1);

    dp[0][0] = dp[0][1] = 1;
    for(int i=1;i<=len;i++)
    {
        switch(str[i])
        {
        case '?':
            dp[i][0] = (dp[i-1][0] + dp[i-1][2])%mod;
            dp[i][1] = (dp[i-1][0] + dp[i-1][2])%mod;
            dp[i][2] = (dp[i-1][1] + dp[i-1][2])%mod;
            break;
        case '0':
            dp[i][0] = dp[i-1][0];
            break;
        case '1':
            dp[i][0] = dp[i-1][2];
            dp[i][1] = dp[i-1][0];
            break;
        case '2':
            dp[i][1] = dp[i-1][2];
            break;
        case '*':
            dp[i][2] = (dp[i-1][1] + dp[i-1][2])%mod;
            break;
        }
    }

    printf("%d\n", (int)((dp[len][0]+dp[len][2])%mod));
}

E. Maze 1D
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera has a strip infinite in both directions and consisting of cells. The cells are numbered by integers. The cell number 0 has a robot.

The robot has instructions — the sequence of moves that he must perform. In one move, the robot moves one cell to the left or one cell to the right, according to instructions. Before the robot starts moving, Valera puts obstacles in some cells of the strip, excluding cell number 0. If the robot should go into the cell with an obstacle according the instructions, it will skip this move.

Also Valera indicates the finish cell in which the robot has to be after completing the entire instructions. The finishing cell should be different from the starting one. It is believed that the robot completed the instructions successfully, if during the process of moving he visited the finish cell exactly once — at its last move. Moreover, the latter move cannot be skipped.

Let's assume that k is the minimum number of obstacles that Valera must put to make the robot able to complete the entire sequence of instructions successfully and end up in some finishing cell. You need to calculate in how many ways Valera can choose k obstacles and the finishing cell so that the robot is able to complete the instructions successfully.

Input

The first line contains a sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), consisting only of letters "L" and "R". If character si equals "L", then the robot on the i-th move must try to move one cell to the left. If the si-th character equals "R", then the robot on the i-th move must try to move one cell to the right.

Output

Print a single integer — the required number of ways. It's guaranteed that this number fits into 64-bit signed integer type.

Sample test(s)
input
RR
output
1
input
RRL
output
1
Note

In the first sample Valera mustn't add any obstacles and his finishing cell must be cell 2.

In the second sample, Valera must add an obstacle in cell number 1, and his finishing cell must be cell number  - 1. In this case robot skips the first two moves and on the third move he goes straight from the starting cell to the finishing one. But if Valera doesn't add any obstacles, or adds an obstacle to another cell, then the robot visits the finishing cell more than once.


    E题其实不难,最后一步必然走到没走过的地方,比如左边,那么障碍物必然放在右边,且最多一个障碍物。二分查找最大的满足条件的位置。特殊情况是结果为len,表示可以不放障碍物;0,无法完成目标。附代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char str[1111111];
char last;
int len;

bool check(int n)
{
    int pos=0;
    int minp=0;
    for(int i=0;i<len;i++)
    {
        minp = min(minp,pos);
        if(str[i]==last)
            pos--;
        else
            pos++;
        if(pos==n) pos--;
    }
    return pos<minp;
}

int main()
{
    scanf("%s",str);
    len = strlen(str);
    last = str[len-1];

    int l=1,r=len;
    while(l<=r)
    {
        int m = (l+r)>>1;
        if(!check(m))
            r = m-1;
        else
            l = m+1;
    }

    printf("%d\n", r==len?1:r);
}

    CodeForces的确是一个很好的比赛,能提高写代码速度,发现自身问题。以后继续努力变紫吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值