大意: $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 (构造)的更多相关文章

  1. Codeforces 1148 E - Earth Wind and Fire

    E - Earth Wind and Fire 思路: 栈模拟 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC opti ...

  2. codeforces#1148E. Earth Wind and Fire(贪心)

    题目链接: http://codeforces.com/contest/1148/problem/E 题意: 给出两个长度为$n$的序列,将第一个序列变成第二个序列,顺序不重要,只需要元素完全相同即可 ...

  3. Codeforces 1148E Earth Wind and Fire

    分析 必要条件: ① $\sum_{i=1}^{n} s_i = \sum_{i=1}^{n} t_i$ 预处理: 将 $s, t$ 从小到大排序. 尝试一 首尾匹配.例子 s = 2, 2, 4, ...

  4. Earth Wind 一个查看全球风向的网站

    可以查看整个地球的全貌 ,还能定位你的位置,特别是动画挺有意思 网址:https://earth.nullschool.net/#current/wind/surface/level/orthogra ...

  5. B - Save the problem! CodeForces - 867B 构造题

    B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...

  6. Johnny Solving CodeForces - 1103C (构造,图论)

    大意: 无向图, 无重边自环, 每个点度数>=3, 要求完成下面任意一个任务 找一条结点数不少于n/k的简单路径 找k个简单环, 每个环结点数小于n/k, 且不为3的倍数, 且每个环有一个特殊点 ...

  7. Codeforces 746G(构造)

                                                                                                      G. ...

  8. Codeforces 1188A 构造

    题意:给你一颗树,树的边权都是偶数,并且边权各不相同.你可以选择树的两个叶子结点,并且把两个叶子结点之间的路径加上一个值(可以为负数),问是否可以通过这种操作构造出这颗树?如果可以,输出构造方案.初始 ...

  9. C - Long Beautiful Integer codeforces 1269C 构造

    题解: 这里的m一定是等于n的,n为数最大为n个9,这n个9一定满足条件,根据题目意思,前k个一定是和原序列前k个相等,因此如果说我们构造出来的大于等于原序列,直接输出就可以了,否则,由于后m-k个一 ...

随机推荐

  1. CISCO实验记录十:switch基本配置

    1.交换机IP配置 2.配置telnet 1.交换机IP配置 #interface vlan 1 #ip address 192.168.0.3 255.255.255.0 #no shutdown ...

  2. CentOS7重启后resolv.conf被重置的解决方案

    近期在修改一台CentOS7服务器的dns时发现只要重启服务器DNS就会被强制还原,解决方案如下: 1.首先在网卡设置中修改NM_CONTROLLED的值: 修改文件/etc/sysconfig/ne ...

  3. Coarse-to-Fine超分辨率相关

    1.A Coarse-to-Fine Subpixel Registration Method to Recover Local Perspective Deformation in the Appl ...

  4. DriverManager

    1: 注册驱动 Class.forName("com.mysql.jdbc.Driver") ; static { try { java.sql.DriverManager.reg ...

  5. python 获取昨天的日期

    from datetime import timedelta, datetime yesterday = datetime.today()+timedelta(-1) yesterday_format ...

  6. CentOS 7.4 发布下载,安全稳定的Linux发行版

    CentOS 7.4 发布了.CentOS 是 RHEL(Red Hat Enterprise Linux)源代码再编译的产物,而且在 RHEL 的基础上修正了不少已知的 Bug ,相对于其他 Lin ...

  7. 模型压缩-ThiNet

    转载:https://blog.csdn.net/u014380165/article/details/77763037 https://www.twblogs.net/a/5b8d02472b717 ...

  8. Goland 激活码

    实测有效,分享下 Goland

  9. 极客时间-左耳听风-程序员攻略-Java底层知识

    Java 字节码相关 字节码编程,也就是动态修改或是动态生成 Java 字节码.Java 的字节码相当于汇编,其中的一些细节. Java Zone: Introduction to Java Byte ...

  10. mysql学习笔记11_18(更新、插入和删除)

    1.初始表 mysql> select * from department; 2.修改销售部的地点为杭州 mysql> update department set address = '杭 ...