NC25043 Protecting the Flower

题目地址:

https://ac.nowcoder.com/acm/problem/25043

基本思路:

又是这种改排序贪心,我们先只考虑两只牛谁先更优,假如先那么损耗的花为,假如先那么损耗的花为,那么我们可以知道只要根据的大小排序,就能决定这两只牛谁先谁后,那么实际上容易证这个对于更多牛的情况也是普遍成立的,因此根据此规则排序然后直接模拟一遍题意统计答案就行了。

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define int long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define INF (int)1e18

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

struct Node{
    int t,d;
    bool operator < (const Node &no) const {
      return t * no.d < d * no.t;//排序规则;
    }
};
vector<Node> vec;
int n,t,d;
signed main() {
  IO;
  cin >> n;
  rep(i,1,n){
    cin >> t >> d;
    vec.push_back({t,d});
  }
  sort(vec.begin(),vec.end());
  int sum = 0,ans = 0;
  for(auto it : vec){//直接模拟统计答案;
    ans += sum * it.d;
    sum += 2 * it.t;
  }
  cout << ans << '\n';
  return 0;
}