### 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:

```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```

```3
0
1
8
2```

### AC代码：TLE代码：

```#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代码：

```#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;
}```

0 条评论

• ### 【Codeforces】1217A - Creating a Character

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。 ...

• ### 【PAT甲级】Counting Ones

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。 ...

• ### 【Codeforces】1220A - Cards

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。 ...

• ### Codeforces 626E Simple Skewness(暴力枚举+二分)

E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megab...

• ### HDU 3032 Nim or not Nim?(Multi-Nim)

Problem Description Nim is a two-player mathematic game of strategy in which ...

• ### 2017 ACM-ICPC 亚洲区（乌鲁木齐赛区）网络赛- A. Banana

In the forest, there is a Banana Company that provides bananas from different pl...

• ### PAT 甲级 1003Emergency(Dijkstra最短路)

1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序...

• ### hdu----（5045）Contest（数位dp）

Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (J...

• ### Codeforces 833D Red-black Cobweb【树分治】

D. Red-black Cobweb time limit per test：6 seconds memory limit per test：256 mega...

• ### hdu 2473 Junk-Mail Filter (并查集之点的删除)

Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/...