# Codeforces Round #369 (Div. 2)

A. Bus to Udayland

#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
const int qq = 1e5+10;
string str[qq];
int main(){
int n;scanf("%d",&n);
for(int i=0; i<n; ++i)	cin >> str[i];
int flag = 0;
for(int i=0; i<n; ++i){
if(str[i][0]=='O'&&str[i][1]=='O'){
flag = 1;
str[i][0] = str[i][1]='+';
break;
}
else if(str[i][3]=='O'&&str[i][4]=='O'){
flag = 1;
str[i][4] = str[i][3]='+';
break;
}
}
if(!flag)	printf("NO\n");
else{
printf("YES\n");
for(int i=0; i<n; ++i)	cout << str[i] << endl;
}
return 0;
}

B. Chris and Magic Square

（注意这题0的位置一定要填上正整数）

#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
using namespace std;
typedef __int64 ll;
const int qq = 505;
ll mar[qq][qq];
ll r[qq],c[qq];
ll zero[5];
int main(){
int n;scanf("%d",&n);
if(n==1){
printf("1\n");
return 0;
}
int tx,ty;
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j){
scanf("%I64d",&mar[i][j]);
if(mar[i][j]==0)	tx=i,ty=j;
}
int flag = 1;
ll tmp;
for(int i=1; i<=n; ++i)
if(i!=tx){
ll sum = 0;
for(int j=1; j<=n; ++j)
sum+=mar[i][j];
r[i] = sum;
tmp = r[i];
}
for(int i=1; i<=n; ++i)
if(i!=tx&&r[i]!=tmp)	flag = 0;
for(int j=1; j<=n; ++j)
if(j!=ty){
ll sum = 0;
for(int i=1; i<=n; ++i)
sum+=mar[i][j];
c[j] = sum;
}
for(int i=1; i<=n; ++i)
if(i!=ty&&c[i]!=tmp)	flag = 0;
ll u;
if(ty!=tx){
u = 0;
for(int i=1; i<=n; ++i)
u+=mar[i][i];
if(u!=tmp)	flag = 0;
}
if(tx!=n-ty+1){
u = 0;
for(int i=1; i<=n; ++i)
u+=mar[i][n-i+1];
if(u!=tmp)	flag = 0;
}
if(!flag){
printf("-1\n");
return 0;
}
for(int j=1; j<=n; ++j)
zero[0]+=mar[tx][j];
for(int i=1; i<=n; ++i)
zero[1]+=mar[i][ty];
int k=2;
if(tx==ty){
for(int i=1; i<=n; ++i)
zero[k]+=mar[i][i];
k++;
}
if(tx+ty==n+1){
for(int i=1; i<=n; ++i)
zero[k]+=mar[i][n+1-i];
k++;
}
ll d = tmp-zero[0];
if(d<=0){
printf("-1\n");
return 0;
}
for(int i=1; i<k; ++i){
if(d!=tmp-zero[i])	flag = 0;
}
if(!flag)	printf("-1\n");
else	printf("%I64d\n", d);
return 0;
}

C. Coloring Trees

#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
using namespace std;
typedef __int64 ll;
const int qq = 100+5;
const ll MAX = 1e16 + 10;
int tree[qq];
int color[qq][qq];
ll dp[qq][qq][qq];	//dp[i][j][k]´ú±íÇ°i¿ÃÊ÷ÃÀÀöÖµÎªjÇÒµÚi¿ÃÊ÷ÑÕÉ«ÎªkµÄÊ±ºòµÄ×îÐ¡energy¡¢
int main(){
int n,m,k;scanf("%d%d%d",&n,&m,&k);
for(int i=1; i<=n; ++i)	scanf("%d",&tree[i]);
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j)
scanf("%d",&color[i][j]);
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
for(int l=1; l<=m; ++l)
dp[i][j][l] = MAX;
if(tree[1])	dp[1][1][tree[1]] = 0;
else	for(int i=1; i<=m; ++i)	dp[1][1][i]=color[1][i];
for(int i=2; i<=n; ++i)
if(!tree[i]){
for(int j=1; j<i; ++j)
for(int l=1; l<=m; ++l)
for(int c=1; c<=m; ++c)
if(l!=c)	dp[i][j+1][c]=min(dp[i][j+1][c], dp[i-1][j][l]+color[i][c]);
else	dp[i][j][c]=min(dp[i][j][c], dp[i-1][j][l]+color[i][c]);
}
else{
for(int j=1; j<i; ++j)
for(int l=1; l<=m; ++l)
if(l!=tree[i])	dp[i][j+1][tree[i]]=min(dp[i][j+1][tree[i]], dp[i-1][j][l]);
else	dp[i][j][tree[i]]=min(dp[i][j][tree[i]], dp[i-1][j][l]);
}
ll ans = MAX;
for(int i=1; i<=m; ++i)
ans=min(ans, dp[n][k][i]);
if(ans==MAX)	ans = -1;
printf("%I64d\n",ans);
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：Codeforces Round #369 (Div. 2) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)