思路:
栈模拟
代码:
#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize(4)#includeusing namespace std;#define y1 y11#define fi first#define se second#define pi acos(-1.0)#define LL long long//#define mp make_pair#define pb emplace_back#define ls rt<<1, l, m#define rs rt<<1|1, m+1, r#define ULL unsigned LL#define pll pair #define pli pair #define pii pair #define piii pair #define puu pair #define pdd pair #define mem(a, b) memset(a, b, sizeof(a))#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);//headint main() { int n; scanf("%d", &n); vector a(n); vector b(n); for (int i = 0; i < n; ++i) scanf("%d", &a[i].fi), a[i].se = i; for (int i = 0; i < n; ++i) scanf("%d", &b[i]); sort(a.begin(), a.end()); sort(b.begin(), b.end()); vector > res; vector st; for (int i = 0; i < n; ++i) { int d = b[i]-a[i].fi; if(d == 0) continue; if(d > 0) { st.pb(d, a[i].se); } else { while(d < 0) { if(st.empty()) return 0*puts("NO"); int dd = min(st.back().fi, -d); d += dd; st.back().fi -= dd; res.pb(st.back().se, a[i].se, dd); if(st.back().fi == 0) st.pop_back(); } } } if(!st.empty()) return 0*puts("NO"); printf("YES\n%d\n", (int)res.size()); for (auto p : res) printf("%d %d %d\n", get<0>(p)+1, get<1>(p)+1, get<2>(p)); return 0;}