uva 11691 - Allergy Test(DP+map)

来源:互联网 发布:全球最贵的十大域名 编辑:程序博客网 时间:2024/06/07 00:31

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 durationDmeasured in whole days, indicating exactly how many days you willsuffer from an allergic reactionif 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:

  1. At 8 o'clock each morning, at most one of the allergens is applied to your body.
  2. 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 followk 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

23222514252

Sample Output

510这一题把题意转化一下,画图会清晰一点,有空再画!这一题一直苦恼数组开不了那么大!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 完全可以!
#include <iostream>#include <cstdio>#include <vector>#include <map>#include <algorithm>using namespace std;#define ll long longconst 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;}

 
0 0
原创粉丝点击