poj3211Washing Clothes(字符串处理+01背包) hdu1171Big Event in HDU(01背包)
题目链接:
这个题目比1711难处理的是字符串怎样处理,所以我们要想办法,自然而然就要想到用结构体存储。所以最后将全部的衣服分组,然后将每组时间减半,看最多能装多少。最后求最大值。那么就非常愉快的转化成了一个01背包问题了。。。
。
hdu1711是说两个得到的价值要尽可能的相等。所以还是把全部的价值分为两半。最后01背包,那么这个问题就得到了解决。。
题目:
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 8637 | Accepted: 2718 |
Description
Dearboy was so busy recently that now he has piles of clothes to wash. Luckily, he has a beautiful and hard-working girlfriend to help him. The clothes are in varieties of colors but each piece of them can be seen as of only one color. In order to prevent
the clothes from getting dyed in mixed colors, Dearboy and his girlfriend have to finish washing all clothes of one color before going on to those of another color.
From experience Dearboy knows how long each piece of clothes takes one person to wash. Each piece will be washed by either Dearboy or his girlfriend but not both of them. The couple can wash two pieces simultaneously. What is the shortest possible time they
need to finish the job?
Input
The input contains several test cases. Each test case begins with a line of two positive integers M and N (M < 10, N < 100), which are the numbers of colors and of clothes. The next line contains Mstrings which
are not longer than 10 characters and do not contain spaces, which the names of the colors. Then follow N lines describing the clothes. Each of these lines contains the time to wash some piece of the clothes (less than 1,000) and its color. Two zeroes
follow the last test case.
Output
For each test case output on a separate line the time the couple needs for washing.
Sample Input
3 4
red blue yellow
2 red
3 blue
4 blue
6 red
0 0
Sample Output
10
Source
代码为:
#include<cstdio>
#include<map>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=100000+10;
int dp[maxn];
struct clothes
{
int num;//颜色同样的衣服的编号
int sum;//颜色形同的衣服的总数
char color[100];//颜色
int time[105];//颜色同样的不同衣服的时间
}clo[10+10];
int main()
{
int m,n,u,max_pack,ans;
char str[100+10];
while(~scanf("%d%d",&m,&n))
{
if(n==0&&m==0) return 0;
for(int i=1;i<=m;i++)
{
scanf("%s",clo[i].color);
clo[i].num=1;
clo[i].sum=0;
}
for(int i=1;i<=n;i++)
{
scanf("%d%s",&u,str);
for(int j=1;j<=m;j++)
{
if(strcmp(str,clo[j].color)==0)
{
int tmp=clo[j].num;
clo[j].time[tmp]=u;
clo[j].sum=clo[j].sum+u;
clo[j].num++;
}
}
}
for(int i=1;i<=m;i++)
clo[i].num--;
ans=0;
for(int i=1;i<=m;i++)
{
memset(dp,0,sizeof(dp));
max_pack=clo[i].sum/2;
for(int j=1;j<=clo[i].num;j++)
for(int k=max_pack;k>=clo[i].time[j];k--)
dp[k]=max(dp[k],dp[k-clo[i].time[j]]+clo[i].time[j]);
ans=ans+max(dp[max_pack],clo[i].sum-dp[max_pack]);
}
cout<<ans<<endl;
}
return 0;
}
#include<map>
#include<iostream>
#include<cstring>
using namespace std; const int maxn=100000+10;
int dp[maxn]; struct clothes
{
int num;//颜色同样的衣服的编号
int sum;//颜色形同的衣服的总数
char color[100];//颜色
int time[105];//颜色同样的不同衣服的时间
}clo[10+10]; int main()
{
int m,n,u,max_pack,ans;
char str[100+10];
while(~scanf("%d%d",&m,&n))
{
if(n==0&&m==0) return 0;
for(int i=1;i<=m;i++)
{
scanf("%s",clo[i].color);
clo[i].num=1;
clo[i].sum=0;
}
for(int i=1;i<=n;i++)
{
scanf("%d%s",&u,str);
for(int j=1;j<=m;j++)
{
if(strcmp(str,clo[j].color)==0)
{
int tmp=clo[j].num;
clo[j].time[tmp]=u;
clo[j].sum=clo[j].sum+u;
clo[j].num++;
}
}
}
for(int i=1;i<=m;i++)
clo[i].num--;
ans=0;
for(int i=1;i<=m;i++)
{
memset(dp,0,sizeof(dp));
max_pack=clo[i].sum/2;
for(int j=1;j<=clo[i].num;j++)
for(int k=max_pack;k>=clo[i].time[j];k--)
dp[k]=max(dp[k],dp[k-clo[i].time[j]]+clo[i].time[j]);
ans=ans+max(dp[max_pack],clo[i].sum-dp[max_pack]);
}
cout<<ans<<endl;
}
return 0;
}
hdu1171 题目:
Big Event in HDU
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23302 Accepted Submission(s): 8206
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is
N (0<N<1000) kinds of facilities (different value, different kinds).
facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
2
10 1
20 1
3
10 1
20 2
30 1
-1
20 10
40 40
代码为
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=250000+10;
int dp[maxn];
int sum[50+10],val[50+10];
int kind[5000+10];
int main()
{
int n,max_pack,ans,cal,Max;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
if(n<=0) return 0;
cal=1;
max_pack=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&val[i],&sum[i]);
max_pack+=sum[i]*val[i];
for(int j=1;j<=sum[i];j++)
{
kind[cal]=val[i];
cal++;
}
}
cal--;
Max=max_pack/2;
for(int i=1;i<=cal;i++)
for(int j=Max;j>=kind[i];j--)
dp[j]=max(dp[j],dp[j-kind[i]]+kind[i]);
ans=max(dp[Max],max_pack-dp[Max]);
cout<<ans<<" "<<max_pack-ans<<endl;
}
return 0;
}
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn=250000+10;
int dp[maxn]; int sum[50+10],val[50+10];
int kind[5000+10]; int main()
{
int n,max_pack,ans,cal,Max;
while(~scanf("%d",&n))
{
memset(dp,0,sizeof(dp));
if(n<=0) return 0;
cal=1;
max_pack=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&val[i],&sum[i]);
max_pack+=sum[i]*val[i];
for(int j=1;j<=sum[i];j++)
{
kind[cal]=val[i];
cal++;
}
}
cal--;
Max=max_pack/2;
for(int i=1;i<=cal;i++)
for(int j=Max;j>=kind[i];j--)
dp[j]=max(dp[j],dp[j-kind[i]]+kind[i]);
ans=max(dp[Max],max_pack-dp[Max]);
cout<<ans<<" "<<max_pack-ans<<endl;
}
return 0;
}
poj3211Washing Clothes(字符串处理+01背包) hdu1171Big Event in HDU(01背包)的更多相关文章
- hdu1171Big Event in HDU(01背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1171 Big Event in HDU (01背包, 母函数)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HUD 1171 Big Event in HDU(01背包)
Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...
- HDU1171--Big Event in HDU(多重背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1171 Big Event in HDU 多重背包二进制优化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...
- HDU1171-Big Event in HDU
描述: Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don ...
- HDU 1171 Big Event in HDU dp背包
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...
- HDU - 1171 Big Event in HDU 多重背包
B - Big Event in HDU Nowadays, we all know that Computer College is the biggest department in HDU. B ...
- HDU 1171 Big Event in HDU (多重背包变形)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- MFC_简易文件管理器
练习_简易文件管理器 Edit1编辑框绑定变量,初始化内容 m_EditCtrl = L"D:\"; 添加List控件,属性设置report,OnInitDialog()函数里添加 ...
- day21-5 类的多态与多态性
类的多态与多态性 多态 多态指的是一类事物有多种形态,如动物有多种形态:人.狗.猪 import abc class Animal(metaclass=abc.ABCMeta): # 同一类事物:动物 ...
- 第2节 mapreduce深入学习:14、mapreduce数据压缩-使用snappy进行压缩
第2节 mapreduce深入学习:14.mapreduce数据压缩-使用snappy进行压缩 文件压缩有两大好处,节约磁盘空间,加速数据在网络和磁盘上的传输. 方式一:在代码中进行设置压缩 代码: ...
- java was started but returned exit code =-805306369的处理方法
Myeclipse出现java was started but returned exit code =-805306369的错误,如图: 解决方法: 换个workspaces:换个工作目录,估计估计 ...
- JAVA基础——内存流
掌握内存操作流 输入和输出都是从文件中来的,当然,也可将输出的位置设置在内存上,这就需要ByteArrayInputStream和ByteArrayOutputStream ByteArrayInpu ...
- Oracle数据库单表循环提取输出
现在有如下的表,名称为Test表: ydid sws_dm sws_mc ry_dm ry_mc 1 1 ...
- Android获取屏幕的大小与密度的代码
Android项目开发中很多时候需要获取手机屏幕的宽高以及屏幕密度来进行动态布局,这里总结了三种获取屏幕大小和屏幕密度的方法 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- GNU编译器学习 --> 如何链接外部库【Linking with external libraries】
库也就是我们常说的library,一个库是若干个已经编译过的目标文件(.obj)的集合,它可以被链接到程序里.那么我们最常见的使用就是,我们在编程时会调用一些函数,这些函数别人已经写好了,它就放在库里 ...
- 笔试算法题(40):后缀数组 & 后缀树(Suffix Array & Suffix Tree)
议题:后缀数组(Suffix Array) 分析: 后缀树和后缀数组都是处理字符串的有效工具,前者较为常见,但后者更容易编程实现,空间耗用更少:后缀数组可用于解决最长公共子串问题,多模式匹配问题,最长 ...
- Jquery 动态添加元素后,获取不到元素对象情况