捞一下水题
  • 板块学术版
  • 楼主__er
  • 当前回复21
  • 已保存回复21
  • 发布时间2022/10/16 10:40
  • 上次更新2023/10/27 07:18:40
查看原帖
捞一下水题
713955
__er楼主2022/10/16 10:40

https://www.luogu.com.cn/discuss/510668

代码更新

#include <bits/stdc++.h>
#define int long long
#define JS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;

struct Node {
	int p, q, num;
} a[100001];

bool cmp(Node x, Node y) {
	return x.num < y.num;
}
int n, tmp, t, sum;

signed main() {
	JS;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].p >> a[i].q;
		a[i].num = a[i].p * a[i].q;
	}
	sort(a + 1, a + 1 + n, cmp);
	if (n % 2 == 0) {
		t = a[n / 2].num > a[n / 2 + 1].num ? a[n / 2].p : a[n / 2 + 1].p;
	}
	if (n % 2 == 1) {
		t = a[n / 2 + 1].p;
	}
	for (int i = 1; i <= n; i++) {
		//cout << abs(t - a[i].p) << " * " << a[i].q << endl << sum << endl << endl;
		sum += abs(t - a[i].p) * a[i].q;
	}
	cout << sum;
	return 0;
}

17pts

2022/10/16 10:40
加载中...