这道题的题意开始我理解错了,我还以为是这个数后面必须递减呢;搞得我没看懂案例;打脸了;
题意:给你n个数字,对于一个数字,如果它的后面的数字有小于它的,那么这个一天就是Bad day,所以ans++;
求有多少天是这样的?
一看1e6了,枚举肯定爆了;
后来我分析了一下:一个数后面,只要比它小就行,那我可以用set来搞,因为set可以直接排序,然后访问最小值;
然后只需要在输入的时候到起来就对了;
思路就是这样的:
从后往前放,如果set的头部元素比当前放进来的元素小,那么这天就是Bad day;
然后为了降低时间复杂度我只需要把输入倒起来,存入a[]中,其实也可以不用;因为我后面可以从n-1—0遍历;
#include<bits/stdc++.h>
using namespace std;
#define maxn 150000+10
const int INF=0x3f3f3f3f;
int T,n,a[maxn];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=n-1;i>=0;i--)scanf("%d",a+i);
int ans=0;
set<int> Set;
for(int i=0;i<n;i++){
if(!Set.empty()&&*Set.begin()<a[i])ans++;//如果非空&&最小元素小于a[i],那么这天就是Bad day;
Set.insert(a[i]);
}
printf("%d\n",ans);
}
return 0;
}