题解:P13962 [ICPC 2023 Nanjing R] 电梯

· · 题解

模拟赛挂大分,遂写题解补题。大贪心,如果想到了动态规划的话想一会儿就知道不可做了。

题意比较清晰不说了。

注意到最大的包裹无论如何都是要运输的,且每一次运输的代价是以当前最大值运输的。所以我们从高到低排序,将大的尽可能的在一块运输,再此基础上优先运输重量为 2 的包裹。放完后可能还有一个大小为 1 的位置。此时再放一个最大的、重量为 1 的包裹,此方法最优。

虽然结论比较显然,但由于我没有翻到证明方法,尝试证明一下。

从大到小排序后,假设有一个包裹 A 在第 i 趟,一个包裹 B 在第 j 趟,由上述的 f_A > f_B 且 i < j。考虑将 A 与 B 交换,。显然第 i 趟的代价是由比 A 更大的包裹决定的,所以代价不变,如果第 i 趟的代价原本就是 A,那么代价变为 f_{A+1},二者差为 f_A - f_{A+1}。而第 j 趟的代价会被 f_A 替换,若第 j 趟的代价原本为 f_{A+q},则二者差为 f_{A+q} - f_A。相加后和为 f_{A+q} - f_{A+1},显然 A+q 一定比 A + 1 靠后,所以 f_{A+q} - f_{A+1} \le 0,减去差后代价反而变大,故上述方案最优。

::::success[AC code]

其实这个代码说难不难说简单不简单,细节很多,而且样例水成屎了,容易产生逻辑错误。我觉得没有注释你们应该也能看懂。

这个故事告诉我们静态调试的重要性。

/*

time : 2026/7/17 8:45:07

contest :

Problem :

Solution :

Code by 720872

*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<climits>
#include<bitset>
#define ll long long
#define ull unsigned long long
#define il inline
#define endl '\n'
#define y1 Y1
#define pii pair<int,int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define _1 first
#define _2 second
#define mkp make_pair
#define pb push_back
#define lowbit(x) x & (-x)
#define int ll
#define debug cout << "debug" << endl;
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
using namespace std;

inline ll read(){
    ll k = 0, f = 1, c = gc();
    for (; !isdigit(c); c = gc()) if (c == '-') f = -1;
    for (; isdigit(c); c = gc()) k = k * 10 + (c ^ 48);
    return k * f;
}
//inline void put(ll x){
//  if(x < 0) putchar('-'), x = -x;
//  if(x < 10) putchar(x + '0');
//  else put(x / 10), putchar(x % 10 + '0');
//}

const int N = 1e5 + 5;
int T, n, one[N], two[N];
ll k;

void solve(){
    memset(one, 0, sizeof one);
    memset(two, 0, sizeof two);
    n = read(), k = read();
    for(int i = 1; i <= n; i++){
        int c = read(), w = read(), f = read();
        if(w == 1) one[f] += c;
        else two[f] += c;
    }
    ll p = 0, e = 0, ans = 0;
    //e 代表这一趟开放的空间,p代表开放大小为 1 的空间的数量。
    for(int i = N - 1; i >= 1; i--){
        ll x = one[i], y = two[i];
        ll t = min(p, x); p -= t, x -= t;
        if(x == 0 && y == 0) continue;
        ll w = x + 2 * y;
        if(e >= w){
            e -= w;
            if(e == 1) p++, e = 0;
        }else{
            ll a = min(y, e / 2);
            y -= a, e -= 2 * a;
            if(e > 0){
                ll b = min(x, e);
                x -= b, e -= b;
            }
            if(e == 1) p++, e = 0;
            w = x + 2 * y;
            if(w > 0){
                ll cnt = w / k, m = w % k;
                ans += i * (cnt + (m ? 1 : 0));
                if(m == 0) e = 0;
                else{
                    e = k - m;
                    if(e == 1) p++, e = 0;                      
                }
            }
        }
    }
    cout << ans << endl;
}

signed main(){
//  freopen("even.in", "r", stdin);
//  freopen("even.out", "w", stdout);
//  ios::sync_with_stdio(false);
//  cin.tie(0); cout.tie(0);
    T = read();
    while(T--)
        solve();
    return 0;
}
/*
input1:
2
4 6
1 1 8
7 2 5
1 1 7
3 2 6
8 1200000
100000 1 100000
100000 1 12345
100000 2 100000
100000 2 12345
100000 1 100000
100000 1 12345
100000 2 100000
100000 2 12345
ouput:
24
100000
*/

::::