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

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(1≤n,m≤100). 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(1≤ai,bi≤100),
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

BestCoder Round #34

思路:简单贪心

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(1≤n,W,H≤50000).n indicate n piles
blocks.

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

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

BestCoder Round #34

这一场真是太爽了,简直无法忍受了,一直错,,,明明想法是好的,每次都有小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;
}
时间: 2024-11-03 22:20:31

HDU - 5190 - Go to movies && 5191 - Building Blocks (BC#34 A,B)的相关文章

HDU 5023 A Corrupt Mayor&#39;s Performance Art(线段树区间更新)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色,有两种操作: P a b c  把区间a到b涂成c颜色 Q a b 查询区间a到b的颜色 线段树区间更新,每个节点保存的信息有,存储颜色的c,30种颜色可以压缩到一个int型里面存储,然后还有一个tot,表示这个区间一共有多少种颜色. 对于P操作,依次往下寻找,找要更新的区间,找到要更新的区间之前

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)

Harry Potter and the Hide Story Problem Description iSea is tired of writing the story of Harry Potter, so, lucky you, solving the following problem is enough. Input The first line contains a single integer T, indicating the number of test cases. Eac

poj 2749 Building roads (二分+拆点+2-sat)

Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 Description Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to drop around, so John wants to build some ro

HDU 2147 kiki&#39;s game kiki的游戏(找P/N状态)

题意:给一个有n*m 个格子的棋盘,将一个硬币放在右上角一格,每次可以往左/下/左下移动一格,碰到不能移动的局面者输. 思路:找P/N状态.先将(n,1)归为P状态,那么能一步到达此位置的有3个位置,分别是其上/右/右上的格子.根据这个规律来找,在整个棋盘的格子上标上P和N.可以发现,棋盘上是有规律的,若提供的n和m皆为奇数,则先手输:否则先手赢. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4

HDU 4883 (BC#2 1001题)TIANKENG’s restaurant(水)

题目地址:HDU 4883 唉..最近得了一种惯性..刚学了什么就碰到个类似的就想用刚学的算法...原来这题如此简单..当时真是想多了.... 只要在需要变化的点上设置一个值,是增多少还是减多少.然后当遍历过来的时候加上或减去这个值就行了... 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>

Building Blocks (hdu 5191)

Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 281    Accepted Submission(s): 59 Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks

hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有n个点和n层,m条边,每一层的任意一个点都可以花费固定的值到下一层或者上一层的任意点 然后m条边链接的点可以花费给出的值进行转移,最后问从i点到n点最少要花费多少. 这题点的个数有100000,层数也是100000,不算额外边暴力建边肯定要爆. 所以可以把层数也当成一个点比如说i点在j层于是n+j就与i链接花费0然后i点可以和上下层任意一个点链接 及i与n+j+1链接i与n+j-1链接

【HDU 1754】I Hate It —— 线段树(数组和指针实现)

原题链接 I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 45627    Accepted Submission(s): 17904 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就