Bad Prices ( Codeforces Round #582 )

Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for nn last days: a1,a2,,ana1,a2,…,an, where aiai is the price of berPhone on the day ii.

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 n=6n=6 and a=[3,9,4,6,7,5]a=[3,9,4,6,7,5], then the number of days with a bad price is 33 — these are days 22 (a2=9a2=9), 44 (a4=6a4=6) and 55 (a5=7a5=7).

Print the number of days with a bad price.

You have to answer tt independent data sets.

Input

The first line contains an integer tt (1t100001≤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 nn (1n1500001≤n≤150000) — the number of days. The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1061≤ai≤106), where aiai is the price on the ii-th day.

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

Output

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

Example
input
Copy
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
output
Copy
3
0
1
8
2

分析:逆序遍历,如果当前值的大于当前值最小值,就计数++,否则,更新当前最小值



#include 
#include 
#include 
#include <string>
#include 
#include 
#include 
#include 
#include <set>
#include 
#include 
#include 
using namespace std;
#define pq priority_queue
#define pql priority_queue
#define pqn priority_queue
#define v vector
#define vl vector
#define lson rt<<1, l, m  
#define rson rt<<1|1, m+1, r
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define no printf("NO\n");
#define gcd __gcd
#define cn cin>>
#define ct cout<<
#define ed <#define ok return 0 ;
#define over cout<#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++)  {cin>>a[i] ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define re return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<"     ++++++      "<
typedef long long ll;
const int N=150000+5;
const int maxn=10000+5;
const int len = 1e5+5;
typedef struct node
{
    int x,y,ankle,T;
}node;
node dp[len]; 
double getlen(node xx,node yy) { return ( (xx.x-yy.x)*(xx.x-yy.x) +(xx.y-yy.y)*(xx.y-yy.y) ); } //计算两点间距离 
int a[N];
int main()
{
    int n,k,t;
    for(cin>>t;t;t--)
    {
        k=0;
        cin>>n;
        input(n);
        int mx=a[n];
        for(int i=n-1;i>=1;i--)
        {
            if(a[i]>mx) k++;
            else mx=a[i];
        }
        cout<endl;
    }
    ok;
}

 

你可能感兴趣的:(Bad Prices ( Codeforces Round #582 ))