1. 程式人生 > 其它 >2022.04.12(Codeforces Round #781 (Div. 2))

2022.04.12(Codeforces Round #781 (Div. 2))

Problem - A - Codeforces

說明:直接輸出n-3,1,1,1即可

程式碼:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int gcd(int x,int y){
    if(x<y){
        swap(x,y);
    }
    if(x%y==0){
        return y;
    }
    return
gcd(y,x%y); } int main(){ int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); if(n%4==0){ printf("%d %d %d %d\n",n/4,n/4,n/4,n/4); }else{ for(int i=1;i<n-2;i++){ if(gcd(i,n-2-i)==1){ printf(
"%d %d 1 1\n",i,n-2-i); break; } } } } }
View Code

Problem - B - Codeforces

說明:直接模擬即可,首先肯定是陣列中最多的數字,通過有限次的複製陣列然後交換,一直迴圈

wa掉的點:(1)迴圈寫的有問題,應該是模擬每一步,不應該簡單統計;

程式碼:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include
<cmath> #include<cstring> using namespace std; int maxx=1e5+10; int main(){ int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); int a[maxx]={0}; for(int i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); int sum[maxx]={0}; int now=a[0]; int t=0; for(int i=0;i<n;i++){ if(a[i]==now){ sum[t]++; }else{ now=a[i]; t++; sum[t]=1; } } sort(sum,sum+t+1); int p=sum[t]; if(t==0){ printf("0\n"); continue; } now=sum[t]; int ans=0; p=sum[t]; while(1){ if(now==n){ break; } if(now>n){ // ans+=n-p; break; } if(now<n){ //printf("變換前:ans:%d p:%d now:%d\n",ans,p,now); ans++; p=now; ans+=min(p,n-p); now*=2; //printf("變換後:ans:%d p:%d now:%d\n",ans,p,now); } } printf("%d\n",ans); } }
View Code