• POJ 2828 Buy Tickets (线段树)


    题意:给定 n 个人,每次在第 pos 位置,插入一个人,然后后面的向后移动,问你最后的顺序是什么。

    析:肯定不能模拟,要不然,肯定TLE,然后我们可以倒着考虑,最后一个人的位置是肯定能确定的,因为他就在最后才插入的,然后剩下的n-1个时,第n-1个人的相对位置也就确定了,依次,这样就可以用线段树来维护了,每个结点代表每个人区间已经放入多少人了。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 2e5 + 5;
    const int mod = 10;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    int a[maxn], b[maxn];
    int sum[maxn<<2];
    int ans[maxn];
    
    void push_up(int rt){ sum[rt] = sum[rt<<1] + sum[rt<<1|1]; }
    
    void update(int M, int val, int l, int r, int rt){
      if(l == r){
        sum[rt] = 1;
        ans[l-1] = val;
        return ;
      }
      int m = l + r >> 1;
      if(M <= m-l+1-sum[rt<<1])  update(M, val, lson);
      else  update(M - (m-l+1-sum[rt<<1]), val, rson);
      push_up(rt);
    }
    
    int main(){
      while(scanf("%d", &n) == 1){
        for(int i = 0; i < n; ++i)
          scanf("%d %d", a+i, b+i);
        memset(sum, 0, sizeof sum);
        for(int i = n-1; i >= 0; --i)
          update(a[i]+1, b[i], 1, n, 1);
        for(int i = 0; i < n; ++i){
          if(i)  putchar(' ');
          printf("%d", ans[i]);
        }
        printf("
    ");
      }
      return 0;
    }
    

      

  • 相关阅读:
    nginx限流方案的实现(三种方式)
    Pthreads并行编程之spin lock与mutex性能对比分析(转)
    C/C++中float和double的存储结构(转)
    list_entry(ptr, type, member)——知道结构体内某一成员变量地址,求结构体地址
    stderr和stdout详细解说(转)
    结构体内存分配理解
    C中的C文件与h文件辨析(转)
    访问vector元素方法的效率比较(转)
    linux c中select使用技巧——计时器(转)
    thread::id
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7266751.html
Copyright © 2020-2023  润新知