2
9
2017
0

CF Round #394

A.

#include <stdio.h>
#include <iostream>
using std::swap;
int main() {
	int a , b;
	scanf("%d%d",&a,&b);
	if(a < b) swap(a,b);
	if(a-b <= 1 && !(a == 0 && b == 0)) puts("YES");
	else puts("NO");
}

B.

 

#include <stdio.h>
const int Maxn = 110;
int a[Maxn] , b[Maxn] , n , L;
int main() {
	scanf("%d%d",&n,&L);
	for(int i=1; i<=n; ++i) {
		scanf("%d",&a[i]);
		a[i+n] = a[i]+L;
	}
	for(int i=1; i<=n; ++i) {
		scanf("%d",&b[i]);
		b[i+n] = b[i]+L;
	}
	for(int i=1; i<=n; ++i) {
		for(int j=1; j<=n; ++j) {
			int cnt = 0;
			for(int k=0; k<n; ++k)
				if((a[i+k] - a[i]) == (b[j+k] - b[j]))
					++cnt;
			if(cnt == n) {
				puts("YES");
				return 0;
			}
		}
	}puts("NO");
}

C.

 

#include <stdio.h>
#define inf 233333333
inline int F() {  register int aa , bb , ch;
	while(ch = getchar() , (ch<'0'||ch>'9') && ch != '-'); ch == '-' ? aa=bb=0 : (aa=ch-'0',bb=1);
	while(ch = getchar() , ch>='0'&&ch<='9') aa = aa*10 + ch-'0'; return bb ? aa : -aa;
}
const int Maxn = 51;
const int Maxm = 51;
int n , m , d[Maxn][3] , mn[3][3];
char a[Maxn][Maxn];
inline bool isdigit(char x) { return x>='0'&&x<='9'; }
int main() {
	n = F() , m = F();
	for(int i=1; i<=n; ++i)
		scanf("%s",a[i]+1);
	for(int i=1; i<=n; ++i) {
		d[i][0] = d[i][1] = d[i][2] = inf;
		for(int j=1; j<=m; ++j) {
			if(isdigit(a[i][j])) {
				if(d[i][0] > j-1) d[i][0] = j-1;
				if(d[i][0] > m-j+1) d[i][0] = m-j+1;
			}
			else if(a[i][j] == '#' || a[i][j] == '*' || a[i][j] == '&') {
				if(d[i][1] > j-1) d[i][1] = j-1;
				if(d[i][1] > m-j+1) d[i][1] = m-j+1;
			}
			else {
				if(d[i][2] > j-1) d[i][2] = j-1;
				if(d[i][2] > m-j+1) d[i][2] = m-j+1;
			}
		}
	}
	d[0][0] = d[0][1] = d[0][2] = inf;
	for(int i=0; i<3; ++i) {
		for(int j=1; j<=n; ++j) {
			if(d[mn[i][2]][i] > d[j][i]) {
				if(d[mn[i][1]][i] > d[j][i]) {
					if(d[mn[i][0]][i] > d[j][i]) {
						mn[i][2] = mn[i][1];
						mn[i][1] = mn[i][0];
						mn[i][0] = j;
					}
					else {
						mn[i][2] = mn[i][1];
						mn[i][1] = j;
					}
				}
				else mn[i][2] = j;
			}
		}
	}
	int ans = inf;
	for(int i=0; i<3; ++i)
		for(int j=0; j<3; ++j)
			for(int k=0; k<3; ++k) {
				int a = mn[0][i] , b = mn[1][j] , c = mn[2][k];
				if(a != b && b != c && c != a) {
					if(ans > d[a][0] + d[b][1] + d[c][2]) ans = d[a][0] + d[b][1] + d[c][2];
				}
			}
	printf("%d\n",ans );
}

D.

 

#include <stdio.h>
#include <algorithm>
inline int F() {  register int aa , bb , ch;
	while(ch = getchar() , (ch<'0'||ch>'9') && ch != '-'); ch == '-' ? aa=bb=0 : (aa=ch-'0',bb=1);
	while(ch = getchar() , ch>='0'&&ch<='9') aa = aa*10 + ch-'0'; return bb ? aa : -aa;
}
const int Maxn = 100010;
struct node { int a , b , c , p , id; } a[Maxn];
inline bool cmpp(const node&a , const node&b) { return a.p < b.p; }
inline bool cmpi(const node&a , const node&b) { return a.id < b.id; }
int n , l , r;
int main() {
	n = F() , l = F() , r = F();
	for(int i=1; i<=n; ++i)
		a[i].a = F() , a[i].id = i;
	for(int j=1; j<=n; ++j)
		a[j].p = F();
	std::sort(a+1,a+n+1,cmpp);
	a[1].b = l; a[1].c = l - a[1].a;
	for(int i=2; i<=n; ++i) {
		a[i].c = a[i-1].c + 1;
		a[i].b = a[i].c + a[i].a;
		if(a[i].b < l) a[i].b = l , a[i].c = a[i].b - a[i].a;
		if(a[i].b > r) { puts("-1"); return 0; }
	}
	std::sort(a+1,a+n+1,cmpi);
	for(int i=1; i<=n; ++i)
		printf("%d ",a[i].b );
}

E.

 

