Problem 1537 - A - Stones I
Total Submit: 493 Accepted: 91 Special Judge: No
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
题意:一堆石头,每个石头有ai与bi值,每任意取一个石头,所有石头的ai值都会减去一个当前取的石头的bi值(已取的石头也会减),求能取的最 大值。
三月底做参加这个比赛时,得以晋级完全是坑出来的,整个实验室就我一个理解错题意还蒙对这道题,以及猴子学长直接把另外一道的样咧答案输出 就aAC了。。。
思路 :我当时理解错题意的是所有石头减去各自的bi,如果这样理解的话,这道题就由一道水题变成真·水题。。。AC代码
<span style="font-size:18px;"><span style="font-size:12px;">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n;
struct node
{
int a;
int b;
int c;
}line[1111];
bool cmp(node x,node y)
{
return x.c>y.c;
}
int main()
{
int i,j;
int max;
int sum;
while(scanf("%d",&n)!=EOF&&n!=0)
{
sum=max=0;
for(i=0;i<n;++i)
{
scanf("%d %d",&line[i].a,&line[i].b);
}
for(i=1;i<=n;++i)
{
for(j=0;j<n;++j)
{
line[j].c=line[j].a-line[j].b*i;
}
sort(line,line+n,cmp);
sum=0;
for(j=0;j<i;++j)
{
sum+=line[j].c;
}
max=(sum>max)?sum:max;
}
printf("%d\n",max);
}
return 0;
}</span></span>