uva 11691 - Allergy Test(DP+map)
2014-02-10 15:24
861 查看
Problem C - Allergy Test
Time limit: X seconds
A test for allergy is conducted over the course of several days, andconsists of exposing you to different substances (so calledallergens). The goal is to decide exactly which of the allergens youare allergic to. Each allergen has a live duration
Dmeasured in whole days, indicating exactly how many days you willsuffer from an allergic reaction
if you are allergic to thatparticular substance. An allergic reaction starts to show almostimmediately after you have been exposed to an allergen which you areallergic to. The test scheme has two action points per day:
At 8 o'clock each morning, at most one of the allergens is applied to your body.
At 8 o'clock each evening, you are examined for allergic reactions.
Thus an allergen with live duration D will affectexactly D allergic reaction examinations.Of course, if you have two or more active allergens in your body atthe time of an observed reaction, you cannot tell from thatinformation only, which
of the substances you are allergic to. You want to find the shortest possible test scheme given the durationsof the allergens you want to test. Furthermore, to allow simple largescale application the test scheme must be non-adaptive, i.e. thescheme should
be fixed in advance. Thus you may not choose when toapply an allergen based on the outcome of previous allergic reactionexaminations.
Input
The first line of the input file contains an integer N (N<30) which denotes the total number of test cases.The description of each test case is given below:The first line of the input contains a single integer k (1≤ k ≤ 20) specifying the number of allergens beingtested for. Then follow
k lines each containing aninteger D (1 ≤ D ≤ 7) specifying the liveduration of each allergen.
Output
For each test case, print in a single line the number of days of the shortest conclusive non-adaptive test scheme.A scheme ends the morning when you no longer have activeallergens in your body, thus a test scheme for a single allergen withlive durationD takes D days.
Sample Input
2 3 2 2 2 5 1 4 2 5 2
Sample Output
5 10 这一题把题意转化一下,画图会清晰一点,有空再画! 这一题一直苦恼数组开不了那么大!int dp[8][21][21][21][21][21][21][21],8表示当前状态可以节省的天数,其余21表示每一种D的个数,但是数组开不了这么大! 所以,用map<long long , int> dp[8]就轻松解决啦~把后面的21转换成long long ,每个21占2位,总共14位,long long 完全可以! [code]#include <iostream> #include <cstdio> #include <vector> #include <map> #include <algorithm> using namespace std; #define ll long long const int maxn = 10; int D[maxn] , k; map<ll , int> dp[maxn]; void initial(){ for(int i = 0;i < maxn;i++){ D[i] = 0; } } void readcase(){ scanf("%d" ,&k); for(int i = 0;i < k;i++){ int d; scanf("%d" , &d); D[d]++; } } int DP(int k){ ll tem = 0; for(int i = 7;i >= 1;i--){ tem = tem*100+D[i]; } //cout << tem << "P"<<endl; if(tem == 0) return 0; if(dp[k].find(tem) != dp[k].end()) return dp[k][tem]; int ans = 1e9; for(int i = 1;i <= 7;i++){ if(D[i] > 0){ D[i]--; ans = min(ans , DP(max(0 , i-k-1))+max(1 , i-k)); D[i]++; } } return dp[k][tem] = ans; } void computing(){ printf("%d\n" , DP(0)); } int main(){ freopen("in" , "r" , stdin); int N; cin >> N; while(N--){ initial(); readcase(); computing(); } return 0; }
[/code]
相关文章推荐
- UVA 11691 Allergy Test(状压dp)
- UVA - 10981(dp ,map做内存类,仍没算出最大内存)
- UVA - 11691(dp 集合类)
- UVA 11691 - Allergy Test(状压dp+贪心)
- uva 11691 - Allergy Test(状态压缩+记忆化)
- UVA 10891 Game of Sum(区间DP)
- uva 357 - Let Me Count The Ways(动态规划-注意dp初始化的问题)
- UVA 111 简单DP 但是有坑
- UVALive 3983(单调队列优化dp)
- UVA10003 简单DP
- 2017-2018 ACM-ICPC Southeast Regional Contest (Div. 1) J.Treasure Map dp
- UVA11552——Fewest Flops(DP)
- UVA 11584 - Partitioning by Palindromes DP
- UVA 1218 树形DP
- UVa 1025 A Spy in the Metro (DP动态规划)
- 区间DP(修缮长城,uva 1336)
- UVa 10285:Longest Run on a Snowboard(DP)
- uva 10688 - The Poor Giant(区间DP,较难,题目难懂,状态转移难。。。)
- UVA - 10285 Longest Run on a Snowboard (线性DP)
- uva 11367 dijkstra+dp状态压缩