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 ...
随机推荐
- response.getWriter()和jsp中的out对象的区别
(1) out和response.getWriter属于的类不同,前者是JspWriter,后者是java.io.PrintWriter.而JspWriter是一个抽象类, PrintWriter是一 ...
- nmon内存分析
可参考: MemTotal:显示当前服务器物理内存大小,本服务器有8063180 KB≍7874 MB左右. MemFree:显示当前服务器的空闲内存大小,本服务器有5052336 KB≍4934 M ...
- PHP_CodeIgniter 细节
下载压缩包解压之后, 可以在 application/config/config.php 中修改配置信息,包括编码,目录地址 web目录默认是system,后台目录默认是Application, 可以 ...
- golang的数据类型之整型类型
数据类型: 整数 : int, int32, int64, uint, uint32, uint64 字符串 : string 布尔:bool 浮点:float32 float64 uint 表示无符 ...
- [Codeforces722E] Research Rover (dp+组合数学)
[Codeforces722E] Research Rover (dp+组合数学) 题面 给出一个N*M的方格阵,从(1,1)出发,到(N,M)结束,从(x,y)只能走到(x+1,y)或(x,y+1) ...
- Codeforces 1091C (数学)
题面 传送门 分析 假设k是固定的,那访问到的节点编号就是\(1+(a·k \mod n )\),其中a为正整数. 通过找规律不难发现会出现循环. 通过题目中的图片我们不难发现 只有k=1,2,3,6 ...
- Swift编程语言学习1.6——可选值
可选值 使用可选(optionals)来处理值可能缺失的情况.可选表示: 有值,等于 x 或者没有值 注意: C 和 Objective-C 中并没有可选这个概念.最接近的是 Objective- ...
- sql插入语句笔记
使用INSERT插入数据行 [一次插入一行数据] 全写: INSERT INTO renshi (name, sex, age ,tel) VALUES ('胡大姐','女','35','13 ...
- KVC、KVO 理解
参考经典链接: https://www.jianshu.com/p/f8198ca5e682 https://www.jianshu.com/p/be80318115a7 一. KVC 1.KVC介绍 ...
- Algorithms4th 1.1.25 欧几里得算法——数学归纳法证明
欧几里得算法的自然语言描述 计算两个非负整数p和q的最大公约数: 若q是0,则最大公约数为p.否则将p除以q得到余数r,p和q的最大公约数即为q和r的最大公约数. 数学归纳法证明 基础步骤: 若q = ...