15th ZJP Acm - Pro L. Doki Doki Literature Club

Des: 
Doki Doki Literature Club! is a visual novel developed by Team Salvato. The protagonist is invited by his childhood friend, Sayori, to join their high school’s literature club. The protagonist then meets the other members of the club: Natsuki, Yuri, and the club president Monika. The protagonist starts to participate in the club’s activities such as writing and sharing poetry, and grows close to the four girls. What a lovely story!

A very important feature of the game is its poetry writing mechanism. The player is given a list of various words to select from that will make up his poem. Each girl in the Literature Club has different word preferences, and will be very happy if the player’s poem is full of her favorite words.

BaoBao is a big fan of the game and likes Sayori the most, so he decides to write a poem to please Sayori. A poem of mmwords s1,s2,,sms1,s2,…,sm is nothing more than a sequence of mm strings, and the happiness of Sayori after reading the poem is calculated by the formula. 

H=mi=1(mi+1)f(si)H=∑i=1m(m−i+1)⋅f(si)

where  HH  is the happiness and  f(si)f(si)  is Sayori’s preference to the word  sisi . 
Given a list of  nn  words and Sayori’s preference to each word, please help BaoBao select  mm  words from the list and finish the poem with these  mm  words to maximize the happiness of Sayori.

Please note that each word can be used at most once!

Input 
There are multiple test cases. The first line of input contains an integer TT (about 100), indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1mn1001≤m≤n≤100), indicating the number of words and the length of the poem.

For the following nn lines, the ii-th line contains a string consisting of lowercased English letters wiwi (1|wi|151≤|wi|≤15) and an integer f(wi)f(wi) (109f(wi)109−109≤f(wi)≤109), indicating the ii-th word and Sayori’s preference to this word. It’s guaranteed that wiwjwi≠wj for all iji≠j.

Output 
For each test case output one line containing an integer HH and mm strings s1,s2,,sms1,s2,…,sm separated by one space, indicating the maximum possible happiness and the corresponding poem. If there are multiple poems which can achieve the maximum happiness, print the lexicographically smallest one.

Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

A sequence of mm strings a1,a2,,ama1,a2,…,am is lexicographically smaller than another sequence of mm strings b1,b2,,bmb1,b2,…,bm, if there exists a kk (1km1≤k≤m) such that ai=biai=bi for all 1i<k1≤i and akak is lexicographically smaller than bkbk.

A string s1=a1a2axs1=a1a2…ax is lexicographically smaller than another string s2=b1b2bys2=b1b2…by, if there exists a kk (1kmin(x,y)1≤k≤min(x,y)) such that ai=biai=bi for all 1i<k1≤i and ak<bkak, or ai=biai=bi for all 1imin(x,y)1≤i≤min(x,y) and x<yx.


就是一个排序然后输出问题,先按数的大小排序,如果输大小一样,按字符串的大小比较.

看代码

# include 
# include 

struct qwe
{
	char mame[20];
	long long chengji;
};

int main(void)
{
	int t, m, n, i, j;
	struct qwe a[110], y;
	long long sum;
	scanf("%d", &t);
	while (t --)
	{
		scanf("%d %d", &n, &m);
		for (i = 1; i <= n; i ++)
			scanf("%s %lld", a[i].mame, &a[i].chengji);
			
		for (i = 1; i <= n-1; i ++)
			for (j = i+1; j <= n; j ++)
				if (a[i].chengji < a[j].chengji || (a[i].chengji == a[j].chengji && strcmp(a[i].mame, a[j].mame) > 0))
				{
					y = a[i];
					a[i] = a[j];
					a[j] = y;
				}
		sum = 0;
		for (i = 1; i <= m; i ++)
		{
			sum = sum + a[i].chengji*(m-i+1); 
		}
		printf("%lld ", sum);
		for (i = 1; i <= m; i ++)
			if (i == m)
			printf("%s", a[i].mame);
			else
			printf("%s ",a[i].mame);

		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(英文题)