数据结构进阶过题

数据结构过题

E-营业额问题

题意

计算最小波动值的和

思路

平衡树模板题。每次读取一个值,插入平衡树,然后查其前趋和后继,取差绝对值较小的加入。

AC代码

#include<iostream>

using namespace std;

const int inf=0x3f3f3f3f;
struct treap
{
	int cnt,fa,son[2],val;
}tree[50001];
int root=0;
int inx=0;
void rotate(int x)
{
	int y=tree[x].fa,z=tree[y].fa,o=(x==tree[y].son[1]);
	tree[y].son[o]=tree[x].son[o^1];
	tree[tree[x].son[o^1]].fa=y;
	tree[x].son[o^1]=y;	tree[y].fa=x;

	tree[z].son[tree[z].son[1]==y]=x;
	tree[x].fa=z;
}
void splay(int x)
{
	for(int y;y=tree[x].fa;rotate(x))
	{
		if (tree[y].fa)
		{ 
			rotate(((tree[y].son[0]==x)==(tree[tree[y].fa].son[0]==y))?y:x);
		}
	}
	root=x;
}
void insert(int x,int a)
{
    int y=0;
	while(x&&tree[x].val!=a)
	{
		y=x;
	    x=tree[x].son[tree[x].val<a];
	}
	if (x) tree[x].cnt++;
	else 
	{
		inx++;
		x=inx;
		tree[x].cnt=1;
		tree[inx].fa=y;
		tree[inx].val=a;
		tree[y].son[a>tree[y].val]=x;
	}	
    splay(x);
}
int fro,beh;
void pre(int x,int a)
{
	if (!x) return;
	if (tree[x].val<=a)
	{
		fro=x;
	    pre(tree[x].son[1],a);
	}
	else pre(tree[x].son[0],a);
}
void suc(int x,int a)
{
	if (!x) return;
	if (tree[x].val>=a)
	{
		beh=x;
		suc(tree[x].son[0],a);
	}
	else suc(tree[x].son[1],a);
}
int n;
int main()
{
	int a,ans=0;
	scanf("%d%d",&n,&a);
	insert(root,inf);
	insert(root,-inf);//防爆炸
	insert(root,a);
	ans+=abs(a);
	for(int i=2;i<=n;i++)
	{
		scanf("%d",&a);
		
		pre(root,a);suc(root,a);
		ans+=min(abs(a-tree[fro].val),abs(tree[beh].val-a));insert(root,a);
	}
	cout << ans;
	return 0;
}

A-Double Queue

题意

1表示输入优先级为P的数;
2表示输出优先级最高的并删除;
3表示输出优先级最低的并删除;
0表示操作结束。

思路

用pair记录权值和优先级,将pair传入set。获取集合首位(最大优先级)和集合末尾(最小优先级),数据处理完毕后可以通过erase()删除。

#include <iostream>
#include <iterator>
#include <set>
using namespace std;
struct cmp{
    bool operator() (const pair<int, int> &a, const pair<int, int> &b)
    {
        return a.second > b.second;
    }
};
set<pair<int, int>, cmp> List;  
int main()
{
    int n;
    while(cin >> n){
        if(n == 0)
            break;
        if(n == 1){
            pair<int, int> t;
            cin >> t.first >> t.second;
            List.insert(t);   //用户开始排队
        }
        if(n == 2){
            if(!List.empty()){
                set<pair<int, int>, cmp>::iterator it = List.begin();
                cout << it->first;
                List.erase(it);
            }else{
                cout << 0 << endl;
            }
        }
        if(n == 3){
            if(!List.empty()){
                set<pair<int, int>, cmp>::iterator it = List.end();
                it--;
                cout << it->first << endl;
                List.erase(it); 
            }else{
                cout << 0 << endl;
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值