POJ2182(排队问题)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10695 | Accepted: 6865 |
Description
Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.
Given this data, tell FJ the exact ordering of the cows.
Input
* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.
Output
Sample Input
5
1
2
1
0
Sample Output
2
4
5
3
1
题意:
第一行给出cow的数目n,接下来2-n行给出每个排在第2-n各位置的cow的在其编号比其小的个数。最后按排队顺序依次给出每个cow的编号。
思路:
从后向前确定编号,设比最后一个cow小的cow数目为a,则最后一个cow的编号为所剩下cow 的第(a+1)大编号。
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
int n;
int deg[MAXN];
int vis[MAXN];
int ans[MAXN];
int seek(int k)
{
int pos=;
for(int i=;i<=n;i++)
{
if(!vis[i])
{
pos++;
if(pos==k) return i;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(vis,,sizeof(vis));
deg[]=;
for(int i=;i<n;i++)
{
scanf("%d",°[i]);
}
for(int i=n-;i>=;i--)
{
int pos=seek(deg[i]+);
vis[pos]=;
ans[i]=pos;
}
for(int i=;i<n;i++)
{
printf("%d\n",ans[i]);
} } return ;
}
转化为排队问题,利用线段树求解.
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
struct node{
int l,r;
int n;
}a[MAXN*];
int que[MAXN];
int pos[MAXN];
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].n=(r-l+);
if(l==r) return;
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
} void update(int rt,int pos,int i)
{
if(a[rt].l==a[rt].r)
{
a[rt].n--;
que[i]=a[rt].l;
return ;
} if(pos<=a[rt<<].n) update(rt<<,pos,i);
else update((rt<<)|,pos-a[rt<<].n,i);
a[rt].n=a[rt<<].n+a[(rt<<)|].n;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
build(,,n);
pos[]=;
for(int i=;i<n;i++)
{
scanf("%d",&pos[i]);
}
for(int i=n-;i>=;i--)
{
update(,pos[i]+,i);
}
for(int i=;i<n;i++)
{
printf("%d\n",que[i]);
}
} return ;
}
POJ2182(排队问题)的更多相关文章
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
- Python开发——排队问题随机模拟分析
案例:主要是基于"蒙特卡罗思想",求解排队等待时间问题 场景:厕所排队问题 1.两场电影结束时间相隔较长,互不影响: 2.每场电影结束之后会有20个人想上厕所: 3.这20个人会在 ...
- poj2182(线段树求序列第k小)
题目链接:https://vjudge.net/problem/POJ-2182 题意:有n头牛,从1..n编号,乱序排成一列,给出第2..n个牛其前面有多少比它编号小的个数,记为a[i],求该序列的 ...
- 【POJ2182】Lost Cows
[POJ2182]Lost Cows 题面 vjudge 题解 从后往前做 每扫到一个点\(i\)以及比前面小的有\(a[i]\)个数 就是查询当前的第\(a[i]+1\)小 然后查询完将这个数删掉 ...
- 【poj2182】【poj2828】树状数组/线段树经典模型:逆序查找-空位插入法
poj2182题意:有一个1~n的排列,现在给定每个人前面有多少个人的编号比他大,求这个排列是什么.n<=8000 poj2182题解: 逆序做,可以确定二分最后一个是什么,然后删除这个数.树状 ...
- 银行排队问题之单队列多窗口加VIP服务(30 分)
银行排队问题之单队列多窗口加VIP服务(30 分) 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选 ...
- PTA 银行排队问题之单队列多窗口服务(25 分)
银行排队问题之单队列多窗口服务(25 分) 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选择时,假 ...
- codevs——2956 排队问题
2956 排队问题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有N个学生去食堂,可教官规定:必须2人或3 ...
- Codevs 2956 排队问题
2956 排队问题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 有N个学生去食堂,可教官规定:必须2人或3人组成一组,求有多少种不 ...
随机推荐
- servletRequest 常用操作
package request; import java.io.IOException;import javax.servlet.ServletException;import javax.servl ...
- (翻译) Container Components
react.js javascript 这篇文章翻译自Medium的一篇文章:Container Components 选择这篇文章翻译的原因是,在刚接触React的时候,这篇文章很好的指引我了解 ...
- react build和server start
先到项目目录build项目 npm run build 项目会打包到dist文件夹下 index.html和index.js等 react的项目build后不能直接访问的问题 先执行 npm inst ...
- T-SQL简单查询语句(模糊查询)
T-SQL简单查询语句 简单查询: 1.最简单查询(查所有数据) select * from 表名: 注:* 代表所有列 select * from info 2.查询指定列 select code, ...
- python 中读取excel
第一步: 先下载一个xlrd 包 # pip install xlrd import xlrd from datetime import date, datetime file = '学生信息表.x ...
- EasyRTMP+EasyDSS实现一套完整的紧急视频回传直播与存储回放方案
需求来源 紧急视频回传云端:即拍即传.云端存储.紧急录像.云拍云录!这些需求现在可能对于我们来说比较远,大部分也是在行业中才会用到,但相信在不就的将来肯定会落地到每个人的手中,因为这是一个自我保护.自 ...
- angularcli填坑系列(持续更新...)
1.在xx.ts中引入css样式无效 @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls ...
- Java 8 default 函数
我们知道在java8之前 ,一个类实现一个接口需要实现接口所有的方法, 但是这样会导致一个问题,当一个接口有很多的实现类的时候,修改这个接口就变成了一个非常麻烦的事,需要修改这个接口的所有实现类 不过 ...
- substr扩展版:支持中文字符串截取
function d_substr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if(function_e ...
- .net 开源框架--转载
Json.NET http://json.codeplex.com/ Json.Net 是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Li ...