题目传送门
。
解法:
经典贪心策略啊。
如果两个点的位置之间有别的点对。
那么我们应该先消除别的点对。正确性显然嘛。
代码实现:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int w[110000],s[110000],n;
int lowbit(int x) {return x&-x;}
int get_sum(int x) {int ans=0;while(x>0) {ans+=s[x];x-=lowbit(x);}return ans;}
void change(int x,int d) {while(x<=2*n) {s[x]+=d;x+=lowbit(x);}}
int main() {
scanf("%d",&n);memset(w,0,sizeof(w));int ans=0;
for(int i=1;i<=2*n;i++) {
int x;scanf("%d",&x);
if(w[x]==0) {change(i,1);w[x]=i;}
else {ans+=get_sum(i)-get_sum(w[x]-1)-1;change(w[x],-1);}
}printf("%d\n",ans);
return 0;
}

本文介绍了一种基于贪心策略的经典算法实现方案,通过消除中间点对的方式优化问题解决过程。利用位运算和数据结构实现了高效的算法流程。
&spm=1001.2101.3001.5002&articleId=79632259&d=1&t=3&u=fc41c5f8d92e4e3bab78957aa2154c7c)
195

被折叠的 条评论
为什么被折叠?



