CF101532G Magical Indices(暴力模拟)

题面:

Alaa sometimes feels bored at work, so at such times she starts playing with a beautiful array a consisting of n integers a1, a2, ..., an.

Alaa starts counting the number of magical indices in the array a. An index x is said to be magical if it satisfying the following rules:

  1. 1 < x < n
  2. ay ≤ ax, for each y (1 ≤ y < x).
  3. ax ≤ az, for each z (x < z ≤ n).

Can you help Alaa by counting the number of magical indices in the array a.

 

Input:

The first line contains an integer T, where T is the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 106), where n is the size of the array a.

The second line of each test case contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106), giving the array a.

 

Output:

For each test case, print a single line containing the number of magical indices in the array a.

 

Example:

 

Note:

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

 

分析:

嘛,就是求数a,它前面的数都比它小,后面的数都比它大,求存在多少个这样的数a。题目中的大小比较不需要严格按照顺序,所以想到了用bef数组存下从开头到当前位置为止最大的数,aft数组存下从末尾到当前位置为止最小的数,然后遍历一次,记录下大于等于bef[i],小于等于aft[i]的数有多少个就行了。WA了一发竟然是因为没看见是都是要算入等于的情况的,只算了严格大于小于。

 

AC代码:

#include<cstdio>
#include<iostream>
using namespace std;
const int maxn = 1000010;
int t, n;
int num[maxn], bef[maxn], aft[maxn];

int main(){
	cin>>t;
	while(t--){
		cin>>n;
		for(int i = 1; i <= n; i++) scanf("%d", &num[i]);
		bef[1] = num[1];
		aft[n] = num[n];		
		for(int i = 2; i <= n; i++){
			if(num[i] > bef[i-1]) bef[i] = num[i];
			else bef[i] = bef[i-1];
		}
		for(int i = n - 1; i != 0; i--){
			if(num[i] < aft[i+1]) aft[i] = num[i];
			else aft[i] = aft[i+1];
		}
		int ans = 0;
		for(int i = 2; i < n; i++){
			if(num[i] >= bef[i-1]){
				if(num[i] <= aft[i+1]) ans++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值