跳到正文

题解

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

Infinity_qwq约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年7月17日
收录
2026年7月17日
更新
2026年10月28日
篇幅
约 6 分钟
题目
P13962

版本

  1. v1当前

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

题意比较清晰不说了。

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

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

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

AC code

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

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

C++
/*

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
*/