[CSP-S simulation test]: army country X (greedy)

Title Description

$ X $ and $ Y $ country country started the war!
As a military staff $ X $ country, you realize the seriousness of the situation. In order to better deal with the enemy, you collect a $ Y $ country cities $ n $ Messages stronghold, you're going to break this $ n $ strongholds!
Information for each $ i $ stronghold of $ firepower by the coefficient A [i] $, the number of soldiers $ B [i] $ composition, as a staff officer with a superb premeditated ability, of course you can take this analysis of the situation. In fact, you analyze the results, captured a stronghold i, in order to stabilize the morale of one's own soldiers, at least $ B [i] $ soldiers entered the war, after the war there will be a $ A [i] $ soldiers killed in action.
Due to constantly adjust contingent, available soldiers running out, so in the war and not a stronghold of soldiers killed in combat may be other strongholds. You need to calculate the number of soldiers to break this $ n $ strongholds required minimum.
Worse yet, a total of $ T $ cities, so you need the minimum number of soldiers required $ T $ cities sequentially output.


Input Format

The first line an integer $ T $, represents the number of cities.
$ T $ the next set of data. Each line contains a number of data of the first n-$ $, represents the number of positions; n-$ $ next line, wherein the first row comprises two $ I $ numbers indicate the site of the fire coefficient $ A [i] $ and soldiers number $ B [i] $.


Output Format

For each output line of the city, it represents the minimum number of soldiers captured the city needed at all sites.


Sample

Sample input:

2
2
4 7
1 5
3
1 4
4 6
3 5

Sample output:

8
10


Data range and tips

For data before $ 20 \% $ of $ n \ leqslant 9 $
for the data before $ 40 \% $ of $ n \ leqslant 1,000 $
for $ 100 \% $ data $ n \ leqslant 100,000, T \ leqslant 10,1 \ leqslant A [i] \ leqslant B [i ] \ leqslant 1,000,000,000 $.
As more data is read into this question, it is recommended to use a faster way to read.


answer

I wish you all a happy National Day, Happy training!

Obviously we will lose big at the back would be better, then you have $ A $, really nothing to say

Time complexity: $ \ Theta (T \ times n \ log n) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
struct rec{int a,b,delta;}e[100001];
int n;
long long ans,now;
bool cmp(rec a,rec b){return a.delta>b.delta;}
int main()
{
	int T;scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		ans=now=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&e[i].a,&e[i].b);
			e[i].delta=e[i].b-e[i].a;
		}
		sort(e+1,e+n+1,cmp);
		for(int i=1;i<=n;i++)
		{
			if(now<e[i].b)
			{
				ans+=e[i].b-now;
				now=e[i].b;
			}
			now-=e[i].a;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11616056.html