Codeforces Round # 632 (Div. 2) Brief solution

Codeforces Round #632 (Div. 2)

A: The construction method I used is: \ (n \ times m \) is an odd number and it is painted in black and white, the upper left corner is painted with \ (B \) , \ (n \ times m \) is an even number and then it is in black and white Paint, paint \ (W \) in the upper left corner, and finally paint \ (B \) in the upper left corner .

And this is too troublesome, in fact, just apply \ (B \) to the first grid directly .

int t, n, m, a[N][N]; 
int main() {
	scanf("%d", &t);
	while(t --) {
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; i ++)
			for(int j = 1; j <= m; j ++)
				a[i][j] = (i + j) & 1;
		if(n * m & 1) {
			for(int i = 1; i <= n; i ++, puts(""))
				for(int j = 1; j <= m; j ++)
					printf("%c", !a[i][j] ? 'B' : 'W');
			continue ;
		}
		a[1][1] = 1;
		for(int i = 1; i <= n; i ++, puts(""))
			for(int j = 1; j <= m; j ++)
				printf("%c", a[i][j] ? 'B' : 'W');
	}
	return 0;
}

B: The optimal operation sequence must be to change the back first and then the front. Just judge it.

int n, a[N], b[N];
int main() {
	int t; scanf("%d", &t);
	while(t --) {
		scanf("%d", &n);
		for(int i = 1; i <= n; i ++) scanf("%d", a + i);
		for(int i = 1; i <= n; i ++) scanf("%d", b + i);
		static bool big[N], small[N]; bool tag = 1;
		bool bg = 0, sm = 0;
		for(int i = 1; i <= n; i ++) {
			big[i] = bg; small[i] = sm;
			if(a[i] > 0) bg = 1;
			if(a[i] < 0) sm = 1;
		}
		for(int i = 1; i <= n; i ++) {
			if(a[i] < b[i] && !big[i]) tag = 0;
			if(a[i] > b[i] && !small[i]) tag = 0;
		}
		puts(tag ? "YES" : "NO");
	}
	return 0;
}

C: For each location \ (i \) , there will be a restriction that the interval cannot contain \ ([l [i], i] \) . Consider that the number of legal intervals ending with \ (i \) is \ (i-\ max_ {j = 1} ^ {i} l [j] \) , just sum.

int n;
ll a[N];
map<ll, ll> Map; 
int main() {
	scanf("%d", &n); ll cnt = 0, up = 0;
	for(int i = 1; i <= n; i ++) {
		scanf("%lld", a + i), a[i] += a[i - 1];
		if(Map[a[i]] || a[i] == 0) {
			up = max(up, Map[a[i]] + 1);
		}
		cnt += i - up;
		Map[a[i]] = i;
	}
	printf("%lld\n", cnt);
	return 0;
}

D: Turn the person's orientation into the 01 sequence. It is equivalent to putting a 01 sequence, each round can select several adjacent \ (10 ​​\) , and then become \ (01 \) . This is similar to the bubble sorting process. We first find a solution with the smallest number of rounds, which must be as many as possible in each round . \ (10 ​​\) . Then disassemble each round according to \ (k \) until exactly \ (k \) round. If \ (k \) is less than the minimum number of rounds or greater than the logarithm of the reverse order, there is no solution.

const int N = 3000 + 10;
int n, k, a[N], ni, pos[N * N], len, bel[N * N];
char s[N];
int main() {
	scanf("%d%d%s", &n, &k, s + 1);
	for(int i = 1; i <= n; i ++) {
		a[i] = s[i] == 'R';
		for(int j = 1; j < i; j ++)
			ni += a[j] > a[i];
	}
	if(k > ni) return puts("-1"), 0;
	int tmp = 0;
	while(1) {
		bool tag = 0; tmp ++;
		for(int i = 1; i < n; i ++) {
			if(a[i] && !a[i + 1]) {
				tag = 1;
				pos[++ len] = i;
				bel[len] = tmp;
			}
		}
		if(!tag) break ;
		for(int i = len; i >= 1 && bel[i] == tmp; i --)
			swap(a[pos[i]], a[pos[i] + 1]);
	}
	tmp --;
	if(k < tmp) return puts("-1"), 0;
	int op = tmp, x = -1; //printf("tmp = %d\n", tmp);
	for(int i = 1; i <= len && op < k; i ++) {
		if(bel[i + 1] == bel[i]) {
			bel[i] = -- x; op ++;
		}
	}
	for(int i = 1, j; i <= len; ) {
		for(j = i; j < len && bel[j + 1] == bel[i]; j ++) ;
		printf("%d", j - i + 1);
		for(int k = i; k <= j; k ++) printf(" %d", pos[k]);
		puts(""); i = j + 1;
	}
	return 0;
}

E: Goo Goo

F: For one of the optimal schemes for a certain \ (k \) element, if a non-self factor of a certain number does not appear, then the answer is better by replacing this number with its factor. Then the imperfection of a scheme is the largest factor of each number of max.

Therefore, \ (1,2, .., n \ ) is selected in order by removing the smallest prime factor (that is, sorting by non-self largest divisor). It can be found that when a number is selected, all its factors (except itself) must also be selected, then this is a legal and optimal solution.

const int N = 5e5 + 10;
int n, c[N];
bool tag[N];
int p[N], pc;
void sieve(int n) {
	c[1] = 1;
	for(int i = 2; i <= n; i ++) {
		if(!tag[i]) p[++ pc] = i, c[i] = i;
		for(int j = 1; j <= pc && i * p[j] <= n; j ++) {
			tag[i * p[j]] = 1;
			if(i % p[j] == 0) {
				c[i * p[j]] = p[j];
				break ;
			}
			c[i * p[j]] = p[j];
		}
	}
}
int main() {
	scanf("%d", &n); sieve(n);
	for(int i = 1; i <= n; i ++) c[i] = i / c[i];
	sort(c + 1, c + n + 1);
	for(int i = 2; i <= n; i ++)
		printf("%d ", c[i]);
	return 0;
}

Guess you like

Origin www.cnblogs.com/hongzy/p/12672753.html