其他分享
首页 > 其他分享> > AGC037C Numbers on a Circle【构造】

AGC037C Numbers on a Circle【构造】

作者:互联网

从后往前做,每次将\(B_i\)减去相邻两个数,注意如果最大的数没有变成初始状态,那么肯定要减,否则相邻两边的就减不了,所以用堆维护。根据辗转相除的复杂度,\(O(n\log^2 n)\)。

#include<bits/stdc++.h>
#define Rint register int
#define MP make_pair
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int N = 200003;
int n, a[N], b[N];
LL ans;
priority_queue<pii> pq;
int main(){
    scanf("%d", &n);
    for(Rint i = 1;i <= n;i ++) scanf("%d", a + i);
    for(Rint i = 1;i <= n;i ++){
        scanf("%d", b + i);
        if(a[i] != b[i]) pq.push(MP(b[i], i));
    }
    while(!pq.empty()){
        pii now = pq.top(); pq.pop();
        int i = now.se, pre = (now.se + n - 2) % n + 1, suf = now.se % n + 1, step = (b[i] - a[i]) / (b[pre] + b[suf]);
        if(!step){puts("-1"); return 0;}
        ans += step; b[i] -= step * (b[pre] + b[suf]);
        if(a[i] != b[i]) pq.push(MP(b[i], i));
    }
    printf("%lld", ans);
}

标签:typedef,Numbers,int,Rint,long,AGC037C,pair,Circle,define
来源: https://www.cnblogs.com/AThousandMoons/p/11609160.html