zoukankan      html  css  js  c++  java
  • 计蒜客:Entertainment Box

    Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fights they have finally decided to buy a video tape recorder. This fabulous, new device can record kk different TV shows simultaneously, and whenever a show recorded in one the machine's kk slots ends, the machine is immediately ready to record another show in the same slot.

    The three friends wonder how many TV shows they can record during one day. They provide you with the TV guide for today's shows, and tell you the number of shows the machine can record simultaneously. How many shows can they record, using their recording machine? Count only shows that are recorded in their entirety.

    Input Format

    The first line of input contains two integers nn, k(1 le k < n le 100 000)(1k<n100000). Then follow nn lines, each containing two integers x_i, y_ixi,yi, meaning that show ii starts at time x_ixi and finishes by time y_iyi. This means that two shows iiand jj, where y_i = x_jyi=xj, can be recorded, without conflict, in the same recording slot. You may assume that 0 le x_i < y_i le 1 000 000 0000xi<yi1000000000.

    Output Format

    The output should contain exactly one line with a single integer: the maximum number of full shows from the TV guide that can be recorded with the tape recorder.

    样例输入1

    3 1
    1 2
    2 3
    2 3

    样例输出1

    2

    样例输入2

    4 1
    1 3
    4 6
    7 8
    2 5

    样例输出2

    3

    样例输入3

    5 2
    1 4
    5 9
    2 7
    3 8
    6 10

    样例输出3

    3

    题目来源

    Nordic Collegiate Programming Contest 2015​


      

      2018/7/26 计蒜客 Nordic Collegiate Programming Contest 2015​ 比赛的E题,这个题做了好一会,总结一下。

     

      这个题是一道类似贪心的题,我一开始考虑的是,按照结束时间从低到高进行排序,从第一个插槽开始走到底,走完再走下一条插槽的情况,但是一直WA,在队友提醒下改为类似并行的解决办法。

      

      先进行按照结束时间从低到高进行排序,然后开辟一个q数组,有几个插槽就开几位,用来存放当前最后结束的影片的时间,然后循环,判断当前的开始时间和q数组内的时间的大小,当该开始

    时间比q数组的时间都小的时候,就不插入。当开始时间比数组内的某个值大的时候,就删除这个值,把当前影片的结束时间插入其中。

     

      代码如下(已AC):

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<vector>
     4 using namespace std;
     5 
     6 int n, k, c, ans=0;
     7 
     8 struct node{
     9     int start;
    10     int end;
    11     int vis;
    12 }a[100001]; 
    13 
    14 vector<int> q;
    15 
    16 bool cmp(node x, node y){
    17     return x.end < y.end;
    18 }
    19 
    20 int main(){
    21     cin >> n >> k;
    22     for(int i=0;i<n;i++){
    23         cin >> a[i].start >> a[i].end;
    24     } 
    25     sort(a, a+n, cmp);
    26     for(int i=0;i<k;i++)q.push_back(0);
    27     for(int i=0;i<n;i++){
    28             vector<int>::iterator count = upper_bound(q.begin(), q.end(), a[i].start);
    29             if(count != q.begin()){
    30                 q.erase(count-1);
    31                 q.push_back(a[i].end); 
    32                 ans++;
    33             }
    34     }
    35     cout << ans;
    36 } 

     

  • 相关阅读:
    Web--2019年5月7日移动端随笔
    Web-css3--19年5月5日随笔
    数据库指令----4月10日
    19--3--14--数据处理
    微信小程序全选,微信小程序checkbox,微信小程序购物车
    手机预览微信小程序
    原生H5页面模拟APP左侧滑动删除效果
    微信小程序实例:实现tabs选项卡效果
    CSS3文本溢出显示省略号
    前端自动化之路之gulp,node.js
  • 原文地址:https://www.cnblogs.com/jyroy/p/9373249.html
Copyright © 2011-2022 走看看