zoukankan      html  css  js  c++  java
  • 牛客小白月赛2 I 艺 【归并思想】【离散化】

    链接:https://www.nowcoder.com/acm/contest/86/I
    来源:牛客网

    题目描述

    接下去,Sεlιнα(Selina) 又搞了个文艺竞演。
    虽说是文艺竞演,其实只是为了满足 Sεlιнα 的内心企盼——看群男友献歌献舞。她排列好了各个参赛男友的节目顺序,然后将他们安排在两个舞台上表演,自己则在演播室里使用两台闭路电视同时观看。万万没想到的是,当一切准备就绪时,其中一台电视炸了,她不会修,也没有时间修。于是只能尴尬地使用一台闭路电视观看两个舞台上的节目。当然,这台电视不支持分屏同时观看,所以 Sεlιнα 只能不停地换台观看。现在,作为导演的 Sεlιнα 已经知道了两个舞台的节目 单以及每个节目 对于她所能产生的愉悦度 ,她想安排电视在每个时刻播放的频道(可以在某些时刻不看),使得自己能得到最大的愉悦度。现在请优秀的你告诉 Sεlιнα 最大能产生的愉悦度是多少。
    要注意的是,文艺竞演没有广告插播,所以当一个节目结束时,另一个节目会立刻开始演出。 并且 Sεlιнα 看节目以分钟为单位,也就是说,她只能在每分钟结束的那一刻切换舞台。节目对 Sεlιнα 产生愉悦度是以分钟为单位的,也就是说,她看第 个节目每一分钟就会产生 的愉悦度。而 Sεlιнα 对节目的完整性丝毫不在意,没有完整地看一个节目是没有关系的。 

    输入描述:

    第一行三个数 ,表示舞台一有 个节目,舞台二有 个节目,总时长为 分钟。
    接下去 行,每行两个整数 ,表示舞台一的第 个节目在第  分钟结束后开始,每分钟能产生愉悦度 。当一个节目开始时,这个舞台之前正在播放的节目直接停止,中间没有暂停。
    接下去 行,每行两个整数 ,表示舞台二的第 个节目在第 分钟结束后开始,每分钟能产生愉悦度 。当一个节目开始时,这个舞台之前正在播放的节目直接停止,中间没有暂停。
    数据保证每个舞台都有一个在 分钟时开始的节目(即最开始的节目),并且在同一个舞台中没有两个节目开始时间相同(即没有一个节目时长为 )。数据不保证输入中每个舞台的 会从小到大排序。 

    输出描述:

    输出共一行,一个整数,表示最大的愉悦度。
    示例1

    输入

    复制
    2 2 5
    2 3
    0 2
    0 3
    3 1

    输出

    复制
    15

    说明

    在这个样例中,Sεlιнα 在开始时观看频道二的节目,每分钟产生愉悦度 
     ;在第二分钟结束时刻切换到频道一,每分钟产生愉悦度 
     ,然后直到结束。总共产生愉悦度 
     。 
    示例2

    输入

    复制
    3 4 17
    8 3
    0 10
    9 10
    7 15
    0 6
    16 9
    14 8

    输出

    复制
    205

    备注:

    
    
    
    
    
     
    思路:
     
    1. 将题目中的两个序列按照时间升序排列,然后合并在一个时间轴,在每个时间段内寻找获得效益最大的。  所以合并操作很容易想到 归并排序的思路
    2. 使用 set ,离散化时间代替手写归并过程
    【注】:题目中 可以有空闲时间段,因此 权值可能为负,所以 max3(0,a,b) 最小是0
     
    方法一 AC 码:
     1 #include <algorithm>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <string>
     5 #include <cmath>
     6 #include <iostream>
     7 #include <map>
     8 #include <queue>
     9 #include <set>
    10 #include <vector>
    11 
    12 using namespace std;
    13 
    14 
    15 #define max3(x, y, z) max(max((x), (y)), (z))
    16 #define min3(x, y, z) min(mix((x), (y)), (z))
    17 #define pb push_back
    18 #define ppb pop_back
    19 #define mk make_pair
    20 #define pii pair<int, int>
    21 #define pll pair<long long, long long>
    22 
    23 
    24 #define debug_l(a) cout << #a << " " << (a) << endl
    25 #define debug_b(a) cout << #a << " " << (a) << " "
    26 #define testin(filename) freopen((filename) ,"r",stdin)
    27 #define testout(filename) freopen((filename) ,"w",stdout)
    28 
    29 
    30 #define close_sync (ios::sync_with_stdio(false))
    31 
    32 
    33 
    34 typedef long long ll;
    35 typedef unsigned long long ull;
    36 
    37 
    38 const double PI  = 3.14159265358979323846264338327;
    39 const double E   = exp(1);
    40 const double eps = 1e-6;
    41 
    42 const int INF    = 0x3f3f3f3f;
    43 const int NINF   = 0xc0c0c0c0;
    44 // int maxn   = 3e3 + 5;
    45 // int MOD    = 1e9 + 7;
    46 
    47 
    48 template <typename T>
    49 void print_arr(T *arr, int arr_len)
    50 {
    51     for (int i = 0; i < arr_len; i++)
    52         cout << arr[i] << " ";
    53     cout << endl;
    54 }
    55 
    56 /*==================================begin=======================================*/
    57 
    58 const int maxn = 1e5+5;
    59 struct Node {int x,v;};
    60 Node f[maxn],s[maxn];
    61 int cmp(Node n1, Node n2){
    62     return n1.x<n2.x;
    63 }
    64 int main()
    65 {
    66     // testin("data.in");
    67     close_sync;
    68     int N,M,T;
    69     cin>>N>>M>>T;
    70     for(int i=0;i<N;i++) cin>>f[i].x>>f[i].v;
    71     for(int i=0;i<M;i++) cin>>s[i].x>>s[i].v;
    72     sort(f,f+N,cmp); sort(s,s+M,cmp);
    73 
    74     f[N].x=s[M].x=T;        //关键,在遍历到数组最后一个元素的时候,下一个值是结束时间。从而计算权值
    75 
    76     int i=0,j=0,t=0,nt;
    77     ll ans=0;
    78     while(i<N && j<M){
    79         if(f[i].x<=t) i++;
    80         if(s[j].x<=t) j++;
    81         nt= min(f[i].x,s[j].x);
    82         ans+=(nt-t)*max3(0,f[i-1].v,s[j-1].v);
    83         t=nt;
    84     }
    85     while(i<N){
    86         if(f[i].x<=t) i++;
    87         ans+=(f[i].x-t)*max3(0,f[i-1].v,s[j-1].v);
    88         t=f[i].x;
    89     }
    90     while(j<M){
    91         if(s[j].x<=t) j++;
    92         ans+=(s[j].x-t)*max3(0,f[i-1].v,s[j-1].v);
    93         t=s[j].x;
    94     }
    95     cout<<ans<<endl;
    96     return 0;
    97 }
    方法二 AC 码:
      1 #include <algorithm>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <string>
      5 #include <cmath>
      6 #include <iostream>
      7 #include <map>
      8 #include <queue>
      9 #include <set>
     10 #include <vector>
     11  
     12 using namespace std;
     13  
     14  
     15 #define max3(x, y, z) max(max((x), (y)), (z))
     16 #define min3(x, y, z) min(mix((x), (y)), (z))
     17 #define pb push_back
     18 #define ppb pop_back
     19 #define mk make_pair
     20 #define pii pair<int, int>
     21 #define pll pair<long long, long long>
     22  
     23  
     24 #define debug_l(a) cout << #a << " " << (a) << endl
     25 #define debug_b(a) cout << #a << " " << (a) << " "
     26 #define testin(filename) freopen((filename) ,"r",stdin)
     27 #define testout(filename) freopen((filename) ,"w",stdout)
     28  
     29  
     30 #define close_sync (ios::sync_with_stdio(false))
     31  
     32  
     33  
     34 typedef long long ll;
     35 typedef unsigned long long ull;
     36  
     37  
     38 const double PI  = 3.14159265358979323846264338327;
     39 const double E   = exp(1);
     40 const double eps = 1e-6;
     41  
     42 const int INF    = 0x3f3f3f3f;
     43 const int NINF   = 0xc0c0c0c0;
     44 // int maxn   = 3e3 + 5;
     45 // int MOD    = 1e9 + 7;
     46  
     47  
     48 template <typename T>
     49 void print_arr(T *arr, int arr_len)
     50 {
     51     for (int i = 0; i < arr_len; i++)
     52         cout << arr[i] << " ";
     53     cout << endl;
     54 }
     55  
     56 /*==================================begin=======================================*/
     57 const int maxn = 1e5+5;
     58 struct Node
     59 {
     60     int x,v;
     61 };
     62  
     63 Node f[maxn],s[maxn];
     64  
     65 int cmp(Node n1, Node n2){
     66     return n1.x<n2.x;
     67 }
     68  
     69 void print_arr(Node a[],int n){
     70     for(int i=0;i<n;i++)
     71         cout<<"("<<a[i].x<<","<<a[i].v<<")";
     72     cout<<endl;
     73 }
     74  
     75 int main()
     76 {
     77     //testin("data.in");
     78     close_sync;
     79     int N,M,T;
     80     cin>>N>>M>>T;
     81     for(int i=0;i<N;i++) cin>>f[i].x>>f[i].v;
     82     for(int i=0;i<M;i++) cin>>s[i].x>>s[i].v;
     83     sort(f,f+N,cmp); sort(s,s+M,cmp);
     84      
     85     // print_arr(f,N);print_arr(s,M);
     86  
     87     set<int> ss;
     88     for(int i=0;i<N;i++) ss.insert(f[i].x);
     89     for(int i=0;i<M;i++) ss.insert(s[i].x);
     90     ss.insert(T);
     91      
     92     // for(set<int>::iterator it=ss.begin();it!=ss.end();++it)
     93     //  cout<<*it<<" ";
     94     // cout<<endl;
     95  
     96     f[N].x=T;f[N].v=f[N-1].v;
     97     s[M].x=T;s[M].v=s[M-1].v;
     98  
     99     set<int>::iterator it=ss.begin();
    100     ll ans=0;
    101     for(int i=0,j=0,t=0;t<T;){
    102         while(i+1<N+1 && f[i+1].x<=t) i++;
    103         while(j+1<M+1 && s[j+1].x<=t) j++;
    104         ans+=(*(++it)-t)*(max3(f[i].v,s[j].v,0));
    105         // debug_b(t),debug_b(*it),debug_b(max3(f[i].v,s[j].v,0)),debug_b(i),debug_l(j);
    106         t=*it;
    107     }
    108     cout<<ans<<endl;
    109  
    110     return 0;
    111 }
  • 相关阅读:
    怎么重新启动网卡
    @JsonProperty的使用
    JAVA中的反射机制
    spring的IOC入门案例
    spring的IOC底层原理
    maven+Spring环境搭建
    SpringMVC与Struts2区别与比较总结
    Struts2面试题
    oracle自增序列创建
    Hibernate分页查询报错
  • 原文地址:https://www.cnblogs.com/TianyuSu/p/9406218.html
Copyright © 2011-2022 走看看