POJ 2182 Lost Cows(树状数组,暴力解法)

2014年03月20日 02:00:55 阅读数:1938 标签: ACM 更多

个人分类: 数据结构--树状数组ACM--题解汇总★★ACM--技巧题practice again

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013480600/article/details/21574467

POJ 2182 Lost Cows(树状数组,暴力解法)

http://poj.org/problem?id=2182

题意:

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.

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.

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

分析:

其实这道题目只要会手算用例就能暴力解决。

假设读入题目给的数组a[n],其中a[1]=0

这道题目只给出了在i之前且比位于第i个位置的值小的值有多少个,我们在纸上分析用例可知,最后一个数的值肯定是a[n]+1.

然后我们从后往前推,且初始化一个数组vis[n]为全1。vis[i]==1表示当前值为i的数还没出现。首先我们推出最后一个数的值为a[n]+1,所以我们标记vis[a[n]+1]=0,接着我们推n-1的值,假设a[n-1]=3,那么就是在n-1的数之前有3个还未出现的数比它小,那么我们从vis[1]开始扫描,找到第4个1的位置就是a[n-1]的值。然后把这第4个1的位置vis[x]标记0.接下去找n-2位置的值,以此类推即可。

<span style="font-size:18px;">#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN =10000;
int a[MAXN];//初始
int vis[MAXN];//标记1
int ans[MAXN];//最终计算的结果
int main()
{int n;while(scanf("%d",&n)==1&&n){a[1]=0;for(int i=2;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++)vis[i]=1;ans[n]=a[n]+1;vis[ans[n]]=0;for(int i=n-1;i>=1;i--){int num = a[i];int j;for(j=1;i<=n;j++){if(vis[j]==1){num--;if(num<0){vis[j]=0;break;}}}ans[i]=j;}for(int i=1;i<=n;i++)printf("%d\n",ans[i]);}return 0;
}</span>

poj 2182 给你每个数前面有几个数比他小让你输出次数的编号相关推荐

  1. C语言 有a,b,c,d4个数,要求按从大到小的顺序输出。

    C语言 有a,b,c,d4个数,要求按从大到小的顺序输出. 代码如下: #include <stdio.h> void main(){ int a,b,c,d,t; scanf(" ...

  2. ACMNO.41C语言-数字调序 有n个整数,使前面各数顺序向后移m个位置,最后m个数变成前面m个数,见图。写一函数:实现以上功能,在主函数中输入n个数和输出调整后的n个数

    题目描述 有n个整数,使前面各数顺序向后移m个位置,最后m个数变成前面m个数,见图. 写一函数:实现以上功能,在主函数中输入n个数和输出调整后的n个数. 输入 输入数据的个数n n个整数 移动的位置m ...

  3. 输入3个数a,b,c,要求按由小到大的顺序输出

    输入3个数a,b,c,要求按由小到大的顺序输出 #include <stdio.h> int main(){printf("请输入三个整数:");int t,a, b, ...

  4. python请输入第一个数请输入第二个数_Python小白学习之路(四)——第一次练习题...

    写在前面: 今天下雪了呢!连着两天都没有更新学习记录. 我没有偷懒呢.做了一天的练习题,昨天学的内容还没总结完,太累了就回去睡觉了 连续一周早起,强大的内心也无法支撑我疲惫的身体 今天早起做了整理.加 ...

  5. c语言中从键盘上输入三个数,将之排序后按由大到小的顺序输出,从键盘上输入3个数,将它们按由大到小的顺序排列好输出...

    vb中从键盘上输入10个数,判断有几个偶数,有几个奇数? 我给你写了,挺简单的.窗体上添加两个label控件一个command控件Dima(1To10)DimbDimcAsIntegerDimdAsI ...

  6. 【C语言】有3个数a,b,c,要求按大小顺序把它们输出(基础法)

    [C语言]有3个数a,b,c,要求按大小顺序把它们输出(基础法) 一.实现原理 二.整体源码 三.实现结果 一.实现原理 1.首先定义3个随机数变量a.b.c和1个中间变量t 2.利用if条件句来比较 ...

  7. Java黑皮书课后题第7章:*7.28(数学:组合)编写一个程序,提示用户输入10个整数,然后显示从这10个数中选出两个数的所有组合

    7.28(数学:组合) 题目 题目描述 破题 代码 运行示例 题目 题目描述 *7.28(数学:组合)编写一个程序,提示用户输入10个整数,然后显示从这10个数中选出两个数的所有组合 破题 声明一个长 ...

  8. 输入三个数,按照由大到小的顺序输出

    void swap(int *p1,int *p2)/*实现两个数交换的函数*/ { int temp; temp=*p1; *p1=*p2; *p2=temp; } void exchange(in ...

  9. [算法] 求排列组合: 从n个数中任选m个数组成一个新数

    #include <iostream> #include <vector>using namespace std;// 求排列组合算法: C(n, m): 从n个数中任选m个数 ...

最新文章

  1. 服务端监控要怎么做?
  2. vs2005 无法加载服务器控件解决办法
  3. velocity java 静态方法_java – 如何访问Velocity模板中的静态成员?
  4. 跟着锅子一步步学习32位汇编(3)---MOV和XCHG指令
  5. 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第1节 常用函数接口_6_函数式接口作为方法的返回值类...
  6. 服务器重装系统需要按什么,安装服务器系统前我们该做些什么
  7. 《Kafka权威指南》读书笔记4 Kafka消费者
  8. android 有线网络,安卓手机免费“有线”上网
  9. 东方财富代码选股_教你选股!一招搞定!
  10. 使用SDK Manager给TX2刷机且安装OpenCV3.4.0、CUDNN7.6.5、Pytorch、Miniforge(含百度云安装包)
  11. 伟大的micropython smartconfig 配网它来了!!!
  12. 策划的权限、视野与产品的最终高度
  13. android lunch menu,android lunch函数浅析
  14. 模电(三)晶体三极管
  15. 微信小程序中background-attachment:fixed兼容问题
  16. FAL:Flash 抽象层的使用
  17. 《推荐系统实践》第二章 利用用户行为数据
  18. c语言虚数变量,C语言如何表示虚数i
  19. pink老师,CSS学习day2-1
  20. 帝国论坛又开放了,帝国爱好者们可以愉快的逛论坛查资源了

热门文章

  1. java 创建Reader_java – 最佳实践:为XMLReader创建SAX解析器
  2. python用generator打印杨辉三角_python写generator输出杨辉三角遇到问题,望高手解答!...
  3. vue树形权限菜单_Vue.js 递归组件实现树形菜单
  4. python绘制曲线y=2x+5_Python数据可视化:Matplotlib绘图详解(二)
  5. 没有基础的人可以学python吗-没有任何基础的人,该如何学习Python?「附具体步骤」...
  6. 用python画圣诞树-python圣诞树
  7. python工资一般多少-Python工资多少?就业发展前景怎么样?
  8. 几行代码就搞定一个文字识别功能,同时还能转换成语音,畅快!
  9. 小米电视4A核心技术之语音识别浅析
  10. java开发 网关_SpringCloud系列之网关(Gateway)应用篇