[2020蓝桥杯B组决赛] G-答疑
题解
这个题不会证明,但是看着像是 排序 + 贪心 的做法,先按 $s + a$ 从小到大排序, 然后累加求和即可,不清楚是不是这个思路,但是网上大部分人好像就是这个思路,在此仅供一个参考。
#include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int N = 1010; int n; struct stu { int s, a, e; // t1 = s + a // t2 = s + a + e int t1, t2; }; stu st[N]; bool cmp(stu a, stu b) { return a.t1 < b.t1; } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> st[i].s >> st[i].a >> st[i].e; st[i].t1 = st[i].s + st[i].a; st[i].t2 = st[i].t1 + st[i].e; } // 先按照 t1 由小至大排序 sort(st, st + n, cmp); // 累加和 LL times = 0; LL s = 0; for (int i = 0; i < n; ++i) { times += st[i].t1 + s; s += st[i].t2; } cout << times << endl; return 0; }
这个题目最后 20分钟 做出来的,但是 $sort$ 忘记 $return$ 了,编译也不报错,慌慌忙忙就交上去了,哭哭哭~~。