Codeforces847K Travel Cards

版权声明:本文为博主原创文章,可以转载,但必须附上原文链接,否则你会终生找不到妹子!!! https://blog.csdn.net/qwerty1125/article/details/78036440

标签:贪心,hash

In theevening Polycarp decided to analyze his today's travel expenses on publictransport.

The bussystem in the capital of Berland is arranged in such a way that each bus runsalong the route between two stops. Each bus has no intermediate stops. So eachof the buses continuously runs along the route from one stop to the other andback. There is at most one bus running between a pair of stops.

Polycarpmade n trips on buses. Abouteach trip the stop where he started the trip and the the stop where he finishedare known. The trips follow in the chronological order in Polycarp's notes.

It isknown that one trip on any bus costs a burles. Incase when passenger makes a transshipment the cost of trip decreases to b burles (b < a). Apassenger makes a transshipment if the stop on which he boards the buscoincides with the stop where he left the previous bus. Obviously, the firsttrip can not be made with transshipment.

Forexample, if Polycarp made three consecutive trips: "BerBank"  "University","University"  "BerMall", "University"  "BerBank", then he payed a + b + a = 2a + bburles.From the BerBank he arrived to the University, where he made transshipment tothe other bus and departed to the BerMall. Then he walked to the University andreturned to the BerBank by bus.

AlsoPolycarp can buy no more than k travelcards. Each travel card costs f burles.The travel card for a single bus route makes free of charge any trip by thisroute (in both directions). Once purchased, a travel card can be used anynumber of times in any direction.

What isthe smallest amount of money Polycarp could have spent today if he can buy nomore than k travelcards?

Input

The firstline contains five integers n, a, b, k, f (1 ≤ n ≤ 300, 1 ≤ b < a ≤ 100, 0 ≤ k ≤ 300, 1 ≤ f ≤ 1000) where:

·  n — the number of Polycarp trips,

·  a — the cost of a regualar single trip,

·  b — the cost of a trip after a transshipment,

·  k — the maximum number of travel cards Polycarpcan buy,

·  f — the cost of a single travel card.

Thefollowing n linesdescribe the trips in the chronological order. Each line contains exactly twodifferent words separated by a single space — the name of the start stop andthe name of the finish stop of the trip. All names consist of uppercase andlowercase English letters and have lengths between 1 to 20 lettersinclusive. Uppercase and lowercase letters should be considered different.

Output

Print thesmallest amount of money Polycarp could have spent today, if he can purchase nomore than k travelcards.

Example

Input

3 5 3 1 8
BerBank University
University BerMall
University BerBank

Output

11

Input

4 2 1 300 1000
a A
A aa
aa AA
AA a

Output

5

Note

In thefirst example Polycarp can buy travel card for the route "BerBank University" and spend 8 burles. Note that his second trip"University"  "BerMall" was made aftertransshipment, so for this trip Polycarp payed 3 burles. So the minimum total sum equals to 8 + 3 = 11 burles.

In thesecond example it doesn't make sense to buy travel cards. Note that each ofPolycarp trip (except the first) was made with transshipment. So the minimumtotal sum equals to 2 + 1 + 1 + 1 = 5 burles.

分析:找使用次数*价值最大的前k个替换成通票,如果不值得换为通票,那么就不换

Hash可以用模板,也可以用C++自带的map,最近真的经常碰到hash题

Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define P(x,y) make_pair(min(x,y),max(x,y))
using namespace std;
const int maxn=305;
 
map<pair<int,int>,int> s;
map<string,int>Map;
int n,x,y,tx,ty,k,f,cnt=0,last=-1,ans=0,c[maxn],num=0;
string a,b;
 
