zoukankan      html  css  js  c++  java
  • ZOJ 3941 Kpop Music Party(省赛, 贪心)

    Kpop Music Party

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Marjar University often hosts Kpop music festival. A Kpop music festival will last several days. During a Kpop festival, there will be a Kpop party every day. Kpop music is very popular all over the world, so there may be more than one Kpop music festival being hosted simultaneously.

    Edward, the headmaster of Marjar University, is always obsessed with Kpop music. He will be very excited continuously for K days after attending a Kpop music party. More specifically, If he attends a Kpop party in the i-th day, he would be very excited from the i-th day to the (i + K - 1)-th day (inclusive). But the excitatory state does not stack. For example, if K is 5 and Edward attended a party in Day 1 and a party in Day 3, he will be very excited only from Day 1 to Day 7.

    Edward has got the schedule of N Kpop music festivals in Marjar University. Each Kpop music festival lasts one or more days. The i-th Kpop festival starts at the Si-th day and ends at the Ei-th day (inclusive). Due to restrictions on the discipline for the headmaster, he can attend at most M Kpop parties. Now he wants to maximize the number of days being excited. Can you help him?

    Input

    There are multiple test cases. The first line of input is an integer T (≤ 1000) indicating the number of test cases. For each test case:

    The first line contains three integers, NK and M.

    The next N lines, each line contains two integers Si and Ei (1 ≤ N ≤ 10, 1 ≤ KMSiEi ≤ 109).

    Output

    For each case, print the number of the most days that Edward can be excited for.

    Sample Input

    2
    1 5 2
    1 3
    3 7 3
    1 5
    2 5
    13 13
    

    Sample Output

    7
    

    18

    这道题目最先想到的是贪心,从最左边开始,依次选择长度k的区间,不相交,这样是最大的。但是这样贪心完之后发现m还有剩余,

    发现每个区间的最后一点也可以选,同样可以有增益效果,但是回头再去选,会打乱一开始的贪心,所以在贪心的时候就枚举每个区间

    最后一个点要不要选,最后也就2的10次方种可能

    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    using namespace std;
    typedef long long int LL;
    struct Node{
        LL l,r;
    }a[15],b[15];
    int cmp(Node a,Node b){
       if(a.l==b.l)
            return a.r<b.r;
      return a.l<b.l;
    }
    int n1;
    LL m,k,res;
    int cnt;
    void dfs(LL pos,int i,LL ans,LL n){
    	if(n>m)
    		return;
        if((i>=cnt+1)||n==m){
            res=max(res,ans);
    		return;
        }
        LL len,pos1,num,p2,p;
        if(pos<b[i].r){
           len=min(b[i].r-b[i].l+1,b[i].r-pos);
           p=len%k;
           if(p==0) p2=len/k;
    	   else p2=len/k+1;
           if(p2+n>m) res=max(res,ans+(m-n)*k);
           pos1=max(b[i].l+p2*k-1,pos+p2*k);
           num=p2*k;
           dfs(pos1,i+1,ans+num,n+p2);//不选最后一个点
           dfs(b[i].r+k-1,i+1,ans+num+b[i].r+k-1-pos1,n+p2+1);//选择最后一个点
        }
        else if(pos>=b[i].r){
            dfs(pos,i+1,ans,n);//不选最后一个点
            dfs(b[i].r+k-1,i+1,ans+b[i].r+k-1-pos,n+1);//选择最后一个点
        }
    }
    int main(){
         int t;
         scanf("%d",&t);
         while(t--){
             scanf("%d%lld%lld",&n1,&k,&m);
             for(int i=1;i<=n1;i++)
                 scanf("%lld%lld",&a[i].l,&a[i].r);
             sort(a+1,a+n1+1,cmp);
             cnt=0;
             int i,j;
             for(i=1;i<=n1;i){
                 for( j=i+1;j<=n1;j++){
                    if(a[j].l<=a[i].r+1)//区间可以合并
                    {a[i].r=max(a[i].r,a[j].r);}
                    else break;
                 }
    			 b[++cnt]=a[i];
                 i=j;
            }
            res=0;
            dfs(0,1,0,0);
            printf("%lld
    ",res);
         }
         return 0;
    }



  • 相关阅读:
    linux php5.6 安装Redis扩展
    linux php7.2安装扩展memcached
    极简的switch控件
    整理了最近百年的藏历数据,做了个公历藏历映射的小工具
    我是怎么让全国最大的儿童失踪预警平台流量掉底的
    jq的getScript函数不支持chaset?override掉!
    大家好像都比较少关心webcrypto,试试写个简单的sha1/sha256/sha384/sha512实现看看
    惊喜:opera换webkit内核后完美支持SDCH压缩协议
    TCPIP协议实践:wireshark抓包分析之链路层与网络层
    使用unity3d和tensorflow实现基于姿态估计的体感游戏
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228680.html
Copyright © 2011-2022 走看看