UVa 1412 Fund Management (预处理+状压DP)
题意:题意很难说清楚自己看原文,链接:UVa 1412 Fund Management
析:总体来说如果没有超时的话,这个题不是特别难,但是这个题很容易超时,主要是体现在状态转移时,很容易想到状态方程表示方法,
dp[i][s]表示第 i 天时状态为s时能获得的最大值,转移方程也很容易,三种决策,要么买,要么卖,要么不买不卖,就这三种,但是却不是好转移,
主要是效率不够,所以我们先预处理所有的状态转移,最后直接用就可以了。用的vector和map来存储状态和编号。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 100000 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} const int maxstate = 15000;
int k[10], s[10], kk;
char name[10][100];
double price[10][110];
vector<vector<int> > states;
map<vector<int>, int> mp;
int buy_next[maxstate][10], sell_next[maxstate][10];
double dp[110][maxstate]; void dfs(int stock, vector<int>& lots, int tot){
if(stock == n){
mp[lots] = states.size();
states.push_back(lots);
return ;
}
for(int i = 0; i <= k[stock] && tot + i <= kk; ++i){
lots[stock] = i;
dfs(stock+1, lots, tot + i);
}
} void init(){
vector<int> lots(n);
states.clear();
mp.clear();
dfs(0, lots, 0);
for(int s = 0; s < states.size(); ++s){
int tot = 0;
for(int i = 0; i < n; ++i) tot += states[s][i];
for(int i = 0; i < n; ++i){
buy_next[s][i] = sell_next[s][i] = -1;
if(buy_next[s][i] < k[i] && tot < kk){ // buy
vector<int> tmp = states[s];
++tmp[i];
buy_next[s][i] = mp[tmp];
}
if(states[s][i] > 0){ // sell
vector<int> tmp = states[s];
--tmp[i];
sell_next[s][i] = mp[tmp];
}
}
}
} double c;
int opt[110][maxstate], pre[110][maxstate]; void update(int i, int s, int s2, double v, int o){
if(v > dp[i+1][s2]){
dp[i+1][s2] = v;
opt[i+1][s2] = o;
pre[i+1][s2] = s;
}
} double solve(){
for(int i = 0; i <= m; ++i)
for(int j = 0; j < states.size(); ++j)
dp[i][j] = -inf;
dp[0][0] = c;
for(int i = 0; i < m; ++i)
for(int s = 0; s < states.size(); ++s){
double v = dp[i][s];
if(v < -1) continue;
update(i, s, s, v, 0); //hold
for(int j = 0; j < n; ++j){
if(buy_next[s][j] >= 0 && v >= price[j][i] - 1e-3)
update(i, s, buy_next[s][j], v-price[j][i], j+1); //buy
if(sell_next[s][j] >= 0)
update(i, s, sell_next[s][j], v+price[j][i], -j-1); //sell
}
}
return dp[m][0];
} void print(int i, int s){
if(!i) return ;
print(i-1, pre[i][s]);
if(opt[i][s] == 0) printf("HOLD\n");
else if(opt[i][s] > 0) printf("BUY %s\n", name[opt[i][s]-1]);
else printf("SELL %s\n", name[-opt[i][s]-1], -opt[i][s]-1);
} int main(){
while(scanf("%lf %d %d %d", &c, &m, &n, &kk) == 4){
for(int i = 0; i < n; ++i){
scanf("%s %d %d", name[i], s+i, k+i);
for(int j = 0; j < m; ++j){
scanf("%lf", &price[i][j]);
price[i][j] *= s[i];
}
}
init();
double ans = solve();
printf("%.2f\n", ans);
print(m, 0);
}
return 0;
}
UVa 1412 Fund Management (预处理+状压DP)的更多相关文章
- UVa 1412 - Fund Management(状压DP + 预处理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【暑假】[深入动态规划]UVa 1412 Fund Management
UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS Memory Limit: Unknown ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- loj 1316(spfa预处理+状压dp)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27024 题意:求0-(n-1)的经过最多的标记的点的最短路. 思路 ...
- UVA - 1252 Twenty Questions (状压dp)
状压dp,用s表示已经询问过的特征,a表示W具有的特征. 当满足条件的物体只有一个的时候就不用再猜测了.对于满足条件的物体个数可以预处理出来 转移的时候应该枚举询问的k,因为实际上要猜的物品是不确定的 ...
- hdu 4568 Hunter(spfa预处理 + 状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4568 思路:首先spfa预处理出每对宝藏之间的最短距离以及宝藏到边界的最短距离,然后dp[state] ...
- UVa 1252 - Twenty Questions(状压DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 10817 Headmaster's Headache (状压DP+记忆化搜索)
题意:一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师.每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两个老 ...
- UVA 10572 Black & White (状压DP)
题意:有一个n*m的矩阵,其中部分格子已经涂黑,部分涂白,要求为其他格子也上黑/白色,问有多少种涂法可以满足一下要求: (1)任意2*2的子矩阵不可以同色. (2)所有格子必须上色. (3)只能有两个 ...
随机推荐
- opencv中的SVM图像分类(二)
opencv中的SVM图像分类(二) 标签: svm图像 2015-07-30 08:45 8296人阅读 评论(35) 收藏 举报 分类: [opencv应用](5) 版权声明:本文为博主原创文 ...
- linux SPI驱动——简单的gpio模拟SPI驱动测试 (二)
1: /* 2: * Add by xuyonghong for duotin car radio fm 3: * Copyright (C) 2016-5-24 xuyonghong@duotin. ...
- MapReduce 1工作原理图文详解
MapReduce工作原理图文详解 一 MapReduce程序执行流程 程序执行流程图如下: 流程分析:1.在客户端启动一个作业.2.向JobTracker请求一个Job ID.3.将运行作业所需要的 ...
- 虚拟机和主机ping不通,SQL Server无法远程连接的解决方法
一.虚拟机网络的配置 这里只列一下自己的配置: 1.编辑---虚拟网络编辑器 进行设置 2.设置对应系统 3.还是Ping不通,最后关闭 虚机内的Windows防火墙,可以Ping通,看来Net模式下 ...
- error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'
1 这个error是什么原因造成的 cmake默认选择的是x86,即32位的生成子. 2 怎么解决这个error 在cmake ..的时候,在后面加上“-G "Visual Studio 1 ...
- SQL的分页算法
select top pageSize * from goods where goodsId not in (select top pageSize*(pageNow-1) goodsId from ...
- jps不显示java进程信息
本来想自己整理,发现已经有前人整理,并且完美解决了我的问题,故转载,感谢分享 转自:http://trinea.iteye.com/blog/1196400 对于jps较熟悉可以直接查看第二部分的分析 ...
- Mac终端操作SVN指令
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domain ...
- poj3461 Oulipo —— KMP
题目链接:http://poj.org/problem?id=3461 代码如下: #include<cstdio>//poj 3461 kmp #include<cstring&g ...
- SCUT125 华为杯 D.笔芯回文 —— DP
题目链接: https://scut.online/p/125 题目描述 bxbx有一个长度一个字符串SS,bxbx可以对其进行若干次操作. 每次操作可以删掉一个长度为k(1 \leq k \leq ...