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 m words s 1 , s 2 , , s m is nothing more than a sequence of m strings, and the happiness of Sayori after reading the poem is calculated by the formula.

H = i = 1 m ( m i + 1 ) f ( s i )

where H is the happiness and f ( s i ) is Sayori’s preference to the word s i .
Given a list of n words and Sayori’s preference to each word, please help BaoBao select m words from the list and finish the poem with these m 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 T (about 100), indicating the number of test cases. For each test case:

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

For the following n lines, the i -th line contains a string consisting of lowercased English letters w i ( 1 | w i | 15 ) and an integer f ( w i ) ( 10 9 f ( w i ) 10 9 ), indicating the i -th word and Sayori’s preference to this word. It’s guaranteed that w i w j for all i j .

Output
For each test case output one line containing an integer H and m strings s 1 , s 2 , , s m 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 m strings a 1 , a 2 , , a m is lexicographically smaller than another sequence of m strings b 1 , b 2 , , b m , if there exists a k ( 1 k m ) such that a i = b i for all 1 i < k and a k is lexicographically smaller than b k .

A string s 1 = a 1 a 2 a x is lexicographically smaller than another string s 2 = b 1 b 2 b y , if there exists a k ( 1 k min ( x , y ) ) such that a i = b i for all 1 i < k and a k < b k , or a i = b i for all 1 i min ( x , y ) and x < y .

题目传送门

/*
* Doki Doki Literature Club
* 这题就是废话多,就两句话有用,最后注意下爆int的子项
*/

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
#define len 102
struct data{
    string _s;
    int key;
}map[102];

int cmp(data _a,data _b)
{

    if( _a.key == _b.key)   return _a._s < _b._s;   // 比赛的时候抽了 自己重写了 '<'
    return _a.key > _b.key ;
}

int main()
{
    int n,m;
    int test;
    //模版可忽略
    static_cast<void>(ios::sync_with_stdio(false)),cin.tie(0);

    cin>>test;

    while(test--)
    {
        cin>>n>>m;
        long long ans = 0;
        for(int i=1;i<=n;++i)
        {
            cin>>map[i]._s>>map[i].key;
        }

        sort(map+1, map+n+1, cmp);

        for (long i =1; i<=m; ++i) {
            ans += (m-i+1)*map[i].key;
        }

        cout<<ans;

        for(int i=1;i<=m;++i)
        {
            cout<<" "<< map[i]._s ;
        }
        cout<<endl;
    }
    return 0;
}

Created By LosReturn.

猜你喜欢

转载自blog.csdn.net/qq_31979619/article/details/80154100