CSP-J 2021 插入排序

7 篇文章 2 订阅

题面
简化题面后
题意为给定一个数组,每次操作会改变一个数或查询一个数排序后的位置
无脑版:直接平衡树维护
可以发现在一个数组中如果只有一个数是无序的,那么可以直接用插排 O ( N ) O(N) O(N)排序
code:by rsjw

#include <cstdio>
#include <algorithm>
using namespace std;
//King Asgore will lead us to the Bright!
//King Asgore will lead us to the Hope!
//King Asgore will lead us to the Dream!
//Now with a smile,
//Won't you be happy about this?
//Won't you be excited about this?
struct Asgore {
	int num,val;
	bool operator<(const Asgore &ri)const {
		if(val==ri.val) return num<ri.num;
		return val<ri.val;
	}
} a[8010];
int f[8010];
void fl(int x) {
	f[a[x].num]=x;
}
int main() {
	freopen("sort.in","r",stdin);
	freopen("sort.out","w",stdout);
	int n,T,x,v,i,op;
	scanf("%d%d",&n,&T);
	for(i=1; i<=n; i++) scanf("%d",&a[i].val),a[i].num=i;
	sort(a+1,a+n+1);
	for(i=1; i<=n; i++) fl(i);
	while(T--) {
		scanf("%d%d",&op,&x);i=f[x];
		if(op==1) {
			scanf("%d",&v);
			if(a[i].val>v) {
				a[i].val=v;
				for(; i>1; i--)
					if(a[i]<a[i-1])
						swap(a[i],a[i-1]),fl(i),fl(i-1);
					else
						break;
			} else if(a[i].val<v) {
				a[i].val=v;
				for(; i<n; i++)
					if(a[i+1]<a[i])
						swap(a[i],a[i+1]),fl(i),fl(i+1);
					else
						break;
			}
		} else if(op==2) printf("%d\n",i);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值