大意: $n$个石子, 第$i$个石子初始位置$s_i$, 每次操作选两个石子$i,j$, 要求$s_i<s_j$, 任取$d$, 满足$0le 2dle 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 ' ' #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 ",cnt); REP(i,1,cnt) printf("%d %d %d ",p1[i],p2[i],p3[i]); }