CodeForces - 1213B Bad Prices (思维)

CodeForces - 1213B Bad Prices

题目

Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days: a1,a2,…,an, where ai is the price of berPhone on the day i.

Polycarp considers the price on the day ii to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n6 and a=[3,9,4,6,7,5], then the number of days with a bad price is 3 — these are days 2 (a2=9), 4 (a4=6) and 5 (a5=7).

Print the number of days with a bad price.

You have to answer t independent data sets.


Input

The first line contains an integer t (1≤t≤100000) — the number of sets of input data in the test. Input data sets must be processed independently, one after another.

Each input data set consists of two lines. The first line contains an integer n (1≤n≤150000) — the number of days. The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤106), where aiai is the price on the i-th day.

It is guaranteed that the sum of nn over all data sets in the test does not exceed 150000.


Output

Print t integers, the j-th of which should be equal to the number of days with a bad price in the j-th input data set.


Sample Input

5
6
3 9 4 6 7 5
1
1000000
2
2 1
10
31 41 59 26 53 58 97 93 23 84
7
3 2 1 2 3 4 5

Sample Output

3
0
1
8
2

题目大意:

找到有多少个数存在后面的数比该数小

题目分析:

我们可以看到,数据的范围是比较大的,从前往后暴力扫很有可能会超时,
所以我们从后往前,先令最后一个为min,比较 min(最后一个)和倒数第二个, 如果 min 小,那么 ans+1;
如果倒数第二个小,就让min变为倒数第二个,再比较 min和倒数第三个,以此类推,具体看代码,有详解。

AC代码

#include <stdio.h>
int main()
{
	long int t,j,day,ans=0;
	scanf("%ld",&t);
 	for(j=0;j<t;j++)
	{
	scanf("%ld",&day);
	long int i=0,a[day];	
	for(i=0;i<day;i++)
	scanf("%ld",&a[i]);
	
	for (i=day-2;i>=0;i--)//我比较懒,没有 再设置一个变量 min 直接用数组里的数a[day-1]代替了
	{
	if(a[i]>a[day-1])
		{
			ans++;
		}
	else {	
	a[day-1]=a[i];
	}		
		}	
	printf("%ld\n",ans);
	ans=0;
	}	
	return 0;
 }

我来要赞了,如果觉得解释还算详细,可以学到点什么的话,点个赞再走吧
欢迎各位路过的dalao 指点,提出问题。

发布了50 篇原创文章 · 获赞 50 · 访问量 2754

猜你喜欢

转载自blog.csdn.net/weixin_45691686/article/details/104231350