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.
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.
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.
2 2 4 1 1 1 2 2 1 2 2
4
2 3 6 2 3 2 2 1 3 2 2 1 2 1 1
5
5 3 7 2 3 1 2 1 1 4 1 3 1 5 3 3 2
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;
}
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!
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.
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.
527
572
4573
3574
1357997531
-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;
}
需要注意的是蜡烛点燃的时间为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;
}