UVA - 812 Trade on Verweggistan dfs + 贪心
来源:互联网 发布:mac os qq截图 编辑:程序博客网 时间:2024/04/27 14:35
题目大意:有n堆东西,只能从上往下买,如果要买下面的东西,只能把上面的所有东西买了才可以,类似一个栈
每个东西都有价格,每个东西卖出去都只能卖10,问能卖的最大利润,和要卖多少个才能达到最大利润
解题思路:贪心的来说每个买了的都要卖出去,才能使得利润最大化,纪录每堆卖出最大利润的所有情况,然后用dfs一一枚举,要判重和排序输出
注意特例,都是0的情况
1
5 10 10 10 10 10
输出应该有0 1 2 3 4 5
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define maxn 60int w, price[maxn][25], sum, num[maxn], cnt, vis[2500],ans[2500];struct Max { int cnt; int M[25]; int num;}MAX[maxn];void init() { int Max = 0, sum2; sum = 0; cnt = 0; memset(vis,0,sizeof(vis)); for(int i = 0; i < w; i++) { scanf("%d", &num[i]); sum += Max; sum2 = Max = 0; for(int j = 0; j < num[i]; j++) { scanf("%d", &price[i][j]); sum2 += 10 - price[i][j]; Max = max(Max, sum2); } MAX[i].num = Max; MAX[i].cnt = 0; } sum += Max; for(int i = 0; i < w; i++) { sum2 = 0; if(MAX[i].num == 0) MAX[i].M[MAX[i].cnt++] = 0; for(int j = 0; j < num[i]; j++) { sum2 += 10 -price[i][j]; if(sum2 == MAX[i].num) MAX[i].M[MAX[i].cnt++] = j + 1; } }}void dfs(int Sum, int cur) { if(cur == w && !vis[Sum]) { ans[cnt++] = Sum; vis[Sum] = 1; return ; } for(int i = 0; i < MAX[cur].cnt; i++) { Sum += MAX[cur].M[i]; dfs(Sum,cur+1); Sum -= MAX[cur].M[i]; }}int main() { int cas = 0; while(scanf("%d", &w) == 1 && w) { if(cas) printf("\n"); init(); dfs(0,0); sort(ans,ans+cnt); printf("Workyards %d\n", ++cas); printf("Maximum profit is %d.\n", sum); printf("Number of pruls to buy:"); for(int i = 0; i < cnt && i < 10; i++) printf(" %d", ans[i]); printf("\n"); } return 0;}
0 0
- UVA - 812 Trade on Verweggistan dfs + 贪心
- UVa 812 - Trade on Verweggistan (贪心+dfs)
- UVa 812 - Trade on Verweggistan(贪心)
- UVA - 812 Trade on Verweggistan
- uva 812 - Trade on Verweggistan(暴力)
- UVa 812 - Trade on Verweggistan 解题报告(枚举)
- 812 - Trade on Verweggistan (暴力枚举)
- POJ 1874Trade on Verweggistan
- poj 1874 Trade on Verweggistan
- UVA812-Trade on Verweggistan(暴力)
- p1874_Trade on Verweggistan
- UVa 1267 Network (DFS&贪心)
- UVA - 1614 Hell on the Markets 贪心+推理
- UVA 10317 - Equating Equations 贪心 dfs
- UVA 1267 - Network(贪心DFS)
- hdoj1009FatMouse' Trade(贪心求解)
- 【贪心】 hdu1009 FatMouse' Trade
- ZOJ FatMouse' Trade 贪心
- Tomcat 使用Log4j 解决catalina.out日志文件过大
- poj 并查集 - 1308 Is It A Tree?
- 从头学Android之SubMenu子菜单
- WebSphere下使用CA证书进行签名和加解密的方法
- java数据引擎(九):应用二
- UVA - 812 Trade on Verweggistan dfs + 贪心
- 百度面试题:找到满足条件的数组
- python之urllib
- C# Tuple<T1,T2....T>元组的使用
- 2 Japan
- 开源免费天气预报接口API以及全国所有地区代码!
- hadoop2.6环境下安装Hive
- MediaPlayerControl接口分析
- c++ map erase()