Codeforces Round #630 (Div. 2) A-C

Codeforces Round # 630 (Div. 2) topic Link

A.
meaning of problems: the need to take a step to the left, go right to step b, step c go down, go up step d, with lattice can go through repeated, and gives inspiration position (x, y), the need to ensure x1 <= x <x2, y1 <= y <= y2

I read someone else's solution to a problem, A big problem string of code that really could not stand, wrote this simple short point.
Solution: Taking into account repeated the same lattice can go too, so that we can go back and forth, just want to calculate the final value of x -a + conversion value changes b, y the -c + d
focus: need to add special boundary happens to be sentenced = y1 = x = x1 = x2 where y y2 or because not move at this time point, a move out of line
following is the code AC

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
	 ll a,b,c,d;
	 cin>>a>>b>>c>>d;
	 ll x,y,x1,y1,x2,y2;
	 cin>>x>>y>>x1>>y1>>x2>>y2;
	 if((x==x1&&x==x2&&a>0||x==x1&&x==x2&&b>0)||(y==y1&&y==y2&&c>0||y==y1&&y==y2&&d>0))
	 {
	 	cout<<"No"<<'\n';continue;
	 }
	   x=x-a+b;y=y-c+d;		
	if(x>=x1&&x<=x2&&y>=y1&&y<=y2)cout<<"Yes"<<'\n';
	else cout<<"No"<<'\n';	
	}	
}

B.
meaning of problems: the number n needs staining, and this number n, if present gcd (a [i], a [j]) = 1, each of the two numbers are not prime to each group, then! can be dyed different colors, i.e. packet, seeking how much the output of each kind of stain and stain with the number of group numbers

Directly without considering the number of engagement can be seen m <= 11 the heavy subject, a [i] <= 1000, little data is less than the last square 1000 is 31 * 31 = 961, can be considered the front m prime number, i.e., an array can be used int p [12] = {0,2,3,5,7,11,13,17,19,23,29,31}, the smallest prime factor of the same number that is assigned to the same group stain can

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second 
#define ll long long 
#define _for(i,j,k) for(int i=j;i<k;i++)
#define endl  '\n'
#define inf 1<<29-1 
const int mod=1e9+7;
const int MAX=1e6+5;
int p[12]={2,3,5,7,11,13,17,19,23,29,31};
int a[32],b[1005];
int main() {
 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n,cnt=0,ans=0;
		cin>>n;
		set<int>st;
		for(int i=0;i<n;i++)
		{
			cin>>b[i];
			for(int j=0;j<11;j++)
			{
				if(b[i]%p[j]==0)
				{
					if(st.count(p[j])==0)
					{
						ans++;
						a[p[j]] = ++cnt;
						b[i] = a[p[j]];
						st.insert(p[j]);
					}
					else b[i] = a[p[j]];
					break;
				}
			}
		}
		cout<<ans<<'\n';
		for(int i=0;i<n;i++)
		cout<<b[i]<<" ";
		cout<<'\n';
	}
}

C.
question is intended: to enter a string of length n, can be divided into n / k segments, each of length k, you need to change the character of each segment, and such that each subsection and satisfies the same palindrome itself, seeking to change happened number

Pretreatment violence + greedy
you can consider the string of 26 characters, and there are n / k sections each section the same as the first segment, i.e., the same position of each segment of the same letter, it can start a two-dimensional array a [i] [j] records the number of times per segment of the i-th character appears. Consider each segment has its own palindrome and n / k segments, for greedy min.
It should be noted that the topic will be stuck memset, so only manually for initialization; note k parity nature, whether it is necessary once again greed;

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second 
#define ll long long 
#define _for(i,j,k) for(int i=j;i<k;i++)
#define endl  '\n'
#define inf 1<<29-1 
const int mod=1e9+7;
const int MAX=1e6+5; 
ll a[MAX][26];
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		for(int i=0;i<k;i++)
		{
			for(int j=0;j<26;j++)
			{
				a[i][j]=0;		
			}
		}
		string s;
		cin>>s;
		for(int i=0;i<k;i++)
		{
			for(int j=i;j<n;j+=k)
			{
				a[i][s[j]-'a']++;
			}	
		}
		ll ans=0,Max,l=0,r=k-1,d=n/k;
		while(l<r)
		{
		
			Max=MAX;
			for(int j=0;j<26;j++)
			{
				Max=min(Max,2*d-a[l][j]-a[r][j]);
			}
			ans+=Max;
			l++;r--;
		}
		if(l==r)
		{
			Max=MAX;
			for(int i = 0; i < 26; i++) 
			{
				Max=min(Max,d-a[l][i]);
			}
			ans+=Max;
		}
		cout<<ans<<'\n';
	}	
}
Released four original articles · won praise 6 · views 171

Guess you like

Origin blog.csdn.net/hrd535523596/article/details/105251224