Codeforces Round #369 (Div. 2)
qer 弱的可啪……
A
—————————————————————————————
题意:输入一辆车的座位占用情况, 中间表示走廊, 走廊两边分别2个座位,当这2个座位都为 ‘O’ 时表示空,求连续的2个空座,即‘OO’;
题解:暴力找到‘OO’, 换成‘++’, 输出;
(为什么这次码风这么奇怪啊 QAQQQQQQQQQQQQQQQQQQQQQ)
代码:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1000 + 10;
int n;
char z[MAXN][10];
inline void print()
{
puts("YES");
for(int i = 1; i <= n; i ++)
printf("%s\n",z[i]);
return;
}
inline bool Judge(int i, int j)
{
if(z[i][j] == z[i][j+1] && z[i][j] == 'O')
{
z[i][j] = z[i][j+1] = '+';
print();
return 1;
}
return 0;
}
int main()
{
char c;
cin >> n;
for(int i = 1; i <= n; i ++)
scanf("%s",z[i]);
for(int i = 1; i <= n; i ++)
{
if(Judge(i,0))
return 0;
if(Judge(i,3))
return 0;
}
puts("NO");
return 0;
}
B
—————————————————————————————
题意:就是一个n*n的方阵,有一个地方是空的‘0’, 让你在这个位置填上一个数,使得对角线、每行、每列的和都相等。
题解:如果可行,那么 每行和中肯定有一行 与其他的行和不同, 有 n-1行的和是相同的。同理 列。那么这个行差(列差) 就是 答案。然后判断一下填上这个答案之后 对角线是否符合要求,如果不符合 那这个答案不合法,输出-1;
坑点: long long 注意溢出; 行(列)和中 只能有 1 行与其他不同; n == 1 时 特判; 答案不能是 0;
(我不会告诉你们我蒟蒻全犯了)
代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 500 + 5;
int n;
LL maps[MAXN][MAXN];
LL R[MAXN], C[MAXN], Dia[2];
void J_R()
{
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
R[i] += maps[i][j];
}
void J_C()
{
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
C[i] += maps[j][i];
}
void J_Z()
{
for(int i = 1; i <= n; i ++)
Dia[0] += maps[i][i];
}
void J_F()
{
for(int i = n, j = 1; i >= 1, j <= n; i --, j ++)
Dia[1] += maps[i][j];
}
int main()
{
int x = 0, y = 0;
scanf("%d",&n);
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
scanf("%d",&maps[i][j]);
if(!maps[i][j] && !x && !y){
x = i; y = j;
}
}
}
if(n == 1 && !maps[n][n])
{
puts("1");
return 0;
}
J_R();J_C();
LL RC = 0, CC = 0;
sort(R+1,R+n+1);
sort(C+1,C+n+1);
RC = abs(R[2] - R[1]);
CC = abs(C[2] - C[1]);
bool weiyi = true;
if(R[3] && R[3] != R[2])
weiyi = false;
if(C[3] && C[3] != C[2])
weiyi = false;
if(RC == CC && RC && weiyi)
{
maps[x][y] = RC;
J_Z();J_F();
if(Dia[0] == Dia[1] && Dia[0] == R[n] && R[n] == C[n])
{
printf("%I64d\n",RC);
return 0;
}
else puts("-1");
}
else puts("-1");
return 0;
}
几个测试数据:
/*
Input
7
250000000 1000000000 1000000000 250000000 1000000000 1000000000 250000000
1000000000 250000000 1000000000 250000000 1000000000 250000000 1000000000
1000000000 1000000000 250000000 250000000 250000000 1000000000 1000000000
250000000 250000000 250000000 0 250000000 250000000 250000000
1000000000 1000000000 250000000 250000000 250000000 1000000000 1000000000
1000000000 250000000 1000000000 250000000 1000000000 250000000 1000000000
250000000 1000000000 1000000000 250000000 1000000000 1000000000 250000000
Answer
3250000000
*/
/*
Input
10
92 67 99 74 1 51 8 58 15 40
17 42 24 49 0 26 83 33 90 65
98 73 80 55 7 57 14 64 16 41
23 48 5 30 82 32 89 39 91 66
4 54 81 56 88 63 20 70 22 47
79 29 6 31 13 38 95 45 97 72
85 60 87 62 19 69 21 71 3 28
10 35 12 37 94 44 96 46 78 53
86 61 93 68 25 75 2 52 9 34
11 36 18 43 100 50 77 27 84 59
Answer
76
*/
以上是 2016-08-30 14:04
2016-10-19 考试考到了这个题,然而只拿了90没判n==1 的情况, 但是感觉思路比上面清晰了许多。
以下是AC代码:
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const LL MAXN = 500 + 5;
LL n, zx = 0, cx = 0;
LL maps[MAXN][MAXN];
LL r[MAXN], c[MAXN];
void Get_r()
{
for(LL i = 1; i <= n; i ++)
for(LL j = 1; j <= n; j ++)
r[i] += maps[i][j];
}
void Get_c()
{
for(LL i = 1; i <= n; i ++)
for(LL j = 1; j <= n; j ++)
c[j] += maps[i][j];
}
void Get_zx()
{
for(LL i = 1; i <= n; i ++)
zx += maps[i][i];
}
void Get_cx()
{
for(LL i = 1, j = n; i <= n, j >= 1; i ++, j --)
cx += maps[i][j];
}
int main()
{
cin >> n;
memset(r,0,sizeof(r)); memset(c,0,sizeof(c));
for(LL i = 1; i <= n; i ++)
for(LL j = 1; j <= n; j ++)
scanf("%lld", &maps[i][j]);
Get_r(); Get_c(); Get_zx(); Get_cx();
bool Bigflag = 0;
LL h = 0;
LL ans = 0, ans1 = 0, ans2 = 0, ans3 = 0;
// 行
LL i = 1;
while(i < n)
{
if(r[i] != r[i+1])
{
if(h < 2)
{
ans1 = abs(r[i+1]-r[i]);
h ++;
}
else Bigflag = 1;
}
i ++;
}
if(Bigflag)
{
puts("-1");
return 0;
}
// 列
i = 1;
h = 0;
while(i < n)
{
if(c[i] != c[i+1])
{
if(h < 2)
{
ans2 = abs(c[i+1]-c[i]);
h ++;
}
else Bigflag = 1;
}
i ++;
}
if(Bigflag)
{
puts("-1");
return 0;
}
ans3 = abs(zx-cx);
// cout << ans1 << " " << ans2 << " " << ans3 << endl;
if(ans3)
{
if(ans1 == ans2 && ans2 == ans3)
{
if(!ans1)
puts("1");
else cout << ans1 << endl;
}
else puts("-1");
}
else
{
if(ans1 == ans2)
{
if(!ans1)
puts("1");
else cout << ans1 << endl;
}
else puts("-1");
}
return 0;
}
/*
5
1067 1066 1066 1066 1067
1066 1067 1066 1067 1066
1066 1066 1067 1066 1066
1066 1067 1066 1067 1066
1067 1066 1066 1066 0
*/