HDU 4923 (貪心+證明)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   for   

Room and Moor




Problem DescriptionPM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

 
InputThe input consists of multiple test cases. The number of test cases T(T<=100) occurs in the first line of input.

For each test case:
The first line contains a single integer N (1<=N<=100000), which denotes the length of A and B.
The second line consists of N integers, where the ith denotes Ai. 
OutputOutput the minimal f (A, B) when B is optimal and round it to 6 decimals. 
Sample Input
4 9 1 1 1 1 1 0 0 1 1 9 1 1 0 0 1 1 1 1 1 4 0 0 1 1 4 0 1 1 1
 
Sample Output
1.428571 1.000000 0.000000 0.000000

 

 

 題意:不多說了。

sl: 其實就是一個貪心的思想。

 網上講解很多。比賽時沒搞出來。。。呵呵了。

 

 1 // by caonima
 2 // hehe
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <stack>
 6 #include <queue>
 7 #include <vector>
 8 #include <algorithm>
 9 using namespace std;
10 const int MAX = 1e6+10;
11 struct node {
12     int one,zero;
13     double val;
14 }v[MAX];
15 stack<node> ans;
16 int a[MAX];
17 void init() {
18     while(!ans.empty()) ans.pop();
19 }
20 int main() {
21     int cas,n;
22     scanf("%d",&cas);
23     while(cas--) {
24         scanf("%d",&n);
25         for(int i=1;i<=n;i++) {
26             scanf("%d",&a[i]);
27         }
28         a[n+1]=1;
29         int L=1,R=n;
30         while(a[L]==0) L++;
31         while(a[R]==1) R--;
32         if(L>=R) printf("0.000000\n");
33         else {
34             int id=0;
35             for(int i=L;i<=R;) {
36                 int cnt0=0,cnt1=0;
37                 while(a[i]==1) {
38                     cnt1++; i++;
39                 }
40                 while(a[i]==0) {
41                     cnt0++; i++;
42                 }
43                 v[id].one=cnt1; v[id].zero=cnt0;
44                 v[id++].val=(double) cnt1/(double)(cnt0+cnt1);
45             }
46             init();
47             for(int i=0;i<id;i++) {
48 
49                 if(ans.empty()) ans.push(v[i]);
50                 else {
51                     node vn=ans.top();
52                     if(vn.val<=v[i].val) ans.push(v[i]);
53                     else {
54                         node t=v[i];
55                         while(true) {
56                             node vx=ans.top();
57                             if(vx.val>t.val) {
58                                 t.one+=vx.one; t.zero+=vx.zero;
59                                 t.val=(double) t.one/(double)(t.one+t.zero);
60                                 ans.pop();
61                             }
62                             else {
63                                 ans.push(t);
64                                 break;
65                             }
66                             if(ans.empty()) {
67                                 ans.push(t);
68                                 break;
69                             }
70                         }
71                     }
72                 }
73             }
74             double res=0;
75             while(!ans.empty()) {
76                 node vn=ans.top();
77                 ans.pop();
78                 res+=vn.val*vn.val*vn.zero+(1-vn.val)*(1-vn.val)*vn.one;
79             }
80             printf("%.6lf\n",res);
81         }
82 
83     }

84 } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.