poj 3211 结构体-分组一维dp
2016-02-01 00:12
183 查看
Washing Clothes
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 M strings 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
Sample Output
题意: 俩人洗衣服。。你一件我一件
必须俩人把同一个色的洗完才能洗下一个颜色的。问最小时间是多少
因为有n个颜色的,所以整个背包问题循环n次,每次找寻 最大限度的填满 T/2的方案。T为这个颜色的衣服总时间
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 9462 | Accepted: 3031 |
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 M strings 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
题意: 俩人洗衣服。。你一件我一件
必须俩人把同一个色的洗完才能洗下一个颜色的。问最小时间是多少
因为有n个颜色的,所以整个背包问题循环n次,每次找寻 最大限度的填满 T/2的方案。T为这个颜色的衣服总时间
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int m,n; int dp[1000005]; struct COLOR{ char color[12]; int time[105]; int num; int sum; }one_of_color[12]; void inta() { for(int i=0;i<m;i++){ scanf("%s",one_of_color[i].color); one_of_color[i].sum=0; one_of_color[i].num=0; } int t; char temp[15]; for(int i=0;i<n;i++){ scanf("%d%s",&t,temp); for(int j=0;j<m;j++){ if(!strcmp(temp,one_of_color[j].color)){ one_of_color[j].time[one_of_color[j].num++]=t; one_of_color[j].sum+=t; break; } } } } void deal() { int ans=0; for(int i=0;i<m;i++){ memset(dp,0,sizeof(dp)); int temp=one_of_color[i].sum/2; for(int j=0;j<one_of_color[i].num;j++){ for(int k=temp;k>=one_of_color[i].time[j];k--){ dp[k]=max(dp[k],dp[k-one_of_color[i].time[j]]+one_of_color[i].time[j]); } } ans=ans+one_of_color[i].sum-dp[temp]; } printf("%d\n",ans); } int main() { while(scanf("%d%d",&m,&n),m&&n){ inta(); deal(); } return 0; }
相关文章推荐
- poj 2385 经典dp
- poj3461 KMP
- KMP 模板
- poj 2752 KMPnext[]数组的理解 以及 两种优化
- poj 1961 2406 让你彻底理解KMP的next[]数组的两个题目
- K-Means++ 聚类之数据可视化:使用gnuplot
- 谈谈开发者眼中的编程语言是怎样的?
- 荣耀4CROOT 成功!附本人ROOT过程——KINGROOT
- kali linux: /android-sdk-linux/platform-tools/adb: 没有那个文件或目录
- 美国罗斯福纪念公园墙上刻着的一段总统名言
- CentOS 6.2 下安装QQ2012完全可用
- 3.3.3 改变目标(运行级别)—— RHEL7 / CentOS7
- 正则表达式pcre在Android下的移植
- repo—清华TUNA镜像源—Android (AOSP)—CyanogenMod Android for Motorola Defy
- Linaro公司基于GCC推出的的ARM交叉编译工具
- 美国海军采购Linux系统去引导无人机舰队
- systemtap 在Ubuntu上安装
- Debian / Ubuntu Linux: Setup Wireless Access Point (WAP) with Hostapd
- (OK) android——Repo sync cm—解决—error
- linux设备驱动归纳总结(三):2.字符型设备的操作open、close、read、write