义乌集训Day9 T3 T4 ??(未完成)

STD
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
const int N=2e6+100;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
pair<int,int>p[N];
struct Q{ LL w; int id; inline bool operator < (const Q &b) const { return w>b.w; } };
int main()
{
#ifndef ONLINE_JUDGE
	freopen("mice.in","r",stdin);
	freopen("mice.out","w",stdout);
#endif
	int n=gi(),m=gi(),i,k;LL s=-n;LL ans=0,w;
	const LL inf=1e15;
	for (i=1;i<=n;i++) p[i]=make_pair(gi(),-1);
	while (m--) p[++n].x=gi(),s+=p[n].y=gi();
	if (s<0) return puts("-1"),0;
	sort(p+1,p+1+n);
	priority_queue< LL , vector<LL> , greater<LL> >q0;
	priority_queue<Q>q1;
	for (i=1;i<=n;i++)
		if (p[i].y==-1) {
			w=inf;
			if (!q1.empty()) {
				k=q1.top().id;w=p[i].x+q1.top().w;q1.pop();
				if (p[k].y) p[k].y--,q1.push((Q){-p[k].x,k});
			}
			ans+=w;
			q0.push(-p[i].x-w);
		} else {
			while (p[i].y&&!q0.empty()&&p[i].x+q0.top()<0) {
				p[i].y--;
				w=p[i].x+q0.top();q0.pop();
				ans+=w;q1.push((Q){-p[i].x-w,0});
			}
			if (p[i].y) q1.push((Q){-p[i].x,i}),p[i].y--;
		}
	cout<<ans<<endl;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;

const int N=3e6+100;

int p[N];

int n;
typedef long long LL;
int c1[N];
LL c2[N];

unsigned long long seed;

unsigned long long rd() {
    seed ^= seed << 13;
    seed ^= seed >> 7;
    seed ^= seed << 17;
    return seed;
}

int main() {
	freopen("triple.in","r",stdin);
	freopen("triple.out","w",stdout);

	int i,s1,t; LL s2;
	LL ans = 0;

	cin >> n >> seed;
	p[1] = 1;
	for (i = 2 ; i <= n; i++) {
	    p[i] = i;
	    swap(p[i], p[rd() % i + 1]);
	}

	for (i = n; i; i--) {
		s1 = s2 = 0;

		for (t = n+1-p[i]; t; t^=t&-t) {
			s1 += c1[t];
			s2 += c2[t];
		}
		ans += 1LL*s1*(s1-1)/2;
		ans -= s2;

		for (t = n+1-p[i]; t<=n; t+=t&-t) {
			c1[t] ++;
			c2[t] += s1;
		}
	}
	cout << ans <<endl;
	//for (int i = 1 ; i <= n ; i++)
	//	cout<< p[i] << " \n"[i==n];
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值