Codeforces Round #630 (Div. 2) (ABC)

Codeforces Round #630 (Div. 2)ABC

A Exercising Walk

The
meaning of the water question is to give you a starting point (x, y). The four numbers indicate the number of steps in the four directions of left, right, down and up respectively. Then, give you a range (x1 <= x <= x2, y1 <=y <= y2) It must be satisfied during the movement, and the movement sequence can be arbitrary.
Pit point -Caiji's own wa is gone-
and most questions move in a different direction.
Left movement is x-1,
right movement is x+1,
down movement is y-1, and
up movement is y+1

#include <cstdio>
#include <cstring>

int T, le, ri, down, up;
int x, y, x1, y1, x2, y2;

int main(){
    
    
	scanf("%d",&T);
	while(T--){
    
    
		scanf("%d%d%d%d", &le, &ri, &down, &up);
		scanf("%d%d%d%d%d%d", &x, &y, &x1, &y1, &x2, &y2);
		if(le && ri && le == ri){
    
    
			if(x - 1 < x1 && x + 1 > x2){
    
    
				printf("No\n");
				continue;
			}
		}
		if(down && up && down == up){
    
    
			if(y - 1 < y1 && y + 1 > y2){
    
    
				printf("No\n");
				continue;
			}
		}
		x += ri - le;
		y += up - down;
		if(x >= x1 && x <= x2 && y >= y1 && y <= y2){
    
    
			printf("Yes\n");
		} else {
    
    
			printf("No\n");
		}
	}
	return 0;
} 

B. Composite Coloring

Water problem

That Italy feels the main topic is more difficult to understand English dish of chicken myself
to the effect that the sample group T, n give you a number, and then find their prime factors, they are the same digital color (same output ), change to a new color if it is different (change to a new number).
However, the output numbers must be greater than or equal to 1, and less than or equal to 11. (The title has proved that the number of colors is within 11, so it is correct to start from 1 and increase upwards).
Idea
First, list all the prime numbers that may be used according to the data range and store them in the prime array.
Then traverse each number, find its corresponding color from left to right in the prime array, and mark whether the color has been used with the vis array. If you haven't used it, add a new color, otherwise use the old color (used color).

#include <cstdio>
#include <cstring>

const int N = 1005;
int T, n;
int a[N];
int prime[N], vis[N], sum;
int ans[N];

bool pd(int x){
    
    
    for(int i=2; i*i<=x; i++)
	if(x % i == 0 ) return false;
    return true;
}

int main(){
    
    
    for(int i=2; i<=1000; i++){
    
    
    	if(pd(i)) prime[++sum] = i;
	}   
	scanf("%d",&T);
    while(T--){
    
    
        scanf("%d",&n);
        int cnt = 0;
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=n; i++){
    
    
            scanf("%d",&a[i]);
            for(int j=1; j<=sum; j++) 
			if(a[i] % prime[j] == 0){
    
    
            	if(!vis[j]) {
    
    
					vis[j] = ++cnt;
				}
        		ans[i] = vis[j];
				break;
            }
        }
        printf("%d\n",cnt);
        for(int i=1; i<n; i++) 
		printf("%d ",ans[i]);
        printf("%d\n",ans[n]);
    }
    return 0;
}

C. K-Complete Word

Thinking questions
Palindrome law
1 = n
2 = n-1
3 = n-2
4 = n-3
5 = n-4

Periodic law
n 2
1 = 3 = 5 = n-1
2 = 4 = 6 = n

n 3
1 = 4 = 7 = n - 2
2 = 5 = 8 = n - 1
3 = 6 = 9 = n

n 4
1 = 5 = 9 = n - 3
2 = 6 = 10 = n - 2
3 = 7 = 11 = n - 1
4 = 8 = 12 = n

According to the sum of the palindrome string and the k-period law, we can find
that the i-th period in the k-period law is the same as the k-i + 1 period elements and then merge them into one group, and find the largest number of elements in each group The number of sums, and then subtracting sum with the same number n / k * 2 is the number of elements in this group that should change. (If i == k-i + 1, it means that there is only one cycle, so just make it a group by itself).

#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

const int N = 2e5 + 10;
char s[N];
int T, n, k; 

int main(){
    
    
	scanf("%d",&T);
	while(T--){
    
    
		map<char,int> mp;
		scanf("%d%d",&n,&k);
		scanf("%s",s+1);
		int ans = 0, sum1 = 0, sum2 = 0;
		for(int i=1; i<=(k+1)>>1; ++i){
    
    
			int l = i, r = k - i + 1;
//			printf("%d %d\n",l,r);
			if(l == r){
    
    
				mp.clear();
				sum1 = 0;
				for(int j=l; j<=n; j+=k){
    
    
					mp[s[j]]++;
					if(mp[s[j]] > sum1){
    
    
						sum1 = mp[s[j]];
					}
				}
				ans += n / k - sum1;
			} else {
    
    
				sum2 = 0;
				mp.clear();
				for(int j=l; j<=n; j+=k){
    
    
					mp[s[j]]++;
					if(mp[s[j]] > sum2){
    
    
						sum2 = mp[s[j]];
					}
				}
				for(int j=r; j<=n; j+=k){
    
    
					mp[s[j]]++;
					if(mp[s[j]] > sum2){
    
    
						sum2 = mp[s[j]];
					}
				}
				ans += n / k * 2 - sum2;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45833169/article/details/109429073