個人博客:https://voids5.cn/2020/04/01/Codeforces-Round-630-Div-2/
題目鏈接:https://codeforces.com/contest/1332
A. Exercising Walk
題意:在題目所給次數反覆左右上下跳過程中不超過題目所給範圍
Code:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef long long ll;
int t,a,b,c,d,x,y,x1,x2,y1,y2;
int main()
{
cin>>t;
while(t--)
{
cin>>a>>b>>c>>d;
cin>>x>>y>>x1>>y1>>x2>>y2;
if(x==x1 && x1==x2 && (a||b))
{
puts("No");
continue;
}
if(y==y1 && y==y2 && (c||d))
{
puts("No");
continue;
}
if(a>=b) a=a-b,b=0;
else b=b-a,a=0;
if(c>=d) c=c-d,d=0;
else d=d-c,c=0;
if(x-x1>=a&&x2-x>=b&&y-y1>=c&&y2-y>=d) puts("Yes");
else puts("No");
}
return 0;
}
B. Composite Coloring
題意:將所給數字都染上顏色,兩個數字能被染上相同數字的條件是gcd>1,且顏色最大不能超過11
解題思路:分解質因數,不用將顏色的數量控制在最小
Code:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef long long ll;
int t,n,a[N],ans[N];
int b[N];
int main()
{
cin>>t;
while(t--)
{
memset(b,0,sizeof(b));
cin>>n;
int l=1;
for(int i=0;i<n;i++){
cin>>a[i];
for(int j=2;j*j<=a[i];j++){
if(a[i]%j==0){
if(b[j]) ans[i]=b[j];
else ans[i]=b[j]=l++;
break;
}
}
}
cout<<--l<<endl;
for(int i=0;i<n;i++){
cout<<ans[i]<<' ';
}
cout<<endl;
}
return 0;
}
C. K-Complete Word
題意:給你一個字符串,要保證這個字符串本身是迴文且是以k個字符爲一個週期的字符串,求對字符最小改動次數使字符串滿足條件
解題思路:這個字符串是以一個週期的一半複製顛倒而成的,然後從半個週期枚舉整個字符串的情況,求最小改動次數即可
Code:
#include<bits/stdc++.h>
using namespace std;
const int N=200010;
typedef long long ll;
int t,n,k,m;
int a[N],c[N];
int ans[27];
string s;
int main()
{
cin>>t;
while(t--){
cin>>n>>k>>s;
int res=0;
for(int i=0;i<(k+1)/2;i++){
memset(ans,0,sizeof(ans));
for(int j=i;j<n;j+=k){//其他週期
ans[s[j]-'a']++;
}
if(k-i-1 != i){
for(int j=k-i-1;j<n;j+=k){ //另外半個週期加其對應的其他週期
ans[s[j]-'a']++;
}
}
int m=*max_element(ans,ans+26);//求數組中的最大值
int sum=accumulate(ans,ans+26,0);//計算數組的和
res+=sum-m;
}
cout<<res<<endl;
}
return 0;
}
D. Walk on Matrix
題意:求最優解法和dp解法的差是k所滿足的矩陣
解題思路:使dp路徑答案爲0,最優路徑爲k(大佬的代碼,先貼出來,明天再看)
#include<bits/stdc++.h>
using namespace std;
const int N=200010;
typedef long long ll;
int t,n,k,m;
int a[N],c[N];
char s[N];
int main()
{
cin>>n;
for(int i=0;i<20 && (m+(1<<i)<3e5);i++)
{
m+=(1<<i);
if((n&(1<<i))==0&&k+(1<<i)<3e5){
k+=(1<<i);
}
}
cout<<2<<' '<<3<<endl;
cout<<m<<' '<<k<<' '<<0<<endl;
cout<<n<<' '<<m<<' '<<n;
return 0;
}