Codeforces Round #617 (Div. 3) A Array with Odd Sum B Food Buying C Yet Another Walking Robot

A

#include <cstdio>
int t, n, num;
int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n); int odd = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &num);
			if (num & 1) odd++; 
		} 
		if (odd == 0 || (!(odd & 1) && odd == n)) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
} 

B

#include <cstdio>
int t, n;
int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		int ans = n;
		while (n >= 10) {
			int t = n / 10;
			ans += t;
			n += t - t * 10;
		}
		printf("%d\n", ans);
	}
	return 0;
} 

C

  • 有mp记录每个位置是否被访问过。 如果访问过代表走了个环。 求出走的路径长度。
#include <cstdio>
#include <algorithm>
#include <map>
#define mk(x,y) make_pair(x,y)
using namespace std;
const int N = 2e5 + 5;
char s[N];
int t, n;
int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d%s", &n, s + 1);
		map<pair<int, int>, int> mp;
		mp[mk(0, 0)] = 1; //作为起点 
		int x = 0, y = 0, l, r, minv = 1e9; 
		for (int i = 1; i <= n; i++) {
			if (s[i] == 'L') x--; 
			else if (s[i] == 'R') x++;  
			else if (s[i] == 'U') y++;
			else if (s[i] == 'D') y--;
			pair<int, int> t = mk(x, y);
			if (mp.count(t)) {
				//取个最小值 
			 	int d = i - mp[t];
			 	if (d < minv) {
			 		minv = d; r = i, l = mp[t];
				}
			}
			mp[mk(x, y)] = i + 1; //及时更新位置 
		} 
		if (minv == 1e9) printf("-1\n");
		else printf("%d %d\n", l, r);
	}
	return 0;
} 
发布了456 篇原创文章 · 获赞 466 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_41280600/article/details/104373048