#include <stdio.h>
inline int F() { register int aa , bb , ch;
	while(ch = getchar() , (ch<'0'||ch>'9') && ch != '-'); ch == '-' ? aa=bb=0 : (aa=ch-'0',bb=1);
	while(ch = getchar() , ch>='0'&&ch<='9') aa = aa*10 + ch-'0'; return bb ? aa : -aa;
}
const int Maxn = 31;
const int Maxm = 60;
const long long inf = 1e18;
const int sx[4] = {1,-1,0,0};
const int sy[4] = {0,0,1,-1};
int n , to[Maxm] , next[Maxm] , g[Maxn] , du[Maxn] , ecnt = 0;
long long  x[Maxn] , y[Maxn]; bool vis[Maxn];
inline void ins(int a , int b) { to[++ecnt] = b; next[ecnt] = g[a]; g[a] = ecnt; ++du[b]; }
void dfs(int now , int lst , long long len) {
	vis[now] = 1;
	for(int i=0; i<4; ++i) {
		if(i == (lst^1)) continue;
		for(int j=g[now]; j; j=next[j]) {
			if(!vis[to[j]]) {
				x[to[j]] = x[now] + sx[i]*len;
				y[to[j]] = y[now] + sy[i]*len;
				dfs(to[j],i,len/2);
				break;
			}
		}
	}
}
int main() {
	n = F();
	for(int i=1; i<n; ++i) {
		int a = F() , b = F();
		ins(a,b); ins(b,a);
	}
	for(int i=1; i<=n; ++i) {
		if(du[i] > 4) {
			puts("NO");
			return 0;
		}
	}
	puts("YES");
	x[1] = inf/2; y[1] = inf/2;
	dfs(1,5,inf/4);
	for(int i=1; i<=n; ++i) {
		printf("%I64d %I64d\n",x[i],y[i]);
	}
}

F.

/*
首先预处理ij位置放c对答案的贡献
然后预处理原图每个点对答案的贡献并且前缀和
然后我们对每个颜色前缀和
然后每张图就是 原图总答案-原图前缀和中一个矩形+颜色前缀和中一个矩形
最后取个最小的
我懒的搞longlong什么的了,应该能过?但是空间400+mb有点恐怖,很多步其实可以放在一个for里面完成。懒的改了还得写下一题QAQ
*/
#include <stdio.h>
inline int F() { register int aa , bb , ch;
	while(ch = getchar() , (ch<'0'||ch>'9') && ch != '-'); ch == '-' ? aa=bb=0 : (aa=ch-'0',bb=1);
	while(ch = getchar() , ch>='0'&&ch<='9') aa = aa*10 + ch-'0'; return bb ? aa : -aa;
}
const int Maxn = 1005;
const int Maxk = 300005;
struct Q { int x1 , y1 , x2 , y2 , f; } q[Maxk];
int n , m , k , col[26][Maxn][Maxn] , ans[26][Maxn][Maxn] , s[2][26];
long long p1[Maxn][Maxn] , p2[26][Maxn][Maxn];
char a[Maxn][Maxn] , ch;
int main() {
	n = F() , m = F() , k = F();
	for(int i=1; i<=n; ++i) scanf("%s",a[i]+1);
	//读入原图
	for(int i=1; i<=k; ++i) {
		q[i].x1 = F() , q[i].y1 = F() , q[i].x2 = F() , q[i].y2 = F();
		while(ch = getchar() , ch<'a'||ch>'z'); int f = ch - 'a'; q[i].f = f;
		int x1 = q[i].x1 , y1 = q[i].y1 , x2 = q[i].x2+1 , y2 = q[i].y2+1;
		++col[f][x1][y1]; ++col[f][x2][y2];
		--col[f][x1][y2]; --col[f][x2][y1];
	}
	for(int f=0; f<26; ++f) {
		for(int i=1; i<=n; ++i) {
			for(int j=1; j<=m; ++j) {
				col[f][i][j] += col[f][i-1][j] + col[f][i][j-1] - col[f][i-1][j-1];
			}
		}
	}
	//打上标记,前缀和后 col[f][i][j] 便是ij这个位置 f 颜色的个数。o
	for(int i=1; i<=n; ++i)
		for(int j=1; j<=m; ++j) {
			int sum = 0 , cnt = 0;
			for(int f=0; f<26; ++f) {
				sum += cnt;
				cnt += col[f][i][j];
			}
			col[a[i][j]-'a'][i][j] += k-cnt;
			sum += ('z'-a[i][j]) * (k-cnt); cnt = 0;
			for(int f=25; f>=0; --f) {
				ans[f][i][j] = sum;
				cnt += col[f][i][j];
				sum += cnt; sum -= k-cnt;
			}
		}
	//ans[f][i][j]表示ij这个位置放f对答案的贡献
	for(int i=1; i<=n; ++i)
		for(int j=1; j<=m; ++j) {
			p1[i][j] = p1[i-1][j] + p1[i][j-1] - p1[i-1][j-1] + ans[a[i][j]-'a'][i][j];
		}
	//得到 原图对答案的贡献
	for(int f=0; f<26; ++f) {
		for(int i=1; i<=n; ++i) {
			for(int j=1; j<=n; ++j) {
				p2[f][i][j] = p2[f][i-1][j] + p2[f][i][j-1] - p2[f][i-1][j-1] + ans[f][i][j];
			}
		}
	}
	//得到 各个颜色对答案的贡献
	long long mn = 1e16;
	for(int i=1; i<=k; ++i) {
		int x1 = q[i].x1-1 , x2 = q[i].x2 , y1 = q[i].y1-1 , y2 = q[i].y2 , f = q[i].f;
		long long tmp = (p1[n][m] - p1[x2][y2] + p1[x1][y2] + p1[x2][y1] - p1[x1][y1]) + (p2[f][x2][y2] - p2[f][x1][y2] - p2[f][x2][y1] + p2[f][x1][y1]);
		if(tmp < mn) mn = tmp;
	}printf("%lld\n",mn);
	//统计k张图中每张图的答案
}
Category: Codeforces | Tags: | Read Count: 941

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com