暴力求助
查看原帖
暴力求助
408019
ShanCreeperProEnder楼主2022/8/28 22:05

想寫個 70 pts 做法,然後只有 55 分 /yiw

時間複雜度 O(n2)O(n^2) 能過的吧。

對以下代碼的一些解釋:

  • like():旋轉項鏈操作;
  • addq1,addq2,第一個項鏈和第二個項鏈的亮度增加操作;
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fore(i,x,n) for(int i=x;i<=n;i++)
const int MAXX=10005;
const int mod=1;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
inline void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
inline void writesp(int x){
    write(x);
    putchar(' ');
}
inline void writeln(int x){
    write(x);
    putchar('\n');
}
inline string sread(){
    char ch=getchar(); string st1="";
    while(!((ch>='a')&&(ch<='z'))) ch=getchar();
    while((ch>='a')&&(ch<='z')) st1+=ch,ch=getchar();
    return st1;
}
queue<int> q1; //第一個項鏈
queue<int> q2; //第二個項鏈
int n,m;
int maxq1=-1,maxq2=-1;
int sumq1=0,sumq2=0;
int c;
int ans=0;
int endans=0;
inline void like(){ //O(n)
    fore(i,1,n){
        int x=q1.front();
        int y=q2.front();
        ans+=((x-y)*(x-y));
        q1.pop();
        q2.pop();
        q1.push(x);
        q2.push(y);
    }
    q1.push(q1.front());
    q1.pop(); 
}
inline void addq1(){ //O(n)
    fore(i,1,n){
        int x=q1.front()+1;
        q1.pop(); 
        q1.push(x); 
    }
}
inline void addq2(){ //O(n)
    fore(i,1,n){
        int y=q2.front()+1;
        q2.pop();
        q2.push(y);
    }
}
/*inline void test1(){
    fore(i,1,n){
        writesp(q1.front());
        q1.push(q1.front());
        q1.pop();
    }
    puts("");
    fore(i,1,n){
        writesp(q2.front());
        q2.push(q2.front());
        q2.pop();
    }
    puts("");
}
inline void test2(){
    writesp(sumq1);
    writeln(sumq2);
}*/
signed main(){
    //freopen("gift.in","r",stdin);
    //freopen("gift.out","w",stdout);
    n=read(); m=read();
    fore(i,1,n){ //O(n)
        c=read();
        q1.push(c);
        maxq1=max(c,maxq1);
        sumq1+=c;
    }
    fore(i,1,n){ //O(n)
        c=read();
        q2.push(c);
        maxq2=max(c,maxq2);
        sumq2+=c;
    }
    //test1();
    if(sumq1>sumq2){
        while(1){ //O(n)
            if(sumq2+n>sumq1) break;
            else{
                sumq2+=n;
                addq2();
            }
        }
    }
    else{
        while(1){ //O(n)
            if(sumq1+n>sumq2) break;
            else{
                sumq1+=n;
                addq1();
            }
        }
    }
    //test2();
    //test1();
    fore(i,1,n){ //O(n^2)
        like();
        if(i==1) endans=ans;
        else endans=min(endans,ans);
        ans=0;
        /*writesp(endans);
        test1();
        puts("");*/
    }
    //puts("");  
    write(endans);
}
/*

15
21

*/
2022/8/28 22:05
加载中...