几道简单题的巧妙解题思维-woj 1537

  Problem 1537 - A - Stones I
Time Limit: 1000MS    Memory Limit: 65536KB   
Total Submit: 493   Accepted: 91   Special Judge: No
Description

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 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.

Input
The input consists of one or more test cases.

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. 
Output
For each test case, output the maximum of the sum in one line.
Sample Input
1
100 100
3
2 1
3 1
4 1
0
Sample Output
0
3

题意:一堆石头,每个石头有aibi值,每任意取一个石头,所有石头的ai值都会减去一个当前取的石头的bi值(已取的石头也会减),求能取的最 大值。

三月底做参加这个比赛时,得以晋级完全是坑出来的,整个实验室就我一个理解错题意还蒙对这道题,以及猴子学长直接把另外一道的样咧答案输出 就aAC了。。。

思路 :我当时理解错题意的是所有石头减去各自的bi,如果这样理解的话,这道题就由一道水题变成真·水题。。。
我们先按理解错题意来看这道题,每取一个石头所有石头会减去各自的bi,那么我们从枚举取1到n个石头的情况,当取k个石头时,所取到的每个石头 的最终的 值为ai-k*bi,排个序,然后累加前k个得到sum。每取完一次比较最大值,得到的最大值就是答案。
---------------------------------------------------我是分割线----------------------------------------------------------------------
现在我们来看原题,每取一个石头所有石头会减去当前取的bi,那么当我们取k个石头时,除了累加ai,最后还累减去k*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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值