zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    MySQL常用命令记录
    VM新安装centos7无法连接网络的问题
    nginx + tomcat实现负载均衡
    Redis集群分布(Windows版)
    7.2 基础知识ArrayMap
    7.1 基础知识Android消息处理机制
    6.5 Android硬件访问服务使用反射
    6.4 Android硬件访问服务编写HAL代码
    6.3 Android硬件访问服务APP代码
    6.2、Android硬件访问服务编写系统代码
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7266751.html
Copyright © 2011-2022 走看看