HDU 6070 Dirt Ratio 线段树 二分

来源:互联网 发布:sd卡数据恢复手机版 编辑:程序博客网 时间:2024/05/28 23:21

题目

In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the following way. First let's ignore all the problems the team didn't pass, assume the team passed Xproblems during the contest, and submitted Y times for these problems, then the ''Dirt Ratio'' is measured as XY. If the ''Dirt Ratio'' of a team is too low, the team tends to cause more penalty, which is not a good performance.



Picture from MyICPC


Little Q is a coach, he is now staring at the submission list of a team. You can assume all the problems occurred in the list was solved by the team during the contest. Little Q calculated the team's low ''Dirt Ratio'', felt very angry. He wants to have a talk with them. To make the problem more serious, he wants to choose a continuous subsequence of the list, and then calculate the ''Dirt Ratio'' just based on that subsequence.

Please write a program to find such subsequence having the lowest ''Dirt Ratio''.



来自官方的题解


二分答案midmid,检验是否存在一个区间满足\frac{size(l,r)}{r-l+1}\leq midrl+1size(l,r)mid,也就是size(l,r)+mid\times l\leq mid\times (r+1)size(l,r)+mid×lmid×(r+1)

从左往右枚举每个位置作为rr,当rr变化为r+1r+1时,对sizesize的影响是一段区间加11,线段树维护区间最小值即可。

时间复杂度O(n\log n\log w)O(nlognlogw)

代码来自标程,仅供参考

