大意: $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]);
}

最新文章

  1. springmvc学习资料整理
  2. Leetcode 230. Kth Smallest Element in a BST
  3. linux操作系统-脚本入门
  4. Web动画API教程2:AnimationPlayer和Timeline
  5. a标签中的点击事件
  6. Gridview布局界面练习Simple Adapter
  7. 操作系统性能分析与优化V1.0
  8. C和指针贴图
  9. 查看解决Oracle对象锁住的问题
  10. 为什么arcgis里,鼠标的图标都变成放大镜不能用了
  11. 基于visual Studio2013解决C语言竞赛题之1026判断排序
  12. 收集经常使用的.net开源项目
  13. python实现TCP/UDP通信
  14. 关于jstl.jar引用问题及解决方法
  15. PS 图像调整算法— —渐变映射
  16. innobackupex 简单使用笔记
  17. (BUG记录)使用迭代器安全的删除处于循环下集合中的元素
  18. Jquery逐行读取txt 文件
  19. Springboot &amp; Mybatis 构建restful 服务四
  20. 5月16日上课笔记-js中DOM操作

热门文章

  1. 重读APUE(6)-umask
  2. JS函数传递字符串参数(符号转义)
  3. 第11组 Alpha冲刺(5/6)
  4. xpath 轴定位表达方式
  5. SQLyog Enterprise Trial 试用期问题
  6. 路由器的路由配置命令汇总(win和linux系统)
  7. Swift 变量
  8. C++ STL 排列 next_permutation prev_permutation
  9. 升级系统中的java到1.8版本
  10. [转]Maven项目读取src.main.resources下的文件