Codeforces Round #694 BC Strange Birthday Party

C. Strange Birthday Party
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Petya organized a strange birthday party. He invited n friends and assigned an integer ki to the i-th of them. Now Petya would like to give a present to each of them. In the nearby shop there are m unique presents available, the j-th present costs cj dollars (1≤c1≤c2≤…≤cm). It’s not allowed to buy a single present more than once.

For the i-th friend Petya can either buy them a present j≤ki, which costs cj dollars, or just give them cki dollars directly.

Help Petya determine the minimum total cost of hosting his party.

Input
The first input line contains a single integer t (1≤t≤103) — the number of test cases.

The first line of each test case contains two integers n and m (1≤n,m≤3⋅105) — the number of friends, and the number of unique presents available.

The following line contains n integers k1,k2,…,kn (1≤ki≤m), assigned by Petya to his friends.

The next line contains m integers c1,c2,…,cm (1≤c1≤c2≤…≤cm≤109) — the prices of the presents.

It is guaranteed that sum of values n over all test cases does not exceed 3⋅105, and the sum of values m over all test cases does not exceed 3⋅105.

Output
For each test case output a single integer — the minimum cost of the party.

Examples
inputCopy
2
5 4
2 3 4 3 2
3 5 12 20
5 5
5 4 3 2 1
10 40 90 160 250
outputCopy
30
190
inputCopy
1
1 1
1
1
outputCopy
1
Note
In the first example, there are two test cases. In the first one, Petya has 5 friends and 4 available presents. Petya can spend only 30 dollars if he gives

5 dollars to the first friend.
A present that costs 12 dollars to the second friend.
A present that costs 5 dollars to the third friend.
A present that costs 3 dollars to the fourth friend.
5 dollars to the fifth friend.
In the second one, Petya has 5 and 5 available presents. Petya can spend only 190 dollars if he gives

A present that costs 10 dollars to the first friend.
A present that costs 40 dollars to the second friend.
90 dollars to the third friend.
40 dollars to the fourth friend.
10 dollars to the fifth friend.

题目大意是说Petya有一些朋友,也有一些礼物 编号ci(每个只有一个,价格从小到大排),每个朋友有一个数ki,他可以送给朋友小于或等于礼物编号的礼物,或者给他和这个编号的礼物相等数量的金钱。问怎样才能使成本尽量少。

这道题一看就是一道贪心。给每个朋友所花费的钱至多是那一件礼物的金钱,至少是第一件礼物的金钱。那么我们马上能想到的就是给数ki大的人安排小的礼物,而给小的人金钱。但是我们不能马上想到怎样安排礼物才能使总花费尽量小

然后我们又能想到还需要把价值小的礼物尽量送出去,这样对答案肯定是有利的。所以可以考虑将ki大的朋友分配尽可能小的礼物,这样可能是正确答案。

我本来想的是给ki大的分配尽量大的礼物,这样可能可以使礼物利用的多一点,理应小一点。但是并不对。而且很难实现。

//#include<bits/stdc++.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 300010;
long long t, n, m;
long long a[maxn];
long long b[maxn];
int main()
{
	cin >> t;
	while (t--)
	{
		cin >> n >> m;
		long long sum = 0;
		for (long long i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		for (long long i = 1; i <= m; i++)
		{
			cin >> b[i];
		}
		sort(a, a + n);
		//sort(b, b + m);
		int now = 1;
		for (int i = n - 1; i >= 0; i--)
		{
			if (b[a[i]] > b[now] && now <= m) sum += b[now++];
			else sum += b[a[i]];
		}
		cout << sum << endl;
	}
	return 0;
}

为了稳健,加的long long,不加也可能不会错

另外附上B题的代码


#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
long long t, n, x;
long long a[maxn];
long long b[maxn];
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>x;
        long long sum=0;
        for(long long i=0; i<n; i++)
        {
            cin>>a[i];
        }
        while(1)
        {
            int flag=1;
            for(int j=0; j<n; j++)
            {
                if(b[j]%x==0)
                {
                    sum+=a[j];
                    b[j]/=x;
                }
                else
                    {
                    flag=0;
                    break;
                }
            }
            if(!flag) break;
        }
        cout<<sum<<'\n';
    }
    return 0;
}

我以为会超时,结果不会

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值