• Codeforces 1148 E


    E - Earth Wind and Fire

    思路:

    栈模拟

    代码:

    #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;
    }
  • 相关阅读:
    Git Revert用法
    C语言------数据输入、输出函数
    C语言------运算符和表达式
    jsp九大内置对象
    三层结构下的多表查询,实体类写法
    String类型中"=="和"equals"的区别
    创建对象数组,给数组赋值(两种理解思路)
    Vbs整人代码
    设计模式之单例
    事务
  • 原文地址:https://www.cnblogs.com/widsom/p/11114822.html
Copyright © 2020-2023  润新知