POJ2182(排队问题)
Lost Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10695 | Accepted: 6865 |
Description
N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.
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.
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
* Line 1: A single integer, N
* 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.
* 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
* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.
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=8005; int n; int deg[MAXN]; int vis[MAXN]; int ans[MAXN]; int seek(int k) { int pos=0; for(int i=1;i<=n;i++) { if(!vis[i]) { pos++; if(pos==k) return i; } } } int main() { while(scanf("%d",&n)!=EOF) { memset(vis,0,sizeof(vis)); deg[0]=0; for(int i=1;i<n;i++) { scanf("%d",°[i]); } for(int i=n-1;i>=0;i--) { int pos=seek(deg[i]+1); vis[pos]=1; ans[i]=pos; } for(int i=0;i<n;i++) { printf("%d\n",ans[i]); } } return 0; }
转化为排队问题,利用线段树求解.
#include"cstdio" #include"cstring" #include"algorithm" using namespace std; const int MAXN=8005; struct node{ int l,r; int n; }a[MAXN*4]; 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+1); if(l==r) return; int mid=(l+r)>>1; build(rt<<1,l,mid); build((rt<<1)|1,mid+1,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<<1].n) update(rt<<1,pos,i); else update((rt<<1)|1,pos-a[rt<<1].n,i); a[rt].n=a[rt<<1].n+a[(rt<<1)|1].n; } int main() { int n; while(scanf("%d",&n)!=EOF) { build(1,1,n); pos[0]=0; for(int i=1;i<n;i++) { scanf("%d",&pos[i]); } for(int i=n-1;i>=0;i--) { update(1,pos[i]+1,i); } for(int i=0;i<n;i++) { printf("%d\n",que[i]); } } return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· dotnet 源代码生成器分析器入门
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 一文搞懂MCP协议与Function Call的区别
· 提示词工程师自白:我如何用一个技巧解放自己的生产力