【codechef】Towers(比较难想到方法的模拟题)
来源:互联网 发布:appstore mac os 编辑:程序博客网 时间:2024/06/05 11:11
There are N towers in a city. All N towers are placed in a single row.Score of tower i is the the number of towers visible to its right side.
Tower j will be visible to Tower i if it satisfies the following condition,
- If height of tower j is less than the height of tower i, then tower j will be visible to tower i unless it is blocked by some tower whose height is greater than or equal to the height of tower i
- If height of tower j is greater than height of tower i, then Tower j will be visible if it not blocked by any tower whose height is greater than or equal to the height of Tower j
Find the score of each tower.
Input
First line consists of the number T which denotes the number of test cases.
In each test case, the first line contains N, which denotes the number of towers.
Next line contains N integers which denote the height of towers.
Output
For each test case, print N integers which denote the score of each tower.
Constraints
1 <= T <= 100
1 <= N <= 10^5
1 <= height of each tower <= 10^9
Example
Input:294 2 3 1 5 2 7 6 931 2 2Output:6 4 4 3 3 2 2 1 01 1 0
Explanation
Example case 1.
For tower 1, Towers 2,3,4,5 will be visible (as they are not blocked by any tower whose height is greater than tower 1). Tower 6 will be blocked by 6.Tower 7 will be visible as it is not blocked by any bigger tower in front of it. Tower 8 will be blocked by tower 7. Tower 9 will be visible because it's height is greater than the blocking tower (which is tower 5).
Example Case 2.For tower 1,
Tower 2 will be visible. Tower 3 will be blocked by tower 2.
http://www.codechef.com/KRKS2015/problems/K15C
当时做的时候超时,方法不太好。大神的代码方法很巧妙~
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int main(){int t;scanf("%d",&t);while(t--){long long int n;scanf("%lld",&n);long long int arr[n],b[n];long long int i,k,j;for(i=0;i<n;i++){scanf("%lld",&arr[i]);b[i]=0;}for (i=n-1;i>=0;i--){for(k=i+1;k<n;k++){if(arr[k]>arr[i]){b[i]=1+b[k]; //求后面比它高的塔的个数 break;}}}for(int i=0;i<n;++i)cout<<b[i]<<" ";cout<<endl;for (i=0;i<n;i++){long long temp=0;for (k=i+1;k<n;k++){if(arr[k]<=arr[i]) //找后面比它矮的 temp++;else{temp+=(b[k]+1);break;} //找到比它高的,加上,再break }printf("%lld ",temp);}}return 0; }
- 【codechef】Towers(比较难想到方法的模拟题)
- 【codechef】 Special Economic Zone(二分图匹配,难想到)
- codeforces-#479B. Towers(模拟)
- 【codechef】Sereja and Votes(模拟)
- POJ1182 并查集 食物链 比较难想到 的问题 并查集 中层次 题
- 【codechef】Devu and his Class(巧妙的 模拟,分类讨论)
- 想到了一个比较简单的方法判断指针是已被释放
- 由 toArray( )方法想到的
- 笔试题:乱序求第n大(小)的数。我能想到的最好的方法。
- 由一道道题想到的(sizeof)
- codechef December Challenge 2012 Book Exercises 模拟题
- 模拟数字滤波器的变化的三种方法的比较(继续整理)
- zoj Capture the Flag 比较难的模拟题
- 一个比较笨的模拟登陆其他网站的方法
- 从jquery的.filter()方法想到的
- 10706 - Number Sequence----------思维题(比较难以想到)暴力搜就行
- 想到了一种进度条的表达方法
- 想到一个做垃圾博客的方法
- 静态库、共享库和动态加载库
- Win8 Metro(C#)数字图像处理--2.42图像光照效果算法
- 又免费用了一年的TexturePacker,给Texturepakcer打个广告
- final关键字
- list排序
- 【codechef】Towers(比较难想到方法的模拟题)
- Android5.0及5.1开启多用户的方法
- 如何把一个数组循环右移k位
- 面向基于英特尔® 架构的平台的实时端到端 H.265/HEVC 解决方案
- 关于GetSystemTimeAdjustment中提到的time of day
- 关于数组
- excel多次替换
- Win8 Metro(C#)数字图像处理--2.43图像马赛克效果算法
- Web端编写(四)——查看会议议程