求助关于 P4375 [USACO18OPEN]Out of Sorts G
  • 板块学术版
  • 楼主lxzy_
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/4/22 14:06
  • 上次更新2023/11/5 00:15:10
查看原帖
求助关于 P4375 [USACO18OPEN]Out of Sorts G
67493
lxzy_楼主2021/4/22 14:06

这道题目我改变了一下第一篇题解的方法,即离散化后,答案为:

max(1,前x个数中>x的数的个数,后x个数中<x的数的个数)

但是一直只能拿30pts

直接把求后x个数中<x的数的个数的那一部分去掉也只能那30pts

求助:

#include<cstdio>
#include<iomanip>
#include<algorithm>
#define lowbit(i) i&(-i)
using namespace std;
const int N=1e5+50;
struct point{
	int s,pos;
}g[N];
bool vis[N];
int a[N],c[N];
int n;
bool cmp(point p,point q){return p.s<q.s;}
inline int Max(int a,int b){return a>b?a:b;}
inline void Insert(int x,int k){for( ;x<=n;x+=lowbit(x)) c[x]+=k;}
inline int Query(int x)
{
	int ans=0;
	for( ;x>0;x-=lowbit(x)) ans+=c[x];
	return ans;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&g[i].s),g[i].pos=i;
	sort(g+1,g+1+n,cmp);
	for(int i=1;i<=n;i++){
		if(g[i].s==g[i-1].s) a[g[i].pos]=a[g[i-1].pos];
		else a[g[i].pos]=a[g[i-1].pos]+1;
	}
	
	//计算前i个数中大于i的数的个数
	int ans=1;
	for(int i=1;i<=n;i++){
		Insert(a[i],1);
		ans=Max(ans,Query(n)-Query(i));
	}
	for(int i=1;i<=n;i++) c[i]=0;
	
	//计算后i个数中小于i的数的个数
	for(int i=n;i>=1;i--){
		Insert(a[i],1);
		ans=Max(ans,Query(i-1));
	}
	printf("%d\n",ans);
	return 0;
}
2021/4/22 14:06
加载中...