• Codeforces 490B Queue【模拟】


    题意还是很好理解的,根据题目给出描述条件然后求出这串QUEUE

    我的做法就是用两个数组 before[] 和 after[] 表示 ai 前面的前面的人的学号 和 ai 后面的后面的人的学号

    ex[] 表示 ai 这个人在输入的时候出现的次数,这个数组用于当人数为奇数的时候,寻找第1个人学号,只要遍历一遍1 - 10^6即可

    具体还是看代码吧 QAQ ,感觉代码还是比较好理解的。

    【这道题目 RE 起码5次,真正原因是数组没有开到位 = = ,也真是觉得自己最近没有写代码了好弱..........

     1 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
     2 #include <stdio.h>
     3 #include <iostream>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <stack>
     7 #include <queue>
     8 #include <vector>
     9 #include <algorithm>
    10 #define ll long long
    11 #define Max(a,b) (((a) > (b)) ? (a) : (b))
    12 #define Min(a,b) (((a) < (b)) ? (a) : (b))
    13 #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    14 
    15 using namespace std;
    16 
    17 const int INF = 0x3f3f3f3f;
    18 int a[200011], ex[1000011];
    19 int before[1000011], after[1000011];
    20 
    21 int main(){
    22     int i, j ,k, t, n, m;
    23     int uu, vv, cur, pos;
    24     while(EOF != scanf("%d",&n)){
    25         memset(ex, 0, sizeof(ex));
    26         memset(before, -1, sizeof(before));
    27         memset(after, -1, sizeof(after));
    28         for(i = 1; i <= n; ++i){    
    29             scanf("%d%d",&uu,&vv);
    30             after[uu] = vv;
    31             before[vv] = uu;
    32             if(uu == 0){
    33                 a[2] = vv;
    34             } else if(vv == 0){
    35                 a[n - 1] = uu;
    36             } else{
    37                 ++ex[uu];
    38                 ++ex[vv];
    39             }
    40         }
    41         if(n % 2 == 0){
    42             cur = a[2];
    43             pos = 2;
    44             while(pos != n){
    45                 pos += 2;
    46                 cur = a[pos] = after[cur];
    47             }
    48             cur = a[n - 1];
    49             pos = n - 1;
    50             while(pos != 1){
    51                 pos -= 2;
    52                 cur = a[pos] = before[cur];
    53             }
    54         } else{
    55             cur = a[2];
    56             pos = 2;
    57             while(pos != n - 1){
    58                 pos += 2;
    59                 cur = a[pos] = after[cur];
    60             }
    61             for(i = 1; i <= 1000001; ++i){  //2 nums
    62                 if(ex[i] == 1 && before[i] == -1){
    63                     break;
    64                 }
    65             }
    66             a[1] = i;
    67             cur = a[1];
    68             pos = 1;
    69             while(pos != n){
    70                 pos += 2;
    71                 cur = a[pos] = after[cur];
    72             }
    73         }
    74 
    75         for(i = 1; i <= n; ++i){
    76             printf("%d ",a[i]);
    77         }
    78         printf("
    ");
    79     }
    80     return 0;
    81 }
    82 /*
    83 8
    84 3 0
    85 5 4
    86 1 5
    87 0 7
    88 11 9
    89 9 6
    90 6 3
    91 7 1
    92 5
    93 3 0
    94 5 3
    95 0 5
    96 6 4
    97 4 2
    98 */
  • 相关阅读:
    Redis
    IDEA编码相关,解决yml编码错误导致的 java.nio.charset.MalformedInputException: Input length = 1
    文件上传和下载
    SpringBoot+Mybatis+Postman实现增删改查
    多态与反射
    正则表达式
    原码、反码、补码的用法和理解
    @Conditional & @Profile SpringBoot中自动化配置条件注解。
    Spring Boot 中的 Starter
    第一个项目~千寻在线水果商城
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4189290.html
Copyright © 2020-2023  润新知