POJ 2828-Buy Tickets(线段树)
2024-10-10 03:57:47
题意:
有n个人,每人有一定的价值,给n个安排,每次安排有两个数 p,v p是这个人前面人的个数 (直接插在第p个人后面其他人后移),v是它的价值,n个安排后 求最终的价值序列。
分析:
越在后面的安排越是他的最终位置,所有我们要从后向前做,每次找前面有p个空位的位置,找到后空位减一,用线段树来维护区间的空位数。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 200010
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int num[N<<],a[N],v[N],n,pos;
void build(int l,int r,int rt){
num[rt]=(r-l)+;//初始全部空位
if(l==r)return;
int m=(l+r)>>;
build(lson);
build(rson);
}
void update(int s,int l,int r,int rt){
num[rt]--;//对应区间空位减一
if(l==r){
pos=l;
return;
}
int m=(l+r)>>;
if(s<=num[rt<<])update(s,lson);//先找左子树
else{
s-=num[rt<<];
update(s,rson);//再找右子树 所需空位-左子树空位
}
}
int main()
{
int tmp[N];
while(~scanf("%d",&n)){
build(,n,);
for(int i=;i<n;++i){
scanf("%d%d",&a[i],&v[i]);
}
for(int i=n-;i>=;i--){
update(a[i]+,,n,);//自己需要一个空位
tmp[pos]=v[i];
}
for(int i=;i<=n;++i)
printf("%d ",tmp[i]);
printf("\n");
}
return ;
}
最新文章
- Google-glog 日志库使用手记
- AngularJS的MVC中C的实现
- POJ 2923 Relocation (状态压缩,01背包)
- 非常出色的一款WinForm窗体重绘GUI类库源码
- zhuan:点滴记录——Ubuntu 14.04中gedit打开文件出现中文乱码问题
- AS3.0面向对象的写法,类和实例
- border-radius讲解2
- Windows系统下python3中安装pyMysql
- 常用的freemark语法(三)
- Roman to Integer(将罗马数字转成整数)
- vue 图片懒加载 vue-lazyload
- <;锋利的jQuery>;读书笔记
- vue上传图片
- 利用Python制作简单的小程序:IP查看器
- 使用html2canvas生成一张图片
- VUE中的v-show和v-if
- 使用脚手架快速搭建React项目
- Linux学习笔记(第十二章)
- SZU2
- 洛谷——P1190 接水问题
热门文章
- Java Timer, TimerTask
- PHP发送微信模版消息
- c++ 小片段
- [转载]C# 中Web.config文件的读取与写入
- 团体程序设计天梯赛-练习集L2-011. 玩转二叉树
- Windows下的Memcache安装与测试教程
- nginx上用fastcgi配置python环境
- cannot find w3wp.exe in VS
- HDU 2992 Hotel booking(BFS+DFS 或者 SPFA+Floyd)
- EdasStudio 开发工具用户手册