AGC037C Numbers on a Circle
题目大意
给你一个序列a和序列b
每次操作是a[i]+=a[i-1]+a[i+1]
问a经过最少几次操作可以得到b
分析
用堆维护a
每次取出最大的
撤销操作直到不能撤销
将新数放入堆
不断维护即可
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define int long long
priority_queue<pair<int,int> >q;
int n,m,b[],a[],pre[],nxt[];
signed main(){
int i,j,k,ans=;
scanf("%lld",&n);
for(i=;i<=n;i++)scanf("%lld",&a[i]);
for(i=;i<=n;i++){
scanf("%lld",&b[i]);
q.push(mp(b[i],i));
}
for(i=;i<=n;i++){
nxt[i]=i+;
pre[i]=i-;
}
pre[]=n;
nxt[n]=;
while(!q.empty()){
int x=q.top().se;
q.pop();
if(b[x]==a[x])continue;
int y=pre[x],z=nxt[x];
if(b[x]-b[y]-b[z]<a[x]){
puts("-1");
return ;
}
k=b[x]-a[x];
ans+=k/(b[y]+b[z]);
b[x]=(k%(b[y]+b[z]))+a[x];
if(a[x]!=b[x])q.push(mp(b[x],x));
}
cout<<ans<<"\n";
return ;
}
AGC037C Numbers on a Circle的更多相关文章
- AGC037C Numbers on a Circle(神奇思路)
Atcoder 全是神仙题-- 先变成能不能从 \(b\) 到 \(a\).操作变成一个数减掉旁边两个数. 考虑里面最大的且不和 \(a\) 中相等的那个数.它两边的数此时都不能操作,否则就减到非正数 ...
- AGC037C Numbers on a Circle【构造】
从后往前做,每次将\(B_i\)减去相邻两个数,注意如果最大的数没有变成初始状态,那么肯定要减,否则相邻两边的就减不了,所以用堆维护.根据辗转相除的复杂度,\(O(n\log^2 n)\). #inc ...
- AGC037 C Numbers on a Circle【思维】
题目传送门 题意 这道题被某大佬改编拿来出成考试题,是长这个样子的: 好的,其实这才是真正的题意: 给定初始序列和最终序列,每次选择一个数变成自己和相邻2个数的和.问初始序列是否可以变为最终序列,若可 ...
- Codeforces Beta Round #94 div 1 D Numbers map+思路
D. Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- CodeForces 128D Numbers 构造
D. Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- crossplatform---Node.js Applications with VS Code
Node.js is a platform for building fast and scalable server applications using JavaScript. Node.js i ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- Daily Scrum – 1/12
Meeting Minutes Merge Wordlist & Word Recite entry. (P0) – Done. Remove "Word Challenge&quo ...
- UVA 1452 八 Jump
Jump Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practi ...
随机推荐
- JS中设置input的type="radio"默认选中
html: <input id="Radio1" type="radio" value="男" name="st_Sex&q ...
- 机器学习实战_基于Scikit-Learn和Tensorflow读书笔记
第一部分 机器学习基础 第二部分 神经网络和深度学习 第9章 运行Tensorflow 分布式系统:分布式系统的定义是这个系统建立在网络的操作系统,具有高度的内聚性和透明性,它与网络的区别在于高层软件 ...
- [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)
[Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...
- Codeforces 1172B(组合数学)
题面 给出一棵n个点的树,要求把它画在圆上,且边不相交,画法与排列一一对应(即旋转后相同的算不同种),求方案数.如下图是4个点的树\(T:V=\{1,2,3,4\},E=\{(1,2),(1,3),( ...
- activity 生命周期 http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for
331down voteaccepted See it in Activity Lifecycle (at Android Developers). onCreate(): Called when t ...
- struts2 基础学习
Struts 2是在WebWork2基础发展而来的. 注意:struts 2和struts 1在代码风格上几乎不一样. Struts 2 相比Struts 1的优点: 1.在软件设计上Struts ...
- azkaban简单使用
步骤 1.通过web服务器创建项目,项目名称和描述都是必须填的2.上传zip包 job定义 1.简单的job一个job就是一个properties文件,只不过文件是以.job结尾,文件里面定义了job ...
- go中基本数据类型的默认值
代码 // 基本数据类型(整型,浮点型,字符串型,布尔型)的默认值 package main import ( "fmt" ) func main() { var a int va ...
- oracel分页查询
SELECT * FROM ( SELECT temp.*, ROWNUM RN FROM (SELECT * FROM 表名) temp WHERE ROWNUM <=end (page*pa ...
- 203-基于ARM和双TI DSP TMS320C6678的6UCPCI高清编解码处理平台
基于ARM和双TI DSP TMS320C6678的6UCPCI高清编解码处理平台 1.产品简介 该板卡由我公司自主研发,以TI Cortex-A8.TI 双DSP TMS320C6678为设计核心, ...