• Distinct Values


    Distinct Values

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2858    Accepted Submission(s): 923


    Problem Description
    Chiaki has an array of n positive integers. You are told some facts about the array: for every two elements ai and aj in the subarray al..r (li<jr), aiajholds.
    Chiaki would like to find a lexicographically minimal array which meets the facts.
     

     

    Input
    There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

    The first line contains two integers n and m (1n,m105) -- the length of the array and the number of facts. Each of the next m lines contains two integers li and ri (1lirin).

    It is guaranteed that neither the sum of all n nor the sum of all m exceeds 106.
     

     

    Output
    For each test case, output n integers denoting the lexicographically minimal array. Integers should be separated by a single space, and no extra spaces are allowed at the end of lines.
     

     

    Sample Input
    3
    2 1
    1 2
    4 2
    1 2
    3 4
    5 2
    1 3
    2 4
     

     

    Sample Output
    1 2
    1 2 1 2
    1 2 3 1 1
     

     

    Source
     
     
    这题看起来用数组模拟都可以,但是会超时.
    不过做了不知道为什么不过.
     
     1 #include <bits/stdc++.h>
     2 #define N 100005
     3 using namespace std;
     4 struct Node{
     5     int x,y;
     6 
     7 }node[N];
     8 bool cmp(Node a,Node b){
     9     if(a.x==b.x){
    10         return a.y>b.y;
    11     }
    12     return a.x<b.x;
    13 }
    14 set<int> s;
    15 int ans[N];
    16 int n;
    17 int main(){
    18     scanf("%d",&n);
    19     while(n--){
    20         s.clear();
    21         int m,k;
    22         scanf("%d%d",&m,&k);
    23         for(int i=1;i<=m;++i)
    24             s.insert(i);
    25         for(int i=0;i<k;++i){
    26             scanf("%d%d",&node[i].x,&node[i].y);
    27         }
    28         sort(node,node+k,cmp);
    29         int xn = 1,yn = 0;
    30         for(int i=0;i<k;++i){
    31             if(node[i].y<=yn)
    32                 continue;
    33             if(yn>=node[i].x){
    34                 while(xn<node[i].x)
    35                     s.insert(ans[xn++]);
    36                 while (yn<node[i].y) {
    37                     ans[++yn] = *s.begin();
    38                     s.erase(s.begin());
    39                 }
    40             }else if(yn<node[i].x){
    41                 while(yn<node[i].x)
    42                     ans[++yn] = 1;
    43                 while(xn<node[i].x)
    44                     s.insert(ans[xn++]);
    45                 if((*s.begin())==1)
    46                     s.erase(s.begin());
    47                 while(yn<node[i].y){
    48                     ans[++yn] = (*s.begin());
    49                     s.erase(s.begin());
    50                 }
    51             }
    52         }
    53         while(yn<m)
    54             ans[++yn] = 1;
    55         for(int i=1;i<=m;i++)
    56             printf("%d%c", ans[i], i==m?'
    ':' ');
    57     }
    58     return 0;
    59 }

    下面是我不过的代码:

     1 #include <bits/stdc++.h>
     2 #define N 200005
     3 using namespace std;
     4 struct Node{
     5     int x,y;
     6 
     7 }node[N];
     8 bool cmp(Node a,Node b){
     9     if(a.x==b.x){
    10         return a.y>b.y;
    11     }
    12     return a.x<b.x;
    13 }
    14 set<int> s;
    15 int ans[N];
    16 int n;
    17 int main(){
    18     scanf("%d",&n);
    19     while(n--){
    20         for(int i=1;i<100005;i++)
    21             s.insert(i);
    22         int m,k;
    23         scanf("%d%d",&m,&k);
    24         for(int i=0;i<k;i++){
    25             scanf("%d%d",&node[i].x,&node[i].y);
    26         }
    27         sort(node,node+k,cmp);
    28         for(int j = node[0].x;j<=node[0].y;j++){
    29             ans[j] = *s.begin();
    30             s.erase(s.begin());
    31         }
    32         int xn = node[0].x,yn = node[0].y;
    33         for(int i=1;i<k;i++){
    34             if(node[i].y<=yn)
    35                 continue;
    36             if(yn>=node[i].x&&node[i].y>yn){
    37                 for(int j = xn;j<node[i].x;j++){
    38                     s.insert(ans[j]);
    39                 }
    40                 for(int j = yn+1;j<=node[i].y;j++){
    41                     ans[j] = *s.begin();
    42                     s.erase(s.begin());
    43                 }
    44                 xn = node[i].x,yn = node[i].y;
    45             }else if(yn<node[i].x){
    46                 for(int j = xn;j<=yn;j++){
    47                     s.insert(ans[j]);
    48                 }
    49                 for(int j = node[i].x;j<=node[i].y;j++){
    50                     ans[j] = *s.begin();
    51                     s.erase(s.begin());
    52                 }
    53                 xn = node[i].x,yn = node[i].y;
    54             }
    55         }
    56         for(int i=1;i<=m;i++){
    57             if(ans[i]==0){
    58                 printf("1 ");
    59             }else{
    60                 printf("%d ",ans[i]);
    61             }
    62         }
    63         printf("
    ");
    64         s.clear();
    65     }
    66     return 0;
    67 }
     
  • 相关阅读:
    php 开发最好的ide: PhpStorm
    mediawiki 的使用
    php 的简单易用的调式方法,打印方法
    mysql 管理、备份、还原及查询的图形化gui工具
    遇到了一个问题,php数组的
    CentOS如何安装linux桌面?
    Linux mkdir 如何递归创建目录?
    VMware
    Linux中setup命令command not found如何解决?
    PotPlayer 如何设置多屏幕全屏播放
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/9362543.html
Copyright © 2020-2023  润新知