• CF 631C report


    Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first ri numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).

    Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence ai of length n and the description of each manager, that is value ri and his favourite order. You are asked to speed up the process and determine how the final report will look like.

    分析

      这题其实不是很难,蒟蒻的我都一眼找出了规律,首先,它不管是顺着排序还是倒着排序,如果某次操作比前边的一次操作范围大,那么前边的那次操作其实是无效的,另外,如果操作中最大的右端点到r,那么r再往后的区间是不会被修改的。因为操作的时候是修改1~r,所以前边的区间会被多次修改,而后边的区间则只会被最后那次不重叠的修改,如下图。

     假如从1到i有很多次操作,从i到j有且仅有一次操作,那么我区间(i,j]的值就能确定,如果是由大到小排,那么j的位置一定是1,反之亦然,所以定义一个头指针和尾指针,倒着扫描一遍整个序列就好。

     1 #include<iostream>
     2 #include<algorithm>
     3 using namespace std;
     4 const int N=2e5+10;
     5 int a[N],ans[N],idx[N],typ[N];
     6 int main(){
     7     int n,m;
     8     cin>>n>>m;
     9     for(int i=1;i<=n;i++)
    10         cin>>a[i];
    11     int mx=0;
    12     for(int i=1;i<=m;i++){
    13         int t,r;
    14         cin>>t>>r;
    15         idx[r]=i;
    16         typ[r]=t;
    17         mx=max(mx,r);
    18     }
    19     sort(a+1,a+mx+1);
    20     int flag,now=0,hh=1,tt=mx;
    21     for(int i=mx;i;i--){
    22         if(now<idx[i]){
    23             now=idx[i];
    24             flag=typ[i];
    25         }
    26         if(flag==1)
    27             ans[i]=a[tt--];
    28         else ans[i]=a[hh++];
    29     }
    30     for(int i=1;i<=mx;i++)
    31         cout<<ans[i]<<" ";
    32     for(int i=mx+1;i<=n;i++)
    33         cout<<a[i]<<" ";
    34     return 0;
    35 }
  • 相关阅读:
    CopyOnWriteArrayList 写时复制思想
    CAS中ABA问题的解决
    彻底解决Chrome“请停用以开发者模式运行的扩展程序”提示(亲测整合)
    解决软件安装无法自定义文件夹,自动安装在C盘 (Windows系统)
    IDEA降低注解检测级别
    大白话带你认识JVM(转)
    Windows 与 Linux (CentOS7) 之间的文件共享
    Dubbo、Zookeeper 以及 Tomcat 启动的相关问题
    创建 maven 项目的时候遇到的问题
    MyBatis 项目的 jar 包导入与源码导入
  • 原文地址:https://www.cnblogs.com/anyixing-fly/p/12612803.html
Copyright © 2020-2023  润新知