专栏首页Don的成长史【Codeforces】1213B - Bad Prices

【Codeforces】1213B - Bad Prices

版权声明:本文为博主原创文章,遵循 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:

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

解题思路:

果然 用进非退,本来我就菜还很久没有写过题了。今晚打开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代码:

#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 版权协议,转载请附上原文出处链接和本声明。 ...

    喜欢ctrl的cxk
  • 【PAT甲级】Counting Ones

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

    喜欢ctrl的cxk
  • 【Codeforces】1220A - Cards

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

    喜欢ctrl的cxk
  • Codeforces 626E Simple Skewness(暴力枚举+二分)

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

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

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

    attack
  • 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛- A. Banana

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

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

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

    ShenduCC
  • hdu----(5045)Contest(数位dp)

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

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

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

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

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

    Gxjun

扫码关注云+社区

领取腾讯云代金券