牛客寒假算法基础集训营3 D. 处女座的训练

链接:https://ac.nowcoder.com/acm/contest/329/D

思路:贪心即可。按照ai/bi顺序由小到大排序,再按照题意按顺序求和输出即可。

 1 #include<bits/stdc++.h>
 2 const int M = 100005;
 3 using namespace std;
 4 typedef long long ll;
 5 struct course {
 6     ll min, t;
 7 }a[M];
 8 bool cmp(course a, course b)
 9 {
10     return a.min * b.t < b.min*a.t;
11 }
12 int main()
13 {
14     ll cnt=0;
15     ll n; cin >> n;
16     for (ll i = 0; i < n; i++)
17     {
18         cin >> a[i].min >> a[i].t;
19         cnt += a[i].t;
20     }
21     sort(a, a + n, cmp);
22     ll sum=0;
23     for (ll i = 0; i < n; i++)
24     {
25         cnt -= a[i].t;
26         sum += cnt * a[i].min;
27     }
28     cout << sum << endl;
29     return 0;
30 }

备注:注意本题数据量较大,用long long。

吐槽:看着像背包,就果断弃了,结果是贪心,亏了亏了。

猜你喜欢

转载自www.cnblogs.com/harutomimori/p/10324799.html