Description
给出nn个字符串并给出每个字符串的权值f(si)f(si),要从这nn个串选出个串t1,...,tmt1,...,tm使得H=∑i=1m(m−i+1)⋅f(ti)H=∑i=1m(m−i+1)⋅f(ti)的值最大
Input
第一行一整数TT表示用例组数,每组用例首先输入两个整数,之后输入nn个字符串和对应的权值
Output
输出HH的最大值以及所选出的个串,多种方案取字典序最小的
Sample Input
4
10 8
hello 0
world 0
behind 0
far 1
be 2
spring 10
can 15
comes 20
winter 25
if 200
5 5
collegiate 0
programming -5
zhejiang 10
provincial 5
contest -45
3 2
bcda 1
bcd 1
bbbbb 1
3 2
a 1
aa 1
aaa 1
Sample Output
2018 if winter comes can spring be far behind
15 zhejiang provincial collegiate programming contest
3 bbbbb bcd
3 a aa
Solution
贪心,把所有字符串按权值降序排,相同权值按字典序升序排,选前mm即可
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=105;
struct node
{
char s[16];
int f;
bool operator<(const node&b)const
{
if(f!=b.f)return f>b.f;
return strcmp(s,b.s)<0;
}
}a[maxn];
int T,n,m;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%s%d",a[i].s,&a[i].f);
sort(a+1,a+n+1);
ll sum=0;
for(int i=1;i<=m;i++)sum+=(ll)(m-i+1)*a[i].f;
printf("%lld",sum);
for(int i=1;i<=m;i++)printf(" %s",a[i].s);
printf("\n");
}
return 0;
}

针对一组带有权值的字符串,通过选择其中一部分字符串来最大化特定公式计算得到的总权值。选择过程需遵循一定规则,并确保输出结果符合指定条件。
1041

被折叠的 条评论
为什么被折叠?



