codeforces简单思维题合集

1030C

题意是问一个数列能否划分为几段使得每段的和相等(段数至少为2)

必然有一段是从1开始的

所以答案必然是n-1个前缀和中的一个

O(n)枚举,O(n)检验

#include 
using namespace std;


#define LL long long
#define db double

const int MAXN = 200;
const int LIM = 101;
const int INF = 0x3f3f3f3f;

template  inline void read(T &x) {
    int ch = getchar();
    bool fg = false;
    for (x = 0; !isdigit(ch); ch = getchar()) {
        if (ch == '-') {
            fg = true;
        }
    }
    for (; isdigit(ch); ch = getchar()) {
        x = x * 10 + ch - '0';
    }
    if (fg) {
        x = -x;
    }
}

int n, a[MAXN], sum[MAXN];
char s[MAXN];

bool check(int x, int k) {
	int cur = k, tot = 1;
	for(int i = k + 1; i <= n; i++) {
		if(sum[i] - sum[cur] == x) {
			cur = i, tot++;
			while(sum[i] == sum[i + 1]) i++, cur++;
		}
		else while(sum[i + 1] == sum[i]) i++;
	}
	if(sum[n] - sum[cur] != x && cur != n) return 0;
	if(tot > 1) return 1;
	return 0;
}