int main()
{
         cin>>n>>x>>y>>k>>f;
         rep(i,1,n){
                 cin>>a>>b;
                 if(!Map[a])Map[a]=++cnt;
                 if(!Map[b])Map[b]=++cnt;
                 int tx=Map[a],ty=Map[b],cost=(tx==last)?y:x;
                 ans+=cost;s[P(tx,ty)]+=cost;last=ty;
         }
         for(map<pair<int,int>,int>::iterator it=s.begin();it!=s.end();it++)
             c[++num]=it->second;
         sort(c+1,c+num+1,greater<int>());
         rep(i,1,k)
             if(c[i]<=f)break;
                 else ans-=c[i]-f;
         cout<<ans<<endl;
         return 0;
}
 



Cards

07-23

Problem DescriptionnGiven some cards each assigned a number, you're required to select EXACTLY K cards among them.nWhile you select a card, I will check the number assigned to it and see if it satisfies some of the following conditions:n1. the number is a prime number;n2. the amount of its divisors is a prime number;n3. the sum of its divisors is a prime number;n4. the product of all its divisors is a perfect square number. A perfect square number is such a kind of number that it can be written as a square of an integer.nThe score you get from this card is equal to the amount of conditions that its number satisfies. The total score you get from the selection of K cards is equal to the sum of scores of each card you select.nAfter you have selected K cards, I will check if there's any condition that has never been satisfied by any card you select. If there is, I will add some extra scores to you for each unsatisfied condition. To make the game more interesting, this score may be negative.nAfter this, you will get your final score. Your task is to figure out the score of each card and find some way to maximize your final score.nNote that 1 is not a prime number. In this problem, we consider a number to be a divisor of itself. For example, considering the number 16, it is not a prime. All its divisors are respectively 1, 2, 4, 8 and 16, and thus, it has 5 divisors with a sum of 31 and a product of 1024. Therefore, it satisfies the condition 2, 3 and 4, which deserves 3 points.n nnInputnThe first line of the input contains the number of test cases T.nEach test case begins with two integers N and K, indicating there are N kinds of cards, and you're required to select K cards among them.nThe next N lines describes all the cards. Each of the N lines consists of two integers A and B, which denote that the number written on this kind of card is A, and you can select at most B cards of this kind.nThe last line contains 4 integers, where the ith integer indicates the extra score that will be added to the result if the ith condition is not satisfied. The ABSOLUTE value of these four integers will not exceed 40000.nYou may assume 0 问答

CARDS

08-08

DescriptionnnAlice and Bob have a set of N cards labelled with numbers 1 ... N (so that no two cards have the same label) and a shuffle machine. We assume that N is an odd integer. nThe shuffle machine accepts the set of cards arranged in an arbitrary order and performs the following operation of double shuffle : for all positions i, 1 <= i <= N, if the card at the position i is j and the card at the position j is k, then after the completion of the operation of double shuffle, position i will hold the card k. nnAlice and Bob play a game. Alice first writes down all the numbers from 1 to N in some random order: a1, a2, ..., aN. Then she arranges the cards so that the position ai holds the card numbered ai+1, for every 1 <= i <= N-1, while the position aN holds the card numbered a1. nnThis way, cards are put in some order x1, x2, ..., xN, where xi is the card at the ith position. nnNow she sequentially performs S double shuffles using the shuffle machine described above. After that, the cards are arranged in some final order p1, p2, ..., pN which Alice reveals to Bob, together with the number S. Bob's task is to guess the order x1, x2, ..., xN in which Alice originally put the cards just before giving them to the shuffle machine. nInputnnThe first line of the input contains two integers separated by a single blank character : the odd integer N, 1 <= N <= 1000, the number of cards, and the integer S, 1 <= S <= 1000, the number of double shuffle operations. nThe following N lines describe the final order of cards after all the double shuffles have been performed such that for each i, 1 <= i <= N, the (i+1)st line of the input file contains pi (the card at the position i after all double shuffles). nOutputnnThe output should contain N lines which describe the order of cards just before they were given to the shuffle machine. nFor each i, 1 <= i <= N, the ith line of the output file should contain xi (the card at the position i before the double shuffles). nSample Inputnn7 4n6n3n1n2n4n7n5nSample Outputnn4n7n5n6n1n2n3 问答

没有更多推荐了,返回首页