zoukankan      html  css  js  c++  java
  • AGC037C Numbers on a Circle【构造】

    从后往前做,每次将(B_i)减去相邻两个数,注意如果最大的数没有变成初始状态,那么肯定要减,否则相邻两边的就减不了,所以用堆维护。根据辗转相除的复杂度,(O(nlog^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);
    }
    
  • 相关阅读:
    存储型 XSS 原理复现
    反射型 XSS 原理复现
    HTTP 简易理解
    Markdown 流程图语法
    Dirsearch 快速开始
    sqlmap 快速开始
    SQL 注入原理
    XSS 原理
    51nod 1835 完全图
    11.5 AM 请求
  • 原文地址:https://www.cnblogs.com/AThousandMoons/p/11609160.html
Copyright © 2011-2022 走看看