Codeforces Round #288 (Div. 2)(A,B,C)

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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>

using namespace std;
const int inf=0x3f3f3f3f;
int map[1010][1010];
int main()
{
    int n,m,k,i;
    int x,y;
    int flag;
    int cnt;
    while(~scanf("%d %d %d",&n,&m,&k)) {
        memset(map,0,sizeof(map));
        flag=0;
        cnt=0;
        for(i=0; i<k; i++) {
            scanf("%d %d",&x,&y);
            if(!flag) {
                map[x][y]=1;
                if(x-1>=1&&x-1<=n&&y-1>=1&&y-1<=m) {
                    if(map[x-1][y-1]&&map[x][y-1]&&map[x-1][y]) {
                        cnt=i+1;
                        flag=1;
                    }
                }
                if(x-1>=1&&x-1<=n&&y+1>=1&&y+1<=m) {
                    if(map[x-1][y] && map[x-1][y+1]&&map[x][y+1]) {
                        cnt=i+1;
                        flag=1;
                    }

                } if(x+1>=1&&x+1<=n&&y-1>=1&&y-1<=m) {
                    if(map[x][y-1] && map[x+1][y-1]&&map[x+1][y]) {
                        cnt=i+1;
                        flag=1;
                    }

                }
                if(x+1>=1&&x+1<=n&&y+1>=1&&y+1<=m) {
                    if(map[x][y+1] && map[x+1][y+1]&& map[x+1][y]) {
                        cnt=i+1;
                        flag=1;
                    }

                }
            }

        }
        printf("%d\n",cnt);
    }
    return 0;
}

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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>

using namespace std;
const int inf=0x3f3f3f3f;
char a[100010];
int main()
{
    int i;
    int c;
    char str;
    int flag1,flag2;
    while(~scanf("%s",a)) {
        flag1=flag2=0;
        c=-1;
        int l=strlen(a);
        int key=a[l-1]-'0';
        for(i=0;i<l;i++) {
            int t=a[i]-'0';;
            if(t%2==0&&key>t) {
                flag1=1;
                str=a[i];
                a[i]=a[l-1];
                a[l-1]=str;
                flag2=1;
                break;
            } else if(t%2==0) {
                flag1=1;
                c=i;
            }
        }
        if(!flag1) {
            printf("-1\n");
            continue;
        } else if(flag2) {
            printf("%s\n",a);
        } else {
            str=a[c];
            a[c]=a[l-1];
            a[l-1]=str;
            printf("%s\n",a);
        }
    }
    return 0;
}

C. Anya and Ghosts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.

For each of the m ghosts Anya knows the time at which it comes: the i-th visit will happen wi seconds after midnight, all wi's are distinct. Each visit lasts exactly one second.

What is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time.

Input

The first line contains three integers mtr (1 ≤ m, t, r ≤ 300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.

The next line contains m space-separated numbers wi (1 ≤ i ≤ m1 ≤ wi ≤ 300), the i-th of them repesents at what second after the midnight the i-th ghost will come. All wi's are distinct, they follow in the strictly increasing order.

Output

If it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that.

If that is impossible, print  - 1.

Sample test(s)
input
1 8 3
10
output
3
input
2 10 1
5 8
output
1
input
1 1 3
10
output
-1
Note

Anya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.

It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.

In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight.

In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.

In the third sample test the answer is  - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes.


题意为有m个鬼(第二行为m只鬼到来的时间,为递增序列),一支蜡烛可以照t秒,每只鬼需要r只蜡烛照着,问最少需要点多少只蜡烛
需要注意的是蜡烛点燃的时间为1s,说明当第一只鬼来的时候(来的时间假设为i),我们要在[i-t,i-1]这个时间段中点燃蜡烛即可,但为了让每只蜡烛取得最大的价值,所以蜡烛的点燃时间要越接近i-1具体看程序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>

using namespace std;
int a[410];
int main()
{
    int m,t,r;
    while(~scanf("%d%d%d",&m,&t,&r))      //鬼的数量,蜡烛燃烧的时间,鬼来的时候需要多少蜡烛照着
    {
        for(int i=0; i<m; i++)            //依次为每只鬼来的时间
            scanf("%d",&a[i]);
        int s[410];
        memset(s,0,sizeof(s));            //用来记录没个时间点有几根蜡烛照着,初始为0
        int b[410];
        memset(b,0,sizeof(b));            //用来记录当前时间是否点过蜡烛,初始为0,点过为1
        int flag;                         //标记是否当前时间点的鬼是否有足够的蜡烛照着,不满足跳出输出-1
        int ans=0;                        //点蜡烛的次数
        for(int i=0; i<m; i++)
        {
            if(s[a[i]]>=r)               //当前时间点点燃的蜡烛数量足够,则判断下一只鬼来的时间
                continue;
            flag=0;
            for(int j=a[i]-1; j>=a[i]-t; j--)    //在当前鬼来的时间前1秒到前t秒中,点燃一根蜡烛
            {
                if(j<0)                         //j<0是在午夜之前点燃
                {
                    ans++;
                    for(int k=j; k<=j+t; k++)
                    {
                        if(k>0)
                            s[k]++;
                    }
                }
                else if(b[j]==0)            //当前点没有点过蜡烛,则把它点燃
                {
                    b[j]=1;                 
                    ans++;
                    for(int k=j; k<=j+t; k++)  //从点燃的点开始到+t秒,s[k]++,表明这个时间点有s[k]根蜡烛照着
                        s[k]++;
                }
                if(s[a[i]]>=r)               //能满足要求,则判断下一个
                {
                    flag=1;
                    break;
                }
            }
            if(!flag) break;                 //当前鬼不能有r跟蜡烛照着,则退出,输出-1
          }
        if(!flag) printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}


发布了608 篇原创文章 · 获赞 5971 · 访问量 93万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览