signed main() {
	read(n);
	scanf("%s", s + 1);
	if(n == 1) {
		puts("NO");
		return 0;
	}
	for(int i = 1; i <= n; i++) a[i] = (int) (s[i] - '0');
	for(int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
	for(int i = 1; i <= n; i++) {
		if(check(sum[i], i)) {
			puts("YES");
			return 0;
		}
	}
	puts("NO");
	return 0;
}

1031B

第一眼以为不可做

然鹅a,b的取值只有0,1,2,3,完全可以枚举t1然后递推检验

#include 
#define LL long long
#define db double
using namespace std;
 
const int MAXN = 200010;
const int INF = 0x3f3f3f3f;
 
template  inline void read(T &x) {
	char c = getchar();
	bool f = false;
	for (x = 0; !isdigit(c); c = getchar()) {
		if (c == '-') {
			f = true;
		}
	}
	for (; isdigit(c); c = getchar()) {
		x = x * 10 + c - '0';
	}
	if (f) {
		x = -x;
	}
}
 
template  inline bool CheckMax(T &a, const T &b) {
	return a < b ? a = b, true : false;
}
 
template  inline bool CheckMin(T &a, const T &b) {
	return a > b ? a = b, true : false;
}
 
int n;
int a[MAXN], b[MAXN], t[MAXN];
int cnt;

#define ls lower_bound
#define us upper_bound

typedef pair pii;
typedef pair ppp;

map > f;

bool check(int T, int x) {
	if(x == n) {
		t[n] = T;
		return 1;
	}
	t[x] = T;
	for(int j = 0; j <= 3; j++) {
		if((t[x] | j) == a[x] && (t[x] & j) == b[x]) {
			return check(j, x + 1);
		}
	}
	return 0;
}

signed main() {
	read(n);
	for(int i = 1; i < n; i++) read(a[i]);
	for(int i = 1; i < n; i++) read(b[i]);
	for(int i = 1; i < n; i++) {
		if(a[i] < b[i]) {
			puts("NO");
			return 0;
		}
	}
	for(int i = 0; i <= 3; i++) {
		if(check(i, 1)) {
			puts("YES");
			for(int i = 1; i <= n; i++) printf("%d%c", t[i], i == n ? '\n' : ' ');
			return 0;
		}
	}
	puts("NO");
	return 0; 
}

1036D

给你两个数列,问能不能分别划分使得对应段的和相等

搞两个指针,戳戳戳就ojbk了

#include 
using namespace std;

#define LL long long
#define db double

const int MAXN = 300300;
const int MAXE = 400400;
const int INF = 0x3f3f3f3f;

template  inline void read(T &x) {
    int ch = getchar();
    bool fg = false;
    for (x = 0; !isdigit(ch); ch = getchar()) {
        if (ch == '-') {
            fg = true;
        }
    }
    for (; isdigit(ch); ch = getchar()) {
        x = x * 10 + ch - '0';
    }
    if (fg) {
        x = -x;
    }
}

LL a[MAXN], b[MAXN];
int n, m;

signed main() {
	read(n);
	for(int i = 1; i <= n; i++) read(a[i]), a[i] += a[i - 1];
	read(m);
	for(int i = 1; i <= m; i++) read(b[i]), b[i] += b[i - 1];
	if(a[n] != b[m]) puts("-1");
	else {
		int i = 1, j = 1, ans = 0;
		while(i <= n && j <= m) {
			while(a[i] < b[j] && i <= n) i++;
			while(a[i] > b[j] && j <= m) j++;
			if(a[i] == b[j] && i <= n && j <= m) {
				i++, ans++;
			}
		}
		printf("%d\n", ans);
	} 
	return 0;
} 

1036B

蜜汁找规律。。。

#include 
using namespace std;

#define LL long long
#define db double

const int MAXN = 200200;
const int LIM = 1000000;
const int INF = 0x3f3f3f3f;

template  inline void read(T &x) {
    int ch = getchar();
    bool fg = false;
    for (x = 0; !isdigit(ch); ch = getchar()) {
        if (ch == '-') {
            fg = true;
        }
    }
    for (; isdigit(ch); ch = getchar()) {
        x = x * 10 + ch - '0';
    }
    if (fg) {
        x = -x;
    }
}

signed main() {
	LL n, m, k, Q;
	read(Q);
	while(Q --) {
		read(n), read(m), read(k);
		if(max(n, m) > k) puts("-1");
		else {
			if((n + m) & 1) printf("%I64d\n", k - 1);
			else if((k + m) & 1) printf("%I64d\n", k - 2);
			else printf("%I64d\n", k);
		}
	}
	return 0;
} 

1060C

两个sigma并没有联系,所以可以单独考虑a和b,不必算出c

然后维护一下最大值就好了

#include 
#define LL long long
#define db double
using namespace std;

const int MAXN = 2002;
const int MAXE = 400400;
const int INF = 0x3f3f3f3f;

template inline void CheckMax(T &A, T B) {
	A < B ? A = B : A;
}

template inline void CheckMin(T &A, T B) {
	A > B ? A = B : A;
}

template  inline void read(T &x) {
    int c = getchar();
    bool f = false;
    for (x = 0; !isdigit(c); c = getchar()) {
        if (c == '-') {
            f = true;
        }
    }
    for (; isdigit(c); c = getchar()) {
        x = x * 10 + c - '0';
    }
    if (f) {
        x = -x;
    }
}

int n, m;
LL a[MAXN], b[MAXN], X;
LL asum[MAXN], bsum[MAXN];

signed main() {
	read(n), read(m);
	for(int i = 1; i <= n; i++) read(a[i]);
	for(int i = 1; i <= m; i++) read(b[i]);
	read(X);
	for(int i = 1; i <= n; i++) a[i] += a[i - 1];
	for(int j = 1; j <= m; j++) b[j] += b[j - 1];
	memset(asum, 0x7f, sizeof(asum));
	memset(bsum, 0x7f, sizeof(bsum));
	for(int len = 1; len <= n || len <= m; len++) {
		if(len <= n) {
			for(int i = 1; i <= n - len + 1; i++) {
				CheckMin(asum[len], a[i + len - 1] - a[i - 1]);
			}
		}
		if(len <= m) {
			for(int i = 1; i <= m - len + 1; i++) {
				CheckMin(bsum[len], b[i + len - 1] - b[i - 1]);
			}
		}
	}
	int ans = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(asum[i] * bsum[j] <= X)
				CheckMax(ans, i * j);
		}
	}
	printf("%d\n", ans);
	return 0;
}

 

你可能感兴趣的:(简单题,codeforces)