前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Codeforces】1213B - Bad Prices

【Codeforces】1213B - Bad Prices

作者头像
喜欢ctrl的cxk
发布2019-11-08 15:47:48
5750
发布2019-11-08 15:47:48
举报

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

a_{1}
a_{1}

,

a_{2}
a_{2}

,…,

a_{n}
a_{n}

, where

a_{i}
a_{i}

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 (

a_{2}
a_{2}

= 9), 4 (

a_{4}
a_{4}

= 6) and 5 (

a_{5}
a_{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

a_{1}
a_{1}

,

a_{2}
a_{2}

,…,

a_{n}
a_{n}

(1 ≤

a_{i}
a_{i}

10^{6}
10^{6}

), where

a_{i}
a_{i}

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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
  • 解题思路:
  • AC代码:TLE代码:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档