记忆化搜搜——Code Jam 2009 Round 1C #C Bribe the Prisoners
来源:互联网 发布:php源码分析 编辑:程序博客网 时间:2024/05/22 03:04
题目链接:
https://codejam.withgoogle.com/codejam/contest/189252/dashboard#s=p2题意:给出一个长度为P的监狱,每个位置一个人,现在要释放Q个人,没释放一个人,要给他左右两边的连续有人的位置的人贿赂一枚金币,求释放这Q个人最少需要花费多少金币
分析:初看这题和哈夫曼路径很像,其实不是,因为每释放一个人,相当于少了一个单位的长度,权值是在改变的,强行贪心会WA到深处自然醒的。然后我们发现Q最大只有100,那么可以直接搜索一下试试。我们假设每次搜一个区间的最小花费,区间的端点处为空牢房。即:
int DFS(s,t){ if(s+1==t) return 0; else { int tmp = INF; for(int i=s+1;i<t;i++) { tmp = min(tmp, DFS(s,i)+DFS(i,t)+cost(i); } return tmp }}
在搜索的过程中,我们发现,很多区间被重复搜索,所以可以每搜一个区间,保存下来,以后就直接使用。
AC代码:
/************************************************************************* > File Name: test.cpp > Author: Akira > Mail: qaq.febr2.qaq@gmail.com ************************************************************************/#include<bits/stdc++.h>typedef long long LL;typedef unsigned long long ULL;typedef long double LD;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define Sqr(a) ((a)*(a))using namespace std;#define MaxN 110#define MaxM MaxN*10#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626const int mod = 1E9+7;const double eps = 1e-6;#define bug cout<<88888888<<endl;#define debug(x) cout << #x" = " << x;int P,Q,A[MaxN];int DP[MaxN][MaxN];int DFS(int s, int t){ if(DP[s][t]!=-1) return DP[s][t]; else { if(t-s==1) return DP[s][t]=0; int cnt = INF; int cost = (A[t]-A[s])-2; for(int i=s+1;i<t;i++) { int tmp = cost+DFS(s,i)+DFS(i,t); cnt = min(cnt, tmp); } //cout << s << " " << t << " "<< cnt << endl; return DP[s][t] = cnt; }}void solve(int t){ MST(DP,-1); A[0] = 0; A[Q+1] = P+1; sort(A,A+Q+1); int ans = DFS(0,Q+1); printf("Case #%d: %d\n", t, ans);}int main(){ //std::ios::sync_with_stdio(false); freopen("C-large-practice.in","r",stdin); freopen("C.out","w",stdout); /********************************************/ int T; scanf("%d", &T); for(int t=1;t<=T;t++) { scanf("%d%d", &P, &Q); { for(int i=1;i<=Q;i++) scanf("%d", A+i); if(Q==1) { printf("Case #%d: %d\n", t, P-1); } else solve(t); } } /********************************************/ fclose(stdin); fclose(stdout); system("pause");}
0 0
- 记忆化搜搜——Code Jam 2009 Round 1C #C Bribe the Prisoners
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
- Code Jam 2009 Round1C Problem C. Bribe the Prisoners —— 区间DP
- 刷题: bribe the prisoners(2009 Round 1C C)
- GCJ 2009 Round 1C C (Bribe the Prisoners)
- GCJ--Bribe the Prisoners (2009 Round 1C C)
- GCJ 2009 Round 1C Bribe the Prisoners
- [Code Jam] Bribe the Prisoners
- 挑战2.7.3 Round 1C 2009 C. Bribe the Prisoners 区间dp
- Bribe the Prisoners(GCJ 2009 Round 1C C)(dp)
- GCJ 2009 Round1C C Bribe the Prisoners
- Bribe the Prisoners——GCJ 2009 Round1C C(区间dp)
- DP-记忆化dp--Bribe the Prisoners
- GCJ 2009 Bribe the Prisoners
- GCJ1C09C - Bribe the Prisoners
- Bribe the Prisoners(Easy)
- [C++]Saving the Universe——Google Code Jam Qualification Round 2008
- 国际化
- 网络编程系列二--最简单的网络程序
- [leetCode刷题笔记]2017.03.25
- .NET Core Windows环境安装与体验
- 设计模式——工厂方法模式
- 记忆化搜搜——Code Jam 2009 Round 1C #C Bribe the Prisoners
- RSA初步
- FOJ-1076 BIT1005 穿越沙漠
- C程序(进程)的内存布局
- VMware网络模式
- 二叉树的前序、中序、后序遍历及互相求法
- 矩阵键盘行列扫描
- C语言中的运算符总结
- LeetCode13. Roman to Integer