版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42449444/article/details/100168290
Problem Description:
Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for n last days:
,
,…,
, where
is the price of berPhone on the day i.
Polycarp considers the price on the day i to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if n = 6 and a = [3,9,4,6,7,5], then the number of days with a bad price is 3 — these are days 2 (
= 9), 4 (
= 6) and 5 (
= 7).
Print the number of days with a bad price.
You have to answer t independent data sets.
Input Specification:
The first line contains an integer t (1 ≤ t ≤ 10000) — 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 n integers
,
,…,
(1 ≤
≤
), where
is the price on the i-th day.
It is guaranteed that the sum of n over all data sets in the test does not exceed 150000.
Output Specification:
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:
代码语言:javascript复制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:
代码语言:javascript复制3
0
1
8
2
解题思路:
果然 用进非退,本来我就菜还很久没有写过题了。今晚打开codeforces Rand#583(Div.3)竞赛题集的时候,比赛还有半个多小时结束,结果我连签到题都TLE啦。。。。。
这题大意说白了就是一句话判断一个数列中,有多少个数后面存在比它小的数。然后我第一次用了双重for循环,提交之后Time limit exceeded on test 3啦,然后加了一行ios::sync_with_stdio(false);来取消cin和stdin的同步后 提交还是TLE。就很无奈啊 在QQ上问了一下大佬 芋圆西米露(点击紫色字体即可链接到她的博客主页),企图让她在比赛结束前抢救一哈我(这时候还剩10分钟)。大佬因为太困了想睡觉意识模糊所以一开始理解错了题目意思,说这题是并归排序求逆序对数。。。时间流逝 抢救失败,大佬说从后往前跑一遍就好了,然后我试着写了一下AC啦。AC的瞬间觉得我真的好蠢啊 大脑不清醒。。。。。换个思维来说这题不就是从后往前不断更新最小的数,判断前面有几个数比当前最小数要大吗?
AC代码:TLE代码:
代码语言:javascript复制#include <bits/stdc .h>
using namespace std;
#define Up(i,a,b) for(int i = a; i < b; i )
int main()
{
ios::sync_with_stdio(false); //取消cin和stdin的同步
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int a[n];
Up(i,0,n)
{
cin >> a[i];
}
int cnt = 0; //统计有多少个数后面的数比该数要小
Up(i,0,n)
{
Up(j,i 1,n)
{
if(a[j] < a[i])
{
cnt ;
break;
}
}
}
cout << cnt << endl;
}
return 0;
}
AC代码:
代码语言:javascript复制#include <bits/stdc .h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i )
#define Down(i,a,b) for(int i = a; i >= b; i--)
int main()
{
ios::sync_with_stdio(false); //取消cin和stdin的同步
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int a[n];
Up(i,0,n-1)
{
cin >> a[i];
}
int cnt = 0, _ = a[n-1]; //cnt记录前面有多少个数比该数要大,_表示当前最小数
Down(i,n-1,0)
{
_ = min(_,a[i]);
if(a[i] > _)
{
cnt ;
}
}
cout << cnt << endl;
}
return 0;
}