Atcoder Grand Contest 037C(贪心,优先队列,思维)
#define HAVE_STRUCT_TIMESPEC//编译器中time.h和phread.h头文件中timespec结构体重名,故加此行
#include<bits/stdc++.h>
using namespace std;
int a[200007],b[2000007];
priority_queue<pair<int,int> >q;
int n;
int mi(int x){
    return x==1?n:x-1;
}
int pl(int x){
    return x==n?1:x+1;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    for(int i=1;i<=n;++i){
        cin>>b[i];
        q.push({b[i],i});
    }
    long long ans=0;
    while(!q.empty()){
        int i=q.top().second;
        q.pop();
        if(b[i]==a[i])
            continue;
        if(a[i]>b[i]-b[mi(i)]-b[pl(i)])//如果a[i]>b[i]-b[mi(i)]-b[pl(i)],那么a[i]加上已经等于b[i-1]的a[i-1]和已经等于b[i+1]的a[i+1]就会超过b[i],较小的a[i]会先变成b[i],明显是要小的b[i]先满足题意才能大的b[i]后满足题意,所以如果大的b[i],a[i]+b[i-1]+b[i+1]相加都比b[i]大,那么a[i]就大的无法得到了
            return puts("-1"),0;
        int tmp=b[mi(i)]+b[pl(i)];
        int tot=b[i]-a[i];//tot是还需要加上多少才能让a[i]变成b[i]
        ans+=tot/tmp;//tmp是每次能加上的最大值,这样贪心策略最优
        tot%=tmp;//tot保留余数
        b[i]=a[i]+tot;//b[i]变成一个小的数字,放在优先队列里排序,可以让i-1和i+1在变化的时候用到它,它是a[i]变成原来b[i]的一个中间值
        if(a[i]>b[i])
            return puts("-1"),0;
        if(a[i]!=b[i])
            q.push({b[i],i});//扔进队列等待处理中间值
    }
    cout<<ans;
    return 0;
}
Atcoder Grand Contest 037C(贪心,优先队列,思维)的更多相关文章
- Atcoder Grand Contest 031C(构造,思维,异或,DFS)
		#include<bits/stdc++.h>using namespace std;int n,a,b,sum;void dfs(int x,int y,int ban){ if( ... 
- AtCoder Grand Contest 019 B - Reverse and Compare【思维】
		AtCoder Grand Contest 019 B - Reverse and Compare 题意:给定字符串,可以选定任意i.j且i<=j(当然i==j时没啥卵用),然后翻转i到j的字符 ... 
- Atcoder Grand Contest 024 E - Sequence Growing Hard(dp+思维)
		题目传送门 典型的 Atcoder 风格的计数 dp. 题目可以转化为每次在序列中插入一个 \([1,k]\) 的数,共操作 \(n\) 次,满足后一个序列的字典序严格大于前一个序列,问有多少种操作序 ... 
- AtCoder Grand Contest 011
		AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ... 
- AtCoder Grand Contest 009
		AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ... 
- AtCoder Grand Contest 019 F-yes or no
		AtCoder Grand Contest 019 F-yes or no 解题思路: 考虑一个贪心策略,假设当前还有 \(x\) 道 \(\text{yes}\) 和 \(y\) 道 \(\text ... 
- AtCoder Grand Contest 019 A: Ice Tea Store
		tourist出的题诶!想想就很高明,老年选手可能做不太动.不过A题还是按照惯例放水的. AtCoder Grand Contest 019 A: Ice Tea Store 题意:买0.25L,0. ... 
- AtCoder Grand Contest 012
		AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ... 
- AtCoder Grand Contest 031 简要题解
		AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ... 
随机推荐
- Azure IoT Hub 十分钟入门系列 (2)- 使用模拟设备发送设备到云(d2c)的消息
			本文主要分享一个案例: 10分钟- 使用Python 示例代码和SDK向IoT Hub 发送遥测消息 本文主要有如下内容: 了解C2D/D2C消息: 了解IoT Hub中Device的概念 了解并下载 ... 
- Python Django中一些少用却很实用的orm查询方法
			一.使用Q对象进行限制条件之间 "或" 连接查询 from django.db.models import Q from django.contrib.auth.models im ... 
- easyExcel+poi导出Excel出现乱码
			这种问题肯定是浏览器编码问题,修改官方给的util就好了 
- 操作word
			package com.gwt.flow.task; import java.io.File; import java.io.FileInputStream; import java.io.FileN ... 
- 201771010135 杨蓉庆AND张燕 《面对对象程序设计(java)》第十一周学习总结
			1.实验目的与要求 (1) 掌握Vetor.Stack.Hashtable三个类的用途及常用API: (2) 了解java集合框架体系组成: (3) 掌握ArrayList.LinkList两个类的用 ... 
- android nfc功能开发
			链接:Android NFC开发详细总结 https://blog.csdn.net/zhwadezh/article/details/79111348 链接2:Android NFC功能 简单实 ... 
- 消息队列(四)--- RocketMQ-消息发送
			概述 RocketMQ 发送普通消息有三种 可靠同步发送 可靠异步发送 单向(oneway)发送 :只管发送,直接返回,不等待消息服务器的结果,也不注册回调函数,简单地说,就是只管发,不管信息是否发送 ... 
- codeforces-Three Friends
			Three Friends Three friends are going to meet each other. Initially, the first friend stays at the ... 
- Codeforces AIM Tech Round 5 (rated, Div. 1 + Div. 2)
			A. Find Square time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ... 
- 解决:hudson.plugins.git.GitException: Could not init
			解决: 
