hdu 4105 贪心思想

淋漓尽致的贪心思想

波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的。

当在寻找波谷碰到零了就自然当成波谷。

当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到。

代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>

using namespace std;

char data[5999];

int main()
{
	int n, m, k;

	while(scanf("%d", &n) != EOF)
	{
		scanf("%s", data);
		//cout<<data<<endl;
		int a, b, tag = 0;
		a = 11;
		b = 0;
		int ans = 0;
		for(int i = 0; i < n; i ++)
		{
			b = (data[i] - '0');
			if(tag % 2 == 0){
				if(b < a){
					a = b;
				}
				else
				{
					i ++;
					a = data[i]-'0';
				}
			}
			else
			{
				if(b > a)
				{
					a = b;
				}
				else
				{
					if(b == 0)
					{
						while(data[i] == '0'){
							i ++;
							if(i >= n) break;
						}
						//贪心思想,有0就一定让他做波谷,把原先的波谷a给到他的前一个波峰上
						a = 0;	//0做波谷
						b = data[i]-'0';
						a = b;
					}
					else
					{
						i ++;
						a = b*10 + (data[i] - '0');
					}
				}
			}
			if(i >= n) break;
			ans ++; tag ++;
		}
		printf("%d\n", ans-1);
	}

	return 0;
}

hdu 4105 贪心思想,布布扣,bubuko.com

时间: 2024-10-12 12:17:33

hdu 4105 贪心思想的相关文章

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

POJ 3253 Fence Repair 类似哈夫曼树的贪心思想

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24550   Accepted: 7878 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)

NOIP2012BLOCKADE贪心思想证明

这道题的做法是二分时间并检验这个时间是否可行.检验的方法要用到贪心思想. 对于不能到根结点的军队应该尽量向根结点走. 如果军队A能走到根结点但到根结点后剩余的时间不够返回根结点的儿子B,应该让军队A守B. 否则把军队A加入优先队列,用剩余时间小的军队匹配到根结点距离小的儿子. 证明: 一个军队可控制自己和以自己为根的子树.越向上走控制的结点越多. 把这个军队记作A,如果不这样,我们必须要用另一个到达根结点的军队,记作C,因为A剩余时间小于到B的时间,如果用C,则C剩余的时间大于到B的时间,这样交

POJ3544 Journey with Pigs 动规基础贪心思想

非常经典的贪心题目,没有严格证明的话,肯定是YY着做的,题意: 约翰要从A到B,途中会经过N个村庄,他会带N只猪,然后卖掉,每个村庄卖一只,第i个村庄的人出价pi 每斤,从A到第i个存在的距离为disi,而且运一只猪需要话花费t * disi每斤,t一开始会给定 输入第一行n,t 接下来第一行 n个数,代表各个猪的重量 在接下来第二行n个数,代表每个村庄距离A的距离dis 在接下来第三行n个数,代表每个村庄出价p 输出n个数,表示每个村庄买的哪只猪 这题目一开始看到就往dp方向去想,但是发现不行

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem Description WANGPENG is a freshman. He is requested to have a physical examination when entering the university. Now WANGPENG arrives at the hospital. Er-.. There are so many students, and the nu

hdu 1050(贪心算法)

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19278    Accepted Submission(s): 6582 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a