Xiaoming took the flight MH370 on March 8, 2014 to China to take the ACM contest in WHU. Unfortunately, when the airplane crossing the ocean, a beam of mystical light suddenly lit up the sky and all the passengers with the airplane were transferred to another desert planet.
When waking up, Xiaoming found himself lying on a planet with many precious stones. He found that:
There are n precious stones lying on the planet, each of them has 2 positive values ai and bi. Each time Xiaoming can take the ith of the stones ,after that, all of the stones’ aj (including the stones Xiaoming has taken) will cut down bi units.
Xiaoming could choose arbitrary number (zero is permitted) of the stones in any order. Thus, he wanted to maximize the sum of all the stones he has been chosen. Please help him.
First line of each test case consists of one integer n with 1 <= n <= 1000.
Then each of the following n lines contains two values ai and bi.( 1<= ai<=1000, 1<= bi<=1000)
Input is terminated by a value of zero (0) for n.
100 100
3
2 1
3 1
4 1
0
3
题意:看了一遍两遍不懂。原来就是字面上的意思。小明可以取任意i(0<=i<=n)个石头。但是每个石头的价值会减去i*b[自己]。求取得石头最大价值。
题解:枚举取i=1到n个石子,把每个石头剩余价值按降序排列取前i个,取最大值即可。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
//#define DEBUG
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
#ifdef DEBUG
freopen("cin.txt", "r", stdin);
freopen("cout.txt", "w", stdout);
#endif
int a[1010],b[1010],c[1010],n,i,j;
while (~scanf("%d",&n)&&n)
{
for (i=0;i<n;i++)
scanf("%d%d",&a[i],&b[i]);
int ans=-0x7fffffff;
for (i=1;i<=n;i++)
{
for (j=0;j<n;j++)
c[j]=a[j]-i*b[j];
sort(c,c+n,cmp);
int sum=0;
for (j=0;j<i;j++)
sum+=c[j];
ans=max(ans,sum);
}
printf("%d\n",ans);
}
return 0;
}