hdu 4923 第六次多校

本文探讨了如何通过算法优化和高效编码策略来提高程序运行效率。重点介绍了贪心算法的应用,通过实例演示如何利用栈维护区间递增,实现区间合并以达到最优解。文章还提供了代码示例,详细解释了每一步的逻辑与实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Room and Moor

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 79    Accepted Submission(s): 12


Problem Description
PM Room defines a sequence A = {A 1, A 2,..., A N}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B 1, B 2,... , B N} of the same length, which satisfies that:

 

Input
The 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 A i.
 

Output
Output 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
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   4930  4929  4928  4927  4926 

贪心即可 用栈维护区间递增,如果比栈顶小,就合并区间,直到合法为止;

然后每个区间f(a,b)=x*y/(x+y),  x代表区间内1的个数,y代表区间内0的个数。



代码:

#include <cstdio>
#include <stack>
using namespace std;
struct Node {
    int ae, be;
    double ce;
    void init()
    {
        ce=double(be)/double(ae+be);
    }
};
int main ()
{
    //freopen("data.in","r",stdin);
    int n,T,date,oldDate;
    int BB;
    int BH;
    int a,b;
	double ans;
	Node temp;
	stack<Node> q;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);
        a=0,b=0,BB=0,BH=0;
        oldDate=1;
        for (int i=0;i<n;i++)
        {
            scanf("%d",&date);
            if((date!=oldDate)&&i!=0)
            {
                temp.ae=a;
                temp.be=b;
                temp.init();
                while (!q.empty()&&temp.ce<q.top().ce)
                {
                    temp.ae=temp.ae+q.top().ae;
                    temp.be=temp.be+q.top().be;
                    temp.init();
                    q.pop();
                }
                q.push(temp);
                if(date==0)
                {
                    a=1;
                    b=0;
                }
                else {a=0;
                    b=1;}
            }
            else {
                if(date==0)
                {
                    a++;
                }
                else {
                    b++;
                }
            }
            if((i==n-1)&&date==0)
            {
                temp.ae=a;
                temp.be=b;
                temp.init();
                while (!q.empty()&&temp.ce<q.top().ce)
                {

                    temp.ae=temp.ae+q.top().ae;
                    temp.be=temp.be+q.top().be;
                    temp.init();
                    q.pop();
                }
                q.push(temp);
            }
        
            oldDate=date;
        
        }
        ans=0;
        while (!q.empty())
        {
            ans+=q.top().ce*q.top().ae;
            q.pop();
        }
        printf("%.6f\n",ans);
    }
    return 0;
    
    
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值