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

A - Pasha and Pixels

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d
& %I64u

Submit Status

Appoint description: 
System Crawler  (2015-01-28)

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 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

题意:给出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: 
System Crawler  (2015-01-28)

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 ;
}
时间: 2024-10-24 16:03:06

codeforces--A - Pasha and Pixels--B - Anton and currency you all know的相关文章

Codeforces Round #288 (Div. 2) B. Anton and currency you all know

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

Codeforces Round #288 (Div. 2) A. Pasha and Pixels

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

Codeforces Round #253 (Div. 2) A. Anton and Letters

题目很简单,只需要注意带空格的输入用getline即可 #include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> using namespace std; int main(){ string str; getline(cin,str); set<char> a; for(int i= 1 ; i < s

Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心

B. Anton and Lines The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necessar

codeforces 557B. Pasha and Tea 解题报告

题目链接:http://codeforces.com/problemset/problem/557/B 题目意思:有 2n 个茶杯,规定第 i 个茶杯最多只能装 ai 毫升的水.现在给出 w 毫升的水,需要把这w毫升(可以不用光)的水倒入到这 2n 个茶杯中,使得分给n个男的每个水杯的水恰好是n个女的2倍(注意,n个男的水杯装的水是一样的,n 个女也是).现在问的是,怎样使得2n个杯装的水最多,求出这个值. ********************************************

Codeforces Round #379 (Div. 2) C. Anton and Making Potions(二分)

Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare npotions. Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two typ

Codeforces Round #379 (Div. 2) D. Anton and Chess(模拟)

Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check

Codeforces 557B Pasha and Tea 倒茶

题意:Pasha有2n个茶杯,每个茶杯的容积不同.Pasha有2n个朋友,男女各n个.现在Pasha要将总容积为w的茶倒在2n个茶杯里,分给这2n个朋友,并规定同性别的朋友茶杯中的茶容积相同,且每个男性朋友茶杯中茶的容积是每个女性朋友茶杯中茶的容积的两倍.求所有朋友茶杯中的茶容积之和的最大值. 水题.首先按照茶杯的容积排序,较小的n个茶杯肯定是分给了女生,较大的n个茶杯分给男生.若不然,如果某个男性朋友茶杯容积比某个女性朋友茶杯容积小的话,由于男性朋友茶杯中茶的容积较大,那么互换他们的茶杯,仍然

codeforces B. Pasha and String(贪心)

题意:给定一个长度为len的字符序列,然后是n个整数,对于每一个整数ai, 将字符序列区间为[ai,len-ai+1]进行反转.求出经过n次反转之后的序列! 1 /* 2 思路1:将区间为偶数次的直接去掉!对剩下的区间进行反转.超时了,智商上的压制... 3 */ 4 #include<iostream> 5 #include<cstdio> 6 #include<algorithm> 7 #include<stack> 8 #include<cstr