Fight Against Monsters (2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest)(The 2019 Asia Yinchuan First Round Online Programming)

It is my great honour to introduce myself to you here. My name is Aloysius Benjy Cobweb Dartagnan Egbert Felix Gaspar Humbert Ignatius Jayden Kasper Leroy Maximilian. As a storyteller, today I decide to tell you and others a story about the hero Huriyyah, and the monsters.

Once upon a time, citizens in the city were suffering from nn powerful monsters. They ate small children who went out alone and even killed innocent persons. Before the hero appeared, the apprehension had overwhelmed the people for several decades.

For the good of these unfortunate citizens, Huriyyah set off to the forest which was the main lair of monsters and fought with nn fierce and cruel monsters. The health point of the ii-th monster was HPiHPi, and its attack value was ATKiATKi.

They fought in a cave through a turn-based battle. During each second, the hero Huriyyah was attacked by monsters at first, and the damage was the sum of attack values of all alive monsters. Then he selected a monster and attacked it. The monster would suffer the damage of kk (its health point would decrease by kk) which was the times of attacks it had been came under. That is to say, for each monster, the damage of the first time that Huriyyah attacked it was 11, and the damage of Huriyyah's second attack to this monster was 22, the third time to this monster was 33, and so on. If at some time, the health point of a monster was less than or equal to zero, it died. The hero won if all monsters were killed.

Now, my smart audience, can you calculate the minimum amount of total damages our hero should suffer before he won the battle?

Input

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 103103.

For each test case, the first line contains an integers n (1n105)n (1≤n≤105) which is the number of monsters.

The ii-th line of the following nn lines contains two integers HPiHPi and ATKi (1HPi,ATKi105)ATKi (1≤HPi,ATKi≤105) which describe a monster.

We guarantee that the sum of nn in all test cases is up to 106106.

Output

For each test case, output a line containing Case #x: y, where x is the test case number starting from 11, and y is the minimum amount of total damages the hero should suffer.

Example
input
Copy
2
3
1 1
2 2
3 3
3
3 1
2 2
1 3
output
Copy
Case #1: 19
Case #2: 14


分析:要是受到的伤害最低,那么只要Kill小怪兽的顺序最优就可以啦,怎样最优呢?最优的影响因素是 kill 小怪兽的时间,而时间又受到伤害的影响
   所以呢,我们排序的时候就判断先kill A怪兽再kill B怪兽所受到的伤害高还是先kill B怪兽再kill A怪兽受到的伤害高
PS:有队友提出优先队列做,这里提供一下思路,有兴趣的可以搞一下



#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define pq priority_queue<int>
#define pql priority_queue<ll>
#define pqn priority_queue<node>
#define v vector<int>
#define vl vector<ll>
#define lson rt<<1, l, m  
#define rson rt<<1|1, m+1, r
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define no printf("NO\n");
#define gcd __gcd
#define cn cin>>
#define ct cout<<
#define ed <<endl;
#define ok return 0 ;
#define over cout<<endl;
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++)  {cin>>a[i] ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define re return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<"     ++++++      "<<endl; 
typedef long long ll;
const int INT=1e6+5;
const int maxn=10000+5;
const int len = 1e5+5;

typedef struct node
{
    ll x,y,ankle,T;
}node;
//double getlen(node xx,node yy) { return sqrt( (xx.x-yy.x)*(xx.x-yy.x) +(xx.y-yy.y)*(xx.y-yy.y) ); } //计算两点间距离 
node dp[len]; 
int cmp(node a,node b)
{
  /* 可以加可以不加
if(b.y*a.T == a.y*b.T) return a.y>b.y;
  */
return a.y*b.T > b.y*a.T ; } int runtime(int num) { int k=1,t=0; while(num>0) { t++; num-=k; k++; } return t; } int main() { int t,n,k; cin>>t; for(int kk=1;kk<=t;kk++) { cin>>n; ll cnt=0,sum=0; rep(0,n-1) { cin>>dp[i].x>>dp[i].y; dp[i].T = runtime(dp[i].x) ; cnt+=dp[i].y; } sort(dp,dp+n,cmp); rep(0,n-1) { sum+=cnt*dp[i].T; cnt-=dp[i].y; } cout<<"Case #"<<kk<<": "<<sum<<endl; } ok; }

扫描二维码关注公众号,回复: 7141248 查看本文章

猜你喜欢

转载自www.cnblogs.com/Shallow-dream/p/11440046.html