Earth Wind and Fire CodeForces - 1148E (构造)
大意: $n$个石子, 第$i$个石子初始位置$s_i$, 每次操作选两个石子$i,j$, 要求$s_i<s_j$, 任取$d$, 满足$0\le 2d\le s_j-s_i$, 将$s_i,s_j$改为$s_i+d,s_j-d$. 给定数组$t$, 求是否能将所有石子位置摆成数组$t$.
没要求最小化操作数, 所以直接贪心选即可, 操作数一定是不超过$n$的.
这场当时没时间打, 感觉好亏.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e6+10; int n, c1, d1;
struct _ {
int x,id;
bool operator < (const _ &rhs) const {
return x<rhs.x;
}
} a[N], b[N], c[N], d[N];
int p1[N],p2[N],p3[N],cnt,f[N];
void add(int x, int y, int v) {
++cnt;
if (f[x]>f[y]) puts("NO"),exit(0);
p1[cnt]=x,p2[cnt]=y,p3[cnt]=v;
f[x]+=v,f[y]-=v;
}
int main() {
scanf("%d", &n);
ll s1=0,s2=0;
REP(i,1,n) {
scanf("%d",&a[i].x),a[i].id=i,s1+=a[i].x;
f[i]=a[i].x;
}
REP(i,1,n) scanf("%d",&b[i].x),b[i].id=i,s2+=b[i].x;
if (s1!=s2) return puts("NO"),0;
sort(a+1,a+1+n);
sort(b+1,b+1+n);
REP(i,1,n) {
if (a[i].x<b[i].x) c[++c1]={b[i].x-a[i].x,a[i].id};
else if (a[i].x>b[i].x) d[++d1]={a[i].x-b[i].x,a[i].id};
}
sort(d+1,d+1+d1,[](_ x,_ y){return f[x.id]>f[y.id];});
sort(c+1,c+1+c1,[](_ x,_ y){return f[x.id]>f[y.id];});
int now = 1;
REP(i,1,d1) {
while (now<=c1&&c[now].x<=d[i].x) {
add(c[now].id,d[i].id,c[now].x);
d[i].x-=c[now].x, ++now;
}
if (d[i].x) {
add(c[now].id,d[i].id,d[i].x);
c[now].x-=d[i].x;
}
}
puts("YES");
printf("%d\n",cnt);
REP(i,1,cnt) printf("%d %d %d\n",p1[i],p2[i],p3[i]);
}
Earth Wind and Fire CodeForces - 1148E (构造)的更多相关文章
- Codeforces 1148 E - Earth Wind and Fire
E - Earth Wind and Fire 思路: 栈模拟 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC opti ...
- codeforces#1148E. Earth Wind and Fire(贪心)
题目链接: http://codeforces.com/contest/1148/problem/E 题意: 给出两个长度为$n$的序列,将第一个序列变成第二个序列,顺序不重要,只需要元素完全相同即可 ...
- Codeforces 1148E Earth Wind and Fire
分析 必要条件: ① $\sum_{i=1}^{n} s_i = \sum_{i=1}^{n} t_i$ 预处理: 将 $s, t$ 从小到大排序. 尝试一 首尾匹配.例子 s = 2, 2, 4, ...
- Earth Wind 一个查看全球风向的网站
可以查看整个地球的全貌 ,还能定位你的位置,特别是动画挺有意思 网址:https://earth.nullschool.net/#current/wind/surface/level/orthogra ...
- B - Save the problem! CodeForces - 867B 构造题
B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...
- Johnny Solving CodeForces - 1103C (构造,图论)
大意: 无向图, 无重边自环, 每个点度数>=3, 要求完成下面任意一个任务 找一条结点数不少于n/k的简单路径 找k个简单环, 每个环结点数小于n/k, 且不为3的倍数, 且每个环有一个特殊点 ...
- Codeforces 746G(构造)
G. ...
- Codeforces 1188A 构造
题意:给你一颗树,树的边权都是偶数,并且边权各不相同.你可以选择树的两个叶子结点,并且把两个叶子结点之间的路径加上一个值(可以为负数),问是否可以通过这种操作构造出这颗树?如果可以,输出构造方案.初始 ...
- C - Long Beautiful Integer codeforces 1269C 构造
题解: 这里的m一定是等于n的,n为数最大为n个9,这n个9一定满足条件,根据题目意思,前k个一定是和原序列前k个相等,因此如果说我们构造出来的大于等于原序列,直接输出就可以了,否则,由于后m-k个一 ...
随机推荐
- Default Keyboard Shortcut Schemes
Default Keyboard Shortcut Schemes All ReSharper actions can be invoked with keyboard shortcuts. Most ...
- nginx - nginx下配置thinkphp5
首先tp5的访问目录指向到webroot/public文件夹中.thinkphp的url访问:http://serverName/index.php(或者其它应用入口文件)/模块/控制器/操作/[参数 ...
- Highcharts 使用总结
一.Highcharts series属性 1.下面是一个基本曲线图的例子: <html> <head> <meta charset="UTF-8" ...
- windows/Linux 下安装coreseek/sphinx
2013年12月8日 17:26:26 注意的地方: 1.配置文件的 数据源, 索引, 服务 这3处配置的路径要写成windows识别的路径,最好是绝对路径 2.安装windows服务的时候,可以不带 ...
- JAVA 基础编程练习题30 【程序 30 插入数字】
30 [程序 30 插入数字] 题目:有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数组中. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的 ...
- Redis CrackIT 入侵事件引发Linux 沦陷
▲针对全球6379端口的redis服务器做了扫描,结果如上图 如图开放在公网的redis的6379端口的ip总数有63443个.无密码认证的IP有43024个,在总数占比里达到67%.发现遭受到red ...
- Carthage - Could not find any available simulators for iOS
升级xcode版本后,用carthage编译第三方库有可能会报这个错误:[Could not find any available simulators for iOS] 两个解决方法: 1. 升级你 ...
- Product - 产品经理 - 知返
特别说明 本文是已读书籍的学习笔记和内容摘要,原文内容有少部分改动,并添加一些相关信息,但总体不影响原文表达. - ISBN: 9787568041591 - https://book.douban. ...
- IIS中应用Application Request Route 配置负载均衡
转自:https://blog.csdn.net/wucong60/article/details/84930234 简介ApplicationRequest Route(后面简称为ARR)是一个寄宿 ...
- Docker使用Portainer搭建可视化界面
Portainer介绍 Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控制台 ...