CCPC-Wannafly Winter Camp Day1 (Div2, onsite) J 夺宝奇兵 暴力 贪心

题解

与Codeforces Round #503 C题非常相近
考虑枚举一维k 表示先将宝物数量大于k的人手中宝物全部买下 这时所有人的宝物都小于k 只需要将自己手中宝物数量通过购买最便宜的宝物补足k即可

AC代码

#include 
using namespace std;
typedef long long ll;

const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e3 + 10;

struct node
{
	ll v, i;
	bool operator < (const node &oth) const
	{
		return v < oth.v;
	}
};

vector<node> x; //记录最小价格
vector<node> y[MAXN]; //记录个人物品
bool vis[MAXN];

int main()
{
#ifdef LOCAL
	//freopen("C:/input.txt", "r", stdin);
#endif
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		ll a, c;
		scanf("%lld%lld", &a, &c);
		x.push_back({ a, i }), y[c].push_back({ a, i });
	}
	sort(x.begin(), x.end());
	for (int i = 1; i <= n; i++)
		sort(y[i].begin(), y[i].end());
	ll ans = LINF;
	for (int k = 1; k <= m; k++) //枚举k 先把大于等于k票的买了再买便宜的
	{
		memset(vis, 0, sizeof(vis));
		ll res = 0, cnt = 0;  //购买代价 已购买数
		for (int i = 1; i <= n; i++)
			if (y[i].size() >= k)
				for (int j = 0; j <= y[i].size() - k; j++)
					res += y[i][j].v, vis[y[i][j].i] = 1, cnt++;
		for (int i = 0; i < x.size() && cnt < k; i++) //将购买数量补足k
			if (!vis[x[i].i]) //一个物品只能买一次
				res += x[i].v, cnt++;
		ans = min(ans, res);
	}
	cout << ans << endl;

	return 0;
}

你可能感兴趣的:(贪心,暴力枚举,2019,CCPC-Wannafly,Winter,Camp)