思路:
栈模拟
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using 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<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<int, pii> #define puu pair<ULL, ULL> #define pdd pair<long double, long double> #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); //head int main() { int n; scanf("%d", &n); vector<pii> a(n); vector<int> 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<tuple<int, int,int>> res; vector<pii> 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 %d ", (int)res.size()); for (auto p : res) printf("%d %d %d ", get<0>(p)+1, get<1>(p)+1, get<2>(p)); return 0; }