Codeforces Round #288 (Div. 2) A. Pasha and Pixels
有N*M个空白格 , 操作K次、每次把(x,y)所代表的格子涂黑 , 问你最少通过几次操作能画出一个2*2的方框
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int Map[1005][1005] ;
int n , m , k ;
bool check(int x , int y)
{
if(Map[x+1][y] && Map[x][y+1] && Map[x+1][y+1])
return true;
if(Map[x-1][y] && Map[x][y+1] && Map[x-1][y+1])
return true;
if(Map[x-1][y] && Map[x][y-1] && Map[x-1][y-1])
return true;
if(Map[x+1][y] && Map[x][y-1] && Map[x+1][y-1])
return true;
return false;
}
int main()
{
int x , y;
while(cin >> n >> m >> k)
{
int ans = 0;
memset(Map, 0 ,sizeof(Map));
for(int i = 1 ; i <= k ; i++)
{
cin >> x >> y;
Map[x][y] = 1;
if(check(x,y) && ans == 0)
{
ans = i ;
}
}
cout << ans << endl;
}
return 0;
}
Codeforces Round #288 (Div. 2) B. Anton and currency you all know
题目给出一个数字、让你在这个数字中找到一个偶数和最后一个数交换形成新的数、问你这个数最大值是多少
前后FOR一遍就OK
比赛的时候太贪心了直接枚举结果在重判时TLE了。。
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
string str;
cin >> str;
int len = str.length();
for(int i = 0 ; i < len ; i++)
{
if(str[i] % 2 == 0 && str[len - 1] > str[i])
{
swap(str[i],str[len-1]);
cout << str << endl;
return 0;
}
}
for(int i = len - 2 ; i >= 0 ; i--)
{
if(str[i] % 2 == 0)
{
swap(str[i],str[len-1]);
cout << str << endl;
return 0;
}
}
cout << "-1" << endl;
return 0;
}
C. Anya and Ghosts
第一行输入 鬼的总数 蜡烛燃烧持续时间 每个鬼来访时所需要点亮的蜡烛数目
问你最少需要多少蜡烛能让Anya度过这个夜晚
郁闷了比赛的时候写个点蜡烛函数然后在27组WA了 后来写到main里面AC不知道问题出在哪里
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
int m , t , r , ghost[305] , light[700] , vis[700];
while(cin >> m >> t >> r)
{
for(int i = 0 ; i < m ; i++)
{
cin >> ghost[i];
ghost[i] += 320;
}
if(r > t)
cout << "-1" << endl;
else
{
sort(ghost , ghost + m);
memset(light,0,sizeof(light));
memset(vis,0,sizeof(vis));
int cnt = 0;
for(int i = 0 ; i < m ; i++)
{
while( light[ ghost[i] ] < r)
{
cnt++;
int pos = ghost[i];
while(vis[--pos] !=0);
vis[pos] = 1;
for(int i = 1 ; i <= t ;i++)
light[pos + i]++;
}
}
cout << cnt << endl;
}
}
return 0;
}
以下是WA的代码谁能帮帮我解决这为什么WA
</pre><pre name="code" class="cpp">#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int m , t , r , ghost[305] , light[700] , vis[700];
void SetR(int pos)
{
while(vis[--pos] !=0);
vis[pos] = 1;
for(int i = 1 ; i <= t ;i++)
light[pos + i]++;
}
int main()
{
while(cin >> m >> t >> r)
{
for(int i = 0 ; i < m ; i++)
{
cin >> ghost[i];
ghost[i] += 320;
}
if(r > t)
cout << "-1" << endl;
else
{
memset(light,0,sizeof(light));
memset(vis,0,sizeof(vis));
int cnt = 0;
for(int i = 0 ; i < m ; i++)
{
while( light[ ghost[i] ] < r)
{
cnt++;
SetR(ghost[i]);
}
}
cout << cnt << endl;
}
}
return 0;
}