2015南阳CCPC Hdu5547
2016-07-26 10:00
441 查看
D - Pick The Sticks
Time Limit:10000MS Memory Limit:65535KB 64bit IO Format:%I64d
& %I64u
SubmitStatusPracticeHDU
5543
Description
The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting idea and enjoyed
it.
Xiu Yang, one of the cleverest counselors of Cao Cao, understood the command Rather than keep it to himself, he told the point to the whole army. Cao Cao got very angry at his cleverness and would like to punish Xiu Yang. But how can you punish someone because
he's clever? By looking at the chicken rib, he finally got a new idea to punish Xiu Yang.
He told Xiu Yang that as his reward of encrypting the special order, he could take as many gold sticks as possible from his desk. But he could only use one stick as the container.
Formally, we can treat the container stick as an
length segment. And the gold sticks as segments too. There were many gold sticks with different length
and value
.
Xiu Yang needed to put these gold segments onto the container segment. No gold segment was allowed to be overlapped. Luckily, Xiu Yang came up with a good idea. On the two sides of the container, he could make part of the gold sticks outside the container
as long as the center of the gravity of each gold stick was still within the container. This could help him get more valuable gold sticks.
As a result, Xiu Yang took too many gold sticks which made Cao Cao much more angry. Cao Cao killed Xiu Yang before he made himself home. So no one knows how many gold sticks Xiu Yang made it in the container.
Can you help solve the mystery by finding out what's the maximum value of the gold sticks Xiu Yang could have taken?
Input
The first line of the input gives the number of test cases,
.
test cases follow. Each test case start with two integers,
and
,
represents the number of gold sticks and the length of the container stick.
lines follow. Each line consist of two integers,
and
,
represents the length and the value of the
gold stick.
Output
For each test case, output one line containing
Case #x: y, where
is the test case number (starting from 1) and
is the maximum value of the gold sticks Xiu Yang could have taken.
Sample Input
4
3 7
4 1
2 1
8 1
3 7
4 2
2 1
8 4
3 5
4 1
2 2
8 9
1 1
10 3
Sample Output
Case #1: 2
Case #2: 6
Case #3: 11
Case #4: 3
Hint
题意,用一段长度的木棍装一定长度的金块,只要重心在木棍上,就可以放上去,问最多可以装下价值多少的金块,这就是背包问题,用一维背包解决,分三种状态,左右两端和全部的。我用的二维背包错了,心累啊
AC代码:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <stdio.h>
#include <string>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iomanip>
#include<cmath>
#define mod 1000000007
using namespace std;
long long dp[10000][5];
long long w[10000];
int v[10000];
long long max(long long a, long long b)
{
return a > b ? a : b;
}
int main()
{
int t;
int N, L;
scanf("%d", &t);
for (int k = 1; k <= t; k++)
{
memset(dp, 0, sizeof(dp));
memset(w, 0, sizeof(w));
memset(v, 0, sizeof(v));
scanf("%d%d", &N, &L);
long long ans = 0;
for (int j = 0; j < N; j++)
{
scanf("%lld%d", &w[j], &v[j]);
ans = max(ans, v[j]);//对于第四个样例,必须的步骤
}
for (int i = 0; i < N; i++)
for (int j = L * 2; j >= w[i]; j--)//乘以2倍可以在奇数的情况下不用w/2+1
{
for (int kk = 0; kk < 3; kk++)
{
if (j >= w[i] * 2)
dp[j][kk] = max(dp[j][kk], dp[j - w[i] * 2][kk] + v[i]);
if (kk > 0)
{
dp[j][kk] = max(dp[j][kk], dp[j - w[i]][kk - 1] + v[i]);
}
ans = max(dp[j][kk], ans);
}
}
printf("Case #%d: %lld\n", k, ans);
}
}
我的错误二维代码:
#include <iostream>
#include <stdio.h>
#include <string>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iomanip>
#include<cmath>
#define mod 1000000007
using namespace std;
long long dp[1005][1005][3];
long long w[1005];
long long v[1005];
long long max(long long a, long long b)
{
return a > b ? a : b;
}
int main()
{
int t;
int N, L;
scanf("%d", &t);
for (int k = 1; k <= t; k++)
{
memset(dp, 0, sizeof(dp));
scanf("%d%d", &N, &L);
long long ans = 0;
for (int j = 1; j <= N; j++)
{
scanf("%lld%lld", &w[j], &v[j]);
ans = max(ans, v[j]);
}
for (int i = 1; i <= N; i++)
for (int j = L; w[i]&1?j>=(w[i]/2+1):j>=(w[i]/2); j--)
{
for (int kk = 0; kk < 3; kk++)
{
dp[i][j][kk] = dp[i - 1][j][kk];
if (j >= w[i])
{
dp[i][j][kk] = max(dp[i][j][kk], dp[i - 1][j - w[i]][kk] + v[i]);
}
if (kk > 0)
{
if (w[i] & 1)
dp[i][j][kk] = max(dp[i][j][kk], dp[i - 1][j - (w[i] / 2 + 1)][kk - 1] + v[i]);
else
dp[i][j][kk] = max(dp[i][j][kk], dp[i - 1][j - w[i] / 2][kk - 1] + v[i]);
}
ans = max(dp[i][j][kk], ans);
}
}
printf("Case #%d: %lld\n", k, ans);
}
}
Time Limit:10000MS Memory Limit:65535KB 64bit IO Format:%I64d
& %I64u
SubmitStatusPracticeHDU
5543
Description
The story happened long long ago. One day, Cao Cao made a special order called "Chicken Rib" to his army. No one got his point and all became very panic. However, Cao Cao himself felt very proud of his interesting idea and enjoyed
it.
Xiu Yang, one of the cleverest counselors of Cao Cao, understood the command Rather than keep it to himself, he told the point to the whole army. Cao Cao got very angry at his cleverness and would like to punish Xiu Yang. But how can you punish someone because
he's clever? By looking at the chicken rib, he finally got a new idea to punish Xiu Yang.
He told Xiu Yang that as his reward of encrypting the special order, he could take as many gold sticks as possible from his desk. But he could only use one stick as the container.
Formally, we can treat the container stick as an
length segment. And the gold sticks as segments too. There were many gold sticks with different length
and value
.
Xiu Yang needed to put these gold segments onto the container segment. No gold segment was allowed to be overlapped. Luckily, Xiu Yang came up with a good idea. On the two sides of the container, he could make part of the gold sticks outside the container
as long as the center of the gravity of each gold stick was still within the container. This could help him get more valuable gold sticks.
As a result, Xiu Yang took too many gold sticks which made Cao Cao much more angry. Cao Cao killed Xiu Yang before he made himself home. So no one knows how many gold sticks Xiu Yang made it in the container.
Can you help solve the mystery by finding out what's the maximum value of the gold sticks Xiu Yang could have taken?
Input
The first line of the input gives the number of test cases,
.
test cases follow. Each test case start with two integers,
and
,
represents the number of gold sticks and the length of the container stick.
lines follow. Each line consist of two integers,
and
,
represents the length and the value of the
gold stick.
Output
For each test case, output one line containing
Case #x: y, where
is the test case number (starting from 1) and
is the maximum value of the gold sticks Xiu Yang could have taken.
Sample Input
4
3 7
4 1
2 1
8 1
3 7
4 2
2 1
8 4
3 5
4 1
2 2
8 9
1 1
10 3
Sample Output
Case #1: 2
Case #2: 6
Case #3: 11
Case #4: 3
Hint
In the third case, assume the container is lay on x-axis from 0 to 5. Xiu Yang could put the second gold stick center at 0 and put the third gold stick center at 5, so none of them will drop and he can get total 2+9=11 value. In the fourth case, Xiu Yang could just put the only gold stick center on any position of [0,1], and he can get the value of 3.
题意,用一段长度的木棍装一定长度的金块,只要重心在木棍上,就可以放上去,问最多可以装下价值多少的金块,这就是背包问题,用一维背包解决,分三种状态,左右两端和全部的。我用的二维背包错了,心累啊
AC代码:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <stdio.h>
#include <string>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iomanip>
#include<cmath>
#define mod 1000000007
using namespace std;
long long dp[10000][5];
long long w[10000];
int v[10000];
long long max(long long a, long long b)
{
return a > b ? a : b;
}
int main()
{
int t;
int N, L;
scanf("%d", &t);
for (int k = 1; k <= t; k++)
{
memset(dp, 0, sizeof(dp));
memset(w, 0, sizeof(w));
memset(v, 0, sizeof(v));
scanf("%d%d", &N, &L);
long long ans = 0;
for (int j = 0; j < N; j++)
{
scanf("%lld%d", &w[j], &v[j]);
ans = max(ans, v[j]);//对于第四个样例,必须的步骤
}
for (int i = 0; i < N; i++)
for (int j = L * 2; j >= w[i]; j--)//乘以2倍可以在奇数的情况下不用w/2+1
{
for (int kk = 0; kk < 3; kk++)
{
if (j >= w[i] * 2)
dp[j][kk] = max(dp[j][kk], dp[j - w[i] * 2][kk] + v[i]);
if (kk > 0)
{
dp[j][kk] = max(dp[j][kk], dp[j - w[i]][kk - 1] + v[i]);
}
ans = max(dp[j][kk], ans);
}
}
printf("Case #%d: %lld\n", k, ans);
}
}
我的错误二维代码:
#include <iostream>
#include <stdio.h>
#include <string>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iomanip>
#include<cmath>
#define mod 1000000007
using namespace std;
long long dp[1005][1005][3];
long long w[1005];
long long v[1005];
long long max(long long a, long long b)
{
return a > b ? a : b;
}
int main()
{
int t;
int N, L;
scanf("%d", &t);
for (int k = 1; k <= t; k++)
{
memset(dp, 0, sizeof(dp));
scanf("%d%d", &N, &L);
long long ans = 0;
for (int j = 1; j <= N; j++)
{
scanf("%lld%lld", &w[j], &v[j]);
ans = max(ans, v[j]);
}
for (int i = 1; i <= N; i++)
for (int j = L; w[i]&1?j>=(w[i]/2+1):j>=(w[i]/2); j--)
{
for (int kk = 0; kk < 3; kk++)
{
dp[i][j][kk] = dp[i - 1][j][kk];
if (j >= w[i])
{
dp[i][j][kk] = max(dp[i][j][kk], dp[i - 1][j - w[i]][kk] + v[i]);
}
if (kk > 0)
{
if (w[i] & 1)
dp[i][j][kk] = max(dp[i][j][kk], dp[i - 1][j - (w[i] / 2 + 1)][kk - 1] + v[i]);
else
dp[i][j][kk] = max(dp[i][j][kk], dp[i - 1][j - w[i] / 2][kk - 1] + v[i]);
}
ans = max(dp[i][j][kk], ans);
}
}
printf("Case #%d: %lld\n", k, ans);
}
}
相关文章推荐
- AndroidStudio NDK Gradle配置
- CF 219D. Choosing Capital for Treeland 树形DP
- 微信红包海量运营-----“海量之道2.0”(春节发红包如何保证服务器正常运行)
- 笔记3 通过inputstream去读取网络信息
- Python新手学习基础之运算符——位运算
- 国内一线互联网公司内部面试题库
- POJ 1753 Flip Game
- 网络流算法
- JS Array常用方法indexOf/filter/forEach/map/reduce详解
- Web端即时通讯技术盘点:短轮询、Comet、Websocket、SSE
- SSH框架下载地址
- 1044. Shopping in Mars (25)
- 使用python语言操作MongoDB
- 正则表达式
- SQL 2014 架构
- ubuntu 今天在apt-get update的时候报错E: Encountered a section with no Package: header
- 绕过web认证学习总结
- 实现单链表的创建、打印、插入、删除、逆置
- MySQL 5.7.13 主从同步
- As环境下百度地图的使用