现在的位置: 首页 > 综合 > 正文

Codeforces Round #288 (Div. 2)

2019年02月19日 ⁄ 综合 ⁄ 共 4819字 ⁄ 字号 评论关闭

    发现,其实题意都是挺好懂的,如果自己在本子上画画,感觉还是挺简单的,可是转移到代码的时候,发现好难,这次只做出两题,其他题目,我也不知道什么时候才可以做出来、、、、

    链接 :Codeforces Round #290 (Div. 2)
   

A. Pasha and Pixels

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Pasha loves his phone and also putting his hair up... But the hair is now irrelevant.

Pasha has installed a new game to his phone. The goal of the game is following. There is a rectangular field consisting of n row with mpixels
in each row. Initially, all the pixels are colored white. In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy's move the pixel does not change, that is, it remains black.
Pasha loses the game when a 2 × 2 square consisting of black pixels is formed.

Pasha has made a plan of k moves, according to which he will paint pixels. Each turn in his plan is represented as a pair of numbers iand j,
denoting respectively the row and the column of the pixel to be colored on the current move.

Determine whether Pasha loses if he acts in accordance with his plan, and if he does, on what move the 2 × 2 square consisting of black pixels is formed.

Input

The first line of the input contains three integers n, m, k (1 ≤ n, m ≤ 10001 ≤ k ≤ 105) —
the number of rows, the number of columns and the number of moves that Pasha is going to perform.

The next k lines contain Pasha's moves in the order he makes them. Each line contains two integers i and j (1 ≤ i ≤ n1 ≤ j ≤ m),
representing the row number and column number of the pixel that was painted during a move.

Output

If Pasha loses, print the number of the move when the 2 × 2 square consisting of black pixels is formed.

If Pasha doesn't lose, that is, no 2 × 2 square consisting of black pixels is formed during the given k moves,
print 0.

Sample test(s)
input
2 2 4
1 1
1 2
2 1
2 2
output
4
input
2 3 6
2 3
2 2
1 3
2 2
1 2
1 1
output
5
input
5 3 7
2 3
1 2
1 1
4 1
3 1
5 3
3 2
output
0

    题意:在n*m方块里(初始全为白色),对指定方块涂上黑色,求出第几次涂时,可以使得其中有2*2的大方块可以全被涂上黑色,在同一方块上涂鸦,保持不变,即保持黑色。 一开始看到是暴力的时候,就无脑for 了,然后就TLE 了、

    每次涂色时,判断旁边的方块即可。

    

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

#define N 1010

bool col[N][N];
int n, m, k;

bool judge(int x, int y)
{
    if(col[x][y] && col[x + 1][y] && col[x][y - 1] && col[x + 1][y - 1])
        return true;
    else if(col[x][y] && col[x - 1][y] && col[x - 1][y - 1] && col[x][y - 1])
        return true;
    else if(col[x][y] && col[x][y + 1] && col[x - 1][y] && col[x - 1][y + 1] )
        return true;
    else if(col[x][y] && col[x][y + 1] && col[x + 1][y] && col[x + 1][y + 1])
        return true;
    else
        return false;
}

int main()
{
    int a, b;

    while(~scanf("%d%d%d",&n,&m,&k))
    {
        memset(col, false, sizeof(col));
        bool flag = false;
        int ans = 0;
        for(int t = 0; t < k ; t ++)
        {
            scanf("%d%d",&a, &b);
            if(flag)
                continue;
            if(col[a][b])
                continue;
            col[a][b] = true;
            if(judge(a, b))
                flag = true;
            if(flag)
                ans = t + 1;
        }

        if(flag)
        {
            printf("%d\n",ans);
        }
        else
            printf("0\n");
    }
}

B. Anton and currency you all know

time limit per test

0.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that to simplify the calculations, its fractional part was neglected and the exchange rate is
now assumed to be an integer.

Reliable sources have informed the financier Anton of some information about the exchange rate of currency you all know against the burle for tomorrow. Now Anton knows that tomorrow the exchange
rate will be an even number, which can be obtained from the present rate by swapping exactly two distinct digits in it. Of all the possible values that meet these conditions, the exchange rate for tomorrow will be the maximum possible. It is guaranteed that
today the exchange rate is an odd positive integer n. Help Anton to determine the exchange
rate of currency you all know for tomorrow!

Input

The first line contains an odd positive integer n — the exchange rate of currency you all know for
today. The length of number n's representation is within range from 2 to 105,
inclusive. The representation of n doesn't contain any leading zeroes.

Output

If the information about tomorrow's exchange rate is inconsistent, that is, there is no integer that meets the condition, print  - 1.

Otherwise, print the exchange rate of currency you all know against the burle for tomorrow. This should be the maximum possible number of those that are even and that are obtained from today's
exchange rate by swapping exactly two digits. Exchange rate representation should not contain leading zeroes.

Sample test(s)
input
527
output
572
input
4573
output
3574
input
1357997531
output
-1

    题目输入为奇数,要求使得调换其中两个位置上的数,使得该数变为偶数,并且输出该数的最大的可能值。既然是奇数,那就一定是个位数(设为maxn)和其他位上的偶数进行调换了。我们可以这样看,为了保持调换后的数值最大,从第一位数开始,找到第一个比maxn小的数,调换即可,如果没找到,那就找到最后一个偶数再调换即可。如果该数每个位上的数都是奇数,输出-1。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
using namespace std;

#define N 100010

char s[N];
int ans[10];

int main()
{
    while(~scanf("%s",s))
    {
        memset(ans, 0, sizeof(ans));

        int len = strlen(s);
        bool ok = false, ok2 = false;
        int ans = 0;
        int maxn = s[len - 1] - '0';

        for(int i = 0; i < len; i ++)
        {
            if(ok2)
                continue;
            int tmp1 = s[i] - '0';
            if(tmp1 % 2 == 0)
            {
                if(tmp1 < maxn)
                {
                    ans = i;
                    ok2 = true;
                }
                ok = true;
            }
        }

        if(ans == 0 && !ok2)
        {
            for(int i = 0; i < len; i ++)
            {
                int tmp1 = s[i] - '0';
                if(tmp1 % 2 == 0)
                {
                    ans = i;
                    ok = true;
                }
            }
        }

        if(!ok)
        {
            printf("-1\n");
            continue;
        }

        char tmp3;
        tmp3 = s[ans];
        s[ans] = s[len - 1];
        s[len - 1] = tmp3;

        for(int i = 0; i < len; i ++)
        {
            printf("%c",s[i]);
        }
        printf("\n");
    }
    return 0;
}

抱歉!评论已关闭.