首页 > 其他 > 详细

各种方法

时间:2014-07-19 18:39:57      阅读:303      评论:0      收藏:0      [点我收藏+]
C - Allergy Test

Description

 
bubuko.com,布布扣
 
C - Allergy Test

Problem C - Allergy Test

Time limit: X seconds

bubuko.com,布布扣

A test for allergy is conducted over the course of several days, and consists of exposing you to different substances (so called allergens). The goal is to decide exactly which of the allergens you are allergic to. Each allergen has a live duration D measured in whole days, indicating exactly how many days you will suffer from an allergic reaction if you are allergic to that particular substance. An allergic reaction starts to show almost immediately after you have been exposed to an allergen which you are allergic 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 affect exactly D allergic reaction examinations. Of course, if you have two or more active allergens in your body at the time of an observed reaction, you cannot tell from that information only, which of the substances you are allergic to. You want to find the shortest possible test scheme given the durations of the allergens you want to test. Furthermore, to allow simple large scale application the test scheme must be non-adaptive, i.e. the scheme should be fixed in advance. Thus you may not choose when to apply an allergen based on the outcome of previous allergic reaction examinations.   

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 being tested for. Then follow k lines each containing an integer D (1 ≤ D ≤ 7) specifying the live duration 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 active allergens in your body, thus a test scheme for a single allergen with live duration D takes D days.

Sample Input

2
3
2
2
2
5
1
4
2
5
2

Sample Output

5
10

 

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <map>
 5 #include <string>
 6 #include <cmath>
 7 #include <iostream>
 8 #include <time.h>
 9 #include <algorithm>
10 using namespace std;
11 #define ll long long
12 #define INF 0x7fffffff
13 #define mod 1000000000
14 #define maxn (1<<20)
15 int n, m, k, s, t, ans;
16 int dp[maxn][9];
17 int a[22],in[22],vis[22];
18 void dfs(int *b, int t){
19     int f[22];
20     if (t >= ans)return;
21     int flag = 1;
22     for (int i = 0; i < n; i++){
23         if (in[i] == 0)flag = 0;
24         if (b[i]<a[i])flag = 0;
25     }
26     if (flag){
27         ans = min(ans, t); return;
28     }
29     for (int i = 0; i < n; i++){
30         if (vis[i] == 1)b[i]++;
31     }
32     dfs(b, t + 1);
33     for (int i = 0; i < n; i++){
34         if (vis[i] == 1)b[i]--;
35     }
36     int x = 0,p;
37     for (int i = 0; i < n; i++){
38         if ((vis[i] && b[i] < a[i])){
39             x++;
40             p = i;
41         }
42     }
43     if (x == 1&&in[p]==0){
44         in[p] = 1;
45         dfs(b, t);
46         in[p] = 0;
47     }
48     for (int i = 0; i < n;i++)
49     if (vis[i] == 1)b[i]++;
50     for (int i = 0; i < n; i++){
51         if (b[i]>=a[i]||vis[i])continue;
52         vis[i] = 1;
53         dfs(b, t + 1);
54         vis[i] = 0;
55     }
56     for (int i = 0; i < n; i++)
57     if (vis[i] == 1)b[i]--;
58 }
59 int main(){
60     int T; scanf("%d", &T);
61     while (T--){
62         int b[22];
63         scanf("%d", &n);
64         for (int i = 0; i < n; i++){
65             scanf("%d", &a[i]);
66         }
67         ans = 82;
68         for (int i = 0; i < n; i++){
69             memset(in, 0, sizeof in);
70             memset(vis, 0, sizeof vis);
71             memset(b, 0, sizeof b);
72             vis[i] = 1; 
73             dfs(b, 0);
74         }
75         printf("%d\n", ans);
76     }
77     return 0;
78 }
View Code

TLE方法。。。dfs这个dfs也是写的要死。。

各种方法,布布扣,bubuko.com

各种方法

原文:http://www.cnblogs.com/HaibaraAi/p/3854942.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!