不能用multiset解吗?
查看原帖
不能用multiset解吗?
712509
FatLLion楼主2025/1/12 21:01

鄙人做这题时用了贪心的思想,即每次从小麻糍开始,找最小的不小于它大小两倍的麻糍进行组合,这样子留更多的大麻糍给后面的麻糍。 代码使用multiset实现的,样例能过,但AC45%

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int INF = 1e9;
const int N = 5e5+100;

void read (int &x) {
    int f = 1;x = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9') { x = x*10+ch-'0'; ch = getchar();}
    x *= f;
}

void print (int x) {
    if (x<0) putchar('-'), x = -x;
    if (x<10) putchar(x+'0');
    else print(x/10), putchar(x%10+'0');
}

int n, d[N];
//multiset <int> a;

bool check (int p) {
	for (int i = n-1;p >= 0;i--, p--) 
		if (d[p]*2 > d[i]) return false;
	return true;
}

int main () {
	//freopen("xxx.in", "r", stdin);
	//freopen("xxx.out", "w", stdout);
	read(n);
	for (ll i = 1;i <= n;i++) {
		int tmp; read(tmp);
		a.insert(tmp);
	}
	ll ans = 0;
	while (!a.empty()) {
		auto fr = a.begin();
		int now = *fr, tmp = now*2;
		auto pos = a.lower_bound(tmp);
		if (pos == a.end()) break;
		if (*pos < tmp) break;
		a.erase(fr);
		if (!a.empty()) a.erase(pos), ++ans;
	}
	printf("%lld\n", ans);
	return 0;
}
2025/1/12 21:01
加载中...