codeforces--A - Pasha and Pixels--B - Anton and currency you all know

来源:互联网 发布:vb winhttprequest 编辑:程序博客网 时间:2024/06/06 17:33
A - Pasha and Pixels
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status

 

Appoint description: 

Description

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 i and 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 ≤ 1000, 1 ≤ 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 ≤ n, 1 ≤ 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 Input

Input
2 2 41 11 22 12 2
Output
4
Input
2 3 62 32 21 32 21 21 1
Output
5
Input
5 3 72 31 21 14 13 15 33 2
Output
0

 

题意:给出k个点,改变这k个点的颜色,如果是白的,变为黑,如果是黑那么不变。

问在第几步中会出现2*2的黑块。

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std ;int Map[1100][1100] ;struct node{    int x , y ;}c[110000];int f(int x,int y){    if( Map[x-1][y-1] && Map[x-1][y] && Map[x][y-1] && Map[x][y] )        return 1 ;    if( Map[x-1][y] && Map[x-1][y+1] && Map[x][y] && Map[x][y+1] )        return 1 ;    if( Map[x][y-1] && Map[x][y] && Map[x+1][y-1] && Map[x+1][y] )        return 1 ;    if( Map[x][y] && Map[x+1][y] && Map[x][y+1] && Map[x+1][y+1] )        return 1 ;    return 0;}int main(){    int n , m , k , i , j , num1 ;    while( scanf("%d %d %d", &n, &m, &k) != EOF )    {        memset(Map,0,sizeof(Map)) ;        for(i = 0 ; i < k ; i++)        {            scanf("%d %d", &c[i].x, &c[i].y) ;        }        for(i = 0 ; i < k ; i++)        {            Map[c[i].x][c[i].y] = 1 ;            if( f(c[i].x,c[i].y) )                break ;        }        if( i == k )            printf("0\n") ;        else            printf("%d\n", i+1) ;    }    return 0 ;}


 

B - Anton and currency you all know
Time Limit:500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status

 

Appoint description: 

Description

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 Input

Input
527
Output
572
Input
4573
Output
3574
Input
1357997531
Output
-1

 

给出一个奇数,问交换这个数中的两个数,得到一个偶数,问最大的结果是什么。

直接由低位开始,遇到偶数,判断:如果还没有可以交换的值,直接交换,如果有交换的值,只有末尾的奇数比当前位大的时候才交换。

 

#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;char str[110000] , ch ;int main(){    int i , j , l , k ;    while( scanf("%s", str) != EOF )    {        l = strlen(str) ;        k = -1 ;        for(i = l-1 ; i >= 0 ; i--)        {            if( (str[i]-'0')%2 ) continue ;            if( k == -1 )                k = i ;            else if( str[i] < str[l-1] )                k = i ;        }        if( k == -1 )            printf("%d\n", k ) ;        else        {            ch = str[l-1] ; str[l-1] = str[k] ; str[k] = ch ;            for(i = 0 ; i < l ; i++)                printf("%c", str[i]) ;            printf("\n") ;        }    }    return 0 ;}


 

 

0 0
原创粉丝点击