HDU 4923 room and moor greedy + Stack

Source: Internet
Author: User

Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4923

Bi can be a decimal number.

Idea: a witty idea. For M 1 + N 0 parts in a row, the optimal solution must be bi = m/(m + n ), because Bi is incremental (you can push it manually), if Bi> Bi in the previous section appears in the next section, it is impossible to obtain the optimal solution. Therefore, the two sections are combined for processing, in this case, the stack is needed for maintenance.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <map>#include <cstdlib>#include <queue>#include <stack>#include <vector>#include <ctype.h>#include <algorithm>#include <string>#include <set>#define PI acos(-1.0)#define maxn 105#define INF 0x7fffffff#define eps 1e-8typedef long long LL;typedef unsigned long long ULL;using namespace std;int aa[1000005];struct Line{    int t0,t1;    double val;    void calc()    {        val=(double)t1/(double)(t1+t0);    }    double sum()    {        return val*val*(double)(t0)+(1-val)*(1-val)*(double)(t1);    }} l[1000005],h;int main(){    int T;    scanf("%d",&T);    while(T--)    {        memset(l,0,sizeof(l));        memset(aa,0,sizeof(aa));        int tot,head=-1,tail=-1;        scanf("%d",&tot);        for(int i=0; i<tot; i++)            scanf("%d",&aa[i]);        for(int i=0; i<tot; i++)            if(aa[i]==1)            {                head=i;                break;            }        for(int i=tot-1; i>=0; i--)            if(aa[i]==0)            {                tail=i;                break;            }        if(tail<=head)            printf("0.000000\n");        else        {            int t=head,top=0;            while(t<tail)            {                for(int i=t;i<=tail;i++)                    if(aa[i]==0)                    {                        l[top].t1=i-t;                        t=i;                        break;                    }                for(int i=t;i<=tail;i++)                {                    if(i==tail)                    {                        l[top].t0=i-t+1;                        t=i;                        break;                    }                    else if(aa[i]==1)                    {                        l[top].t0=i-t;                        t=i;                        break;                    }                }                    l[top].calc();                    top++;            }            stack < Line > st;            while(!st.empty())                st.pop();            for(int i=0;i<top;i++)            {                if(st.empty())                    st.push(l[i]);                else                {                    while(!st.empty()&&st.top().val>l[i].val)                    {                        h=st.top();                        st.pop();                        l[i].t0+=h.t0;                        l[i].t1+=h.t1;                        l[i].calc();                    }                    st.push(l[i]);                }            }            double ans=0;            while(!st.empty())            {                h=st.top();                ans+=h.sum();                st.pop();            }            printf("%.6lf\n",ans);        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.