Codeforces 1213B-Bad Prices

Problem Description:

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 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 (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 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 a1,a2,…,an (1≤ai≤106), where ai 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

Sample Output:

3
0
1
8
2

比赛时在这题上卡到比赛结束,到最后也没看明白题在说什么。赛后看题解才明白这题大概意思就是有多少个数,数后面有小于它的数。

解题思路:从右向前不断更新minPrice,数出有几个数比minPrice大
AC代码:

#include<bits/stdc++.h>
using namespace std;

int a[1000000];
int main(void) {
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        for(int i = 0;i < n;i++) {
            scanf("%d", &a[i]);
        }
        int cnt = 0;
        int right_min = INT_MAX;
        for(int i = n - 1;i >= 0;i--) {
            if(a[i] > right_min) {
                cnt++;
            }
            right_min = min(a[i], right_min);
        }
        cout<<cnt<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值