Kiil the monster (cost problem

# Meaning of the title
of n monsters, each monster has atk attack hp output and value every second hero will be subject to all surviving monster attack, attack each monster hero damage equal to the number of times the current monster's attack, the first attack on i-1 is the damage the monster, the second after 2 calculates the hero kill all monsters withstand attack minimum
data range:
T: 1E3
n-: (1,1e5)
n-and <1e6 of all test data
Hp, atk [1, 1e5]

# Solution to a problem   
the general idea: first Gaosi high damage
of n is the greatest monster monster every 1e5, the sum will burst int, long long use
each monster's calculated survival time, calculate its average damage
according to the average damage was a first key, the second key attack is sorted in descending
ratio has sorting precision errors, it can be changed so that: a / b> c / d 

. 1  #pragma the GCC Optimize (. 3, "Ofast", "inline")
 2 #include <bits / STDC ++ H.>
 . 3  #define LL Long Long
 . 4  the using  namespace STD;
 . 5  const  int N = 1E5 + 10 ;
 . 6  struct Monster {
 . 7      HP LL;
 . 8      LL ATK;
 . 9      LL Alive; // all pre monsters be attacked from the start time to survive 
10      Double average; // mean survival time of the attack 
. 11  } Mon [N];
 12 is  inline Alive LL (LL X) {
 13 is      LL Tim, RES = 0 ;
14     for (int i = 1;; ++i){
15         res+=i;
16         if(res>=x){
17             tim=i;
18             break;
19         }
20     }
21     return tim;
22 }
23 inline bool cmp(struct monster A,struct monster B){
24     if(A.average==B.average)
25         return A.atk>B.atk;
26     return A.average>B.average;
27 }
28 int main(){
29     ios::sync_with_stdio(0);
30     cin.tie(0);
31     cout.tie(0);
32     int T;
33     cin>>T;
34     LL sum_atk;
35     for (int i = 1; i <=T ; ++i) {
36         int n;
37         cin>>n;
38         sum_atk =0;
39         for (int i = 1; i <=n ; ++i) {
40             cin>>mon[i].hp>>mon[i].atk;
41             mon[i].alive= Alive(mon[i].hp);
42             mon[i].average = mon[i].atk*1.0/mon[i].alive;
43             sum_atk+=mon[i].atk;
44         }
45         LL hurt=0;
46         sort(mon+1,mon+n+1,cmp);
47         for (int i = 1; i <=n ; ++i) {
48             hurt+=sum_atk*mon[i].alive;
49             sum_atk-=mon[i].atk;
50         }
51         cout<<"Case #"<<i<<": "<<hurt<<endl;
52     }
53     return 0;
54 }

 

 

Guess you like

Origin www.cnblogs.com/hhyx/p/12543869.html