3 2 2 2 3 5
4HintLeLe can buy four tickets with four yuan in cinema 1.
思路:简单贪心
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; }
4 3 2 1 2 3 5 4 4 4 1 2 3 4
1 -1HintIn first case, LeLe move one block from third pile to first pile.
这一场真是太爽了,简直无法忍受了,一直错,,,明明想法是好的,每次都有小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)
原文地址:http://blog.csdn.net/u014355480/article/details/44524337