#include<cstdio>const int N=60010,M=131100;int Case,n,i,a[N],ap[N],tag[M];double v[M],t,L,R,MID;inline double min(double a,double b){return a<b?a:b;}inline void tag1(int x,int p){tag[x]+=p;v[x]+=p;}inline void pb(int x){if(tag[x])tag1(x<<1,tag[x]),tag1(x<<1|1,tag[x]),tag[x]=0;}void build(int x,int a,int b){  v[x]=MID*a,tag[x]=0;  if(a==b)return;  int mid=(a+b)>>1;  build(x<<1,a,mid),build(x<<1|1,mid+1,b);}void change(int x,int a,int b,int c,int d){  if(c<=a&&b<=d){tag1(x,1);return;}  pb(x);  int mid=(a+b)>>1;  if(c<=mid)change(x<<1,a,mid,c,d);  if(d>mid)change(x<<1|1,mid+1,b,c,d);  v[x]=min(v[x<<1],v[x<<1|1]);}void ask(int x,int a,int b,int d){  if(b<=d){    if(t>v[x])t=v[x];    return;  }  pb(x);  int mid=(a+b)>>1;  ask(x<<1,a,mid,d);  if(d>mid)ask(x<<1|1,mid+1,b,d);}int main(){  scanf("%d",&Case);  while(Case--){    scanf("%d",&n);    for(i=1;i<=n;i++)scanf("%d",&a[i]);    L=0,R=1;    for(int _=20;_;_--){      MID=(L+R)/2;      build(1,1,n);      for(i=1;i<=n;i++)ap[i]=0;      for(i=1;i<=n;i++){        change(1,1,n,ap[a[i]]+1,i);        t=1e9;        ask(1,1,n,i);        if(t-MID*(i+1)<=0)break;        ap[a[i]]=i;      }      if(i<=n)R=MID;else L=MID;    }    printf("%.10f\n",(L+R)/2);  }  return 0;}


   看到有几个人踩我,好气哦,还是把这个题好好的写一下题解吧

        官方题解我认为已经写得很好了,我简单说一下,size(l,r)+mid×lmid×(r+1),这一个是二分思想的缘由,

  我们的目的就是寻找某一个区间内到底有几个不同的贡献(也就是题目中的a[i]),因为mid,l,r,在我们枚举的过程是已知的

  我们唯一需要寻找的就是这个Size(l,r),这里就用的是线段树进行维护,主要是根据记录a[i]上一次出现的位置来实现的,

  具体的看代码吧,代码说的很明白了,可不可以不要睬我了,(;′⌒`) 

 

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define LL long long#define read(a) scanf("%d",&a)const int maxn= 60000+10;double MID,L,R,t;int a[maxn],pre_pos[maxn];//pre_pos[a[i]]代表的是a[i]这个题上一次提交的位置int n;int diff_num[maxn<<2];//diff_num存的是 线段树上某节点代表的区间  可以增加的不同的颜色的个数double v[maxn<<2];//v数组 存的是 线段树上某点(包括这个点的子树) 的最小的 size+MID*linline double min(double a,double b){    return a<b?a:b;}void build(int x,int l,int r) //建树过程不难理解,因为MID*l是不变的,提前做好初始化{    v[x]=MID*l; diff_num[x]=0;    if(l==r) return;    int mid=(l+r)>>1;    build(x<<1,l,mid);    build(x<<1|1,mid+1,r);}inline void  add(int x,int num){    v[x]+=num;    diff_num[x]+=num;}inline void sonadd(int x){    if(diff_num[x])    {        add(x<<1 ,diff_num[x]);        add(x<<1|1,diff_num[x]);        diff_num[x]=0;    }}void change(int x,int l,int r,int pre,int now){    if(pre<=l&&r<=now)//pre<=l代表当前区间的左端点比遍历到的点的前一个位置大                      //r<=now代表当前区间的右端点比遍历到的点的当前位置小或者等于                      //这样的话代表这个区间可以多一个不同的贡献                      //为什么是r<=now呢,是因为now参数代表的是我们枚举到的右端点                      //所以树上的端点代表的区间只要右端点小于等于now,其实都可以看作是它的右端点是now                      //如果右端点比now大就不行了,那样的话区间就比我们枚举的区间大了,超范围了。    {        add(x,1);//这里就是更新了,如果可以有一个不同的就加一个        return ;    }    sonadd(x); //这里表示如果该点的区间不符合,那么把他所有的子树更新,因为他的子树的区间范围是比该点小的               //所以他的子树一定可以获得和他的父节点一样的不同的贡献               //更新完,x的临时存储获得的不同贡献的diff_num数组要清零,否则下一次给子树更新会加重    int mid=(l+r)>>1;    if(pre<=mid)        change(x<<1,l,mid,pre,now);    if(mid+1<=now)        change(x<<1|1,mid+1,r,pre,now);    v[x]=min(v[x<<1],v[x<<1|1]);//这一步可以很好的减少检索深度    // 这样的话最大的父节点就存储着包括他子树的最小的v[x],只需要找到这个父节点就可以了,    //不需要准确到代表那个区间的子树上的点}void ask(int x,int l, int r,int right){    if(r<=right)    {        if(t>v[x])           t=v[x];//这里是我唯一的疑问,因为是自己理解然后手撕的代码,所以写的时候就在这里加了个括号                  //就在下面的注释里面,但是那样子做就错了,我的理解t<v[x]的时候会一直搜索,导致搜索溢出                  //但是还是不能说服自己,希望大牛看到可以帮忙解释一下        return;/*            {             t=v[x];             return;            }*/    } //   sonadd(x); 这一步代码里有,但是我感觉没有用,我就给注释掉了,提交之后发现果然没有用,这是一步无用的更新    int mid=(l+r)>>1;        ask(x<<1,l,mid,right);    if(mid+1<=right)        ask(x<<1|1,mid+1,r,right);}int main(){    int T;    read(T);    while(T--)    {      read(n);      for(int i=1;i<=n;i++)            read(a[i]);      L=0.0,R=1.0;      for(int cnt=1;cnt<=24;cnt++)//标程里面是20次,无所谓的,这里就是致敬一下黑曼巴      {        int i;        MID=(L+R)/2;        build(1,1,n);        for( i=1;i<=n;i++) pre_pos[i]=0;        for( i=1;i<=n;i++)        {            t=0x3f3f3f3f;//初始化最大值            change(1,1,n,pre_pos[a[i]]+1,i);//这里传的是pre_pos[a[i]]+1,奥秘在change里面,一想就明白,不解释了            ask(1,1,n,i);            if(t-MID*(i+1)<=0)//这里就是公式的转化,size+MID*l-MID*(r+1)            break;            pre_pos[a[i]]=i;        }         if(i<=n)R=MID;         else L=MID;      }      printf("%.10f\n",(L+R)/2);    }    return 0;}


 


原创粉丝点击