码迷,mamicode.com
首页 > Windows程序 > 详细

HDU - 5190 - Go to movies && 5191 - Building Blocks (BC#34 A,B)

时间:2015-03-22 09:12:03      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:acm   hdu   贪心   bestcoder   

Go to movies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 99    Accepted Submission(s): 67


Problem Description
Winter holiday is coming!As the monitor, LeLe plans to go to the movies.
Because the winter holiday tickets are pretty expensive, LeLe decideds to try group-buying.
 

Input
There are multiple test cases, about 20 cases. The first line of input contains two integers n,m(1n,m100)n indicates the number of the students. mindicates how many cinemas have offered group-buying.

For the m lines,each line contains two integers ai,bi(1ai,bi100), indicating the choices of the group buying cinemas offered which means you can use biyuan to buy ai tickets in this cinema.
 

Output
For each case, please help LeLe **choose a cinema** which costs the least money. Output the total money LeLe should pay.
 

Sample Input
3 2 2 2 3 5
 

Sample Output
4
Hint
LeLe can buy four tickets with four yuan in cinema 1.
 

Source
 

思路:简单贪心


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream> 
#include <algorithm>
#include <cmath>
#define INF 0x3fffffff
using namespace std;

struct node {
	int a, b;
}dian[105];

int main() {
	int n, m;
	while(scanf("%d %d", &n, &m) != EOF) {
		for(int i = 0; i < m; i++) {
			scanf("%d %d", &dian[i].a, &dian[i].b);
		}
		
		int ans = INF;
		for(int i = 0; i<m; i++) {
			int t = n / dian[i].a;
			if(n % dian[i].a) t++;
			if(t * dian[i].b < ans) ans = t * dian[i].b;
		}
		printf("%d\n", ans);
	}
	return 0;
} 






Building Blocks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 491    Accepted Submission(s): 106


Problem Description
After enjoying the movie,LeLe went home alone. LeLe decided to build blocks.
LeLe has already built n piles. He wants to move some blocks to make W consecutive piles with exactly the same height H.

LeLe already put all of his blocks in these piles, which means he can not add any blocks into them. Besides, he can move a block from one pile to another or a new one,but not the position betweens two piles already exists.For instance,after one move,"3 2 3" can become "2 2 4" or "3 2 2 1",but not "3 1 1 3".

You are request to calculate the minimum blocks should LeLe move.
 

Input
There are multiple test cases, about 100 cases.

The first line of input contains three integers n,W,H(1n,W,H50000).n indicate n piles blocks.

For the next line ,there are n integers A1,A2,A3,,An indicate the height of each piles. (1Ai50000)

The height of a block is 1.
 

Output
Output the minimum number of blocks should LeLe move.

If there is no solution, output "-1" (without quotes).
 

Sample Input
4 3 2 1 2 3 5 4 4 4 1 2 3 4
 

Sample Output
1 -1
Hint
In first case, LeLe move one block from third pile to first pile.
 

Source
 



这一场真是太爽了,简直无法忍受了,一直错,,,明明想法是好的,每次都有小BUG,我他妈真是醉了,写得真挫,


思路:其实很简单,就是算出长度为W的区间的正数之和n和负数之和,负数之和再取个绝对值得m,取max(n,m)为MIN,再往后扫每一段长度为W的区间,取其中最小值


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define INF 0x7fffffff
using namespace std;

typedef long long LL;
const int maxn = 50005;
LL mu[maxn * 3];
LL zhen;
LL fu; 

int main() {
	int n, W, H;
	while(scanf("%d%d%d", &n, &W, &H) != EOF) {
		LL sum = 0;
		for(int i = W; i < W + n; i++) {
			scanf("%I64d", &mu[i]);
			sum += mu[i];
		}
		if(sum < LL(H) * W) {
			printf("-1\n");
		}
		else 
		{
			LL MIN, m = n + W * 2;
			for(int i = 0; i < W; i++) mu[i] = 0;
			for(int i = W + n; i < m; i++) mu[i] = 0;
			zhen = 0; fu = LL(H) * W;
			MIN = max(zhen, fu);
			for(int i = W; i < m; i++) {
				if(mu[i - W] < H) fu -= (H - mu[i - W]);
				else zhen -= mu[i - W] - H;
				if(mu[i] < H) fu += (H - mu[i]);
				else zhen += mu[i] - H;
				MIN = min(MIN, max(zhen, fu));
			}
			printf("%I64d\n", MIN);
		}
	}
	return 0;
}










一直让我魂牵梦绕调BUG的代码(尼玛!):

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define INF 0x7fffffff
using namespace std;

typedef long long LL;
const int maxn = 50005;
LL mu[maxn * 3];
LL zhen;
LL fu; 

int main() {
    int n, W, H;
    while(scanf("%d%d%d", &n, &W, &H) != EOF) {
        LL sum = 0;
        memset(mu, 0, sizeof(mu));
        for(int i = W; i < W + n; i++) {
            scanf("%I64d", &mu[i]);
            mu[i] -= H;
            sum += mu[i];//其实这里可以放上面,我就不用找的这么辛苦了。。。哎。。。代码能力。 
        }
        if(sum < 0) {			//错误!这里sum值不是判断其小于0,而是判断sum+n*H<W*H,
            printf("-1\n");		//我靠,劳资就是这里被自己误导的,
        }						//写程序真的是要严谨严谨再严谨啊!!!!
        else 
        {
            LL MIN, m = n + W * 2;
            for(int i = 0; i < W; i++) mu[i] -= H;
            for(int i = W + n; i < m; i++) mu[i] -= H;
            zhen = 0; fu = LL(H) * W;
            MIN = max(zhen, fu);
            for(int i = W; i < m; i++) {
                if(mu[i - W] < 0) fu -= (-mu[i - W]);
                else zhen -= mu[i - W];
                if(mu[i] < 0) fu += (-mu[i]);
                else zhen += mu[i];
                MIN = min(MIN, max(zhen, fu));
            }
            printf("%I64d\n", MIN);
        }
    }
    return 0;
}










HDU - 5190 - Go to movies && 5191 - Building Blocks (BC#34 A,B)

标签:acm   hdu   贪心   bestcoder   

原文地址:http://blog.csdn.net/u014355480/article/details/44524337

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!