1. 程式人生 > >codeforces簡單思維題合集

codeforces簡單思維題合集

1030C

題意是問一個數列能否劃分為幾段使得每段的和相等(段數至少為2)

必然有一段是從1開始的

所以答案必然是n-1個字首和中的一個

O(n)列舉,O(n)檢驗

#include <bits/stdc++.h>
using namespace std;


#define LL long long
#define db double

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

template <typename T> 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 <bits/stdc++.h>
#define LL long long
#define db double
using namespace std;
 
const int MAXN = 200010;
const int INF = 0x3f3f3f3f;
 
template <typename T> 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 <typename T> inline bool CheckMax(T &a, const T &b) {
	return a < b ? a = b, true : false;
}
 
template <typename T> 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<int, int> pii;
typedef pair<pii, pii> ppp;

map<pii, vector<pii> > 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 <bits/stdc++.h>
using namespace std;

#define LL long long
#define db double

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

template <typename T> 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 <bits/stdc++.h>
using namespace std;

#define LL long long
#define db double

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

template <typename T> 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 <bits/stdc++.h>
#define LL long long
#define db double
using namespace std;

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

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

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

template <typename T> 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;
}