Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贪心+树状数组

Posted by bugbugft at 2023-05-03 20:11:32 on Problem 1201
本题卡的比较严,基本上好想的只有差分约束和树状数组+贪心。在这里给一个自己写的AC代码以供参考,并没有优化,屎山将就看看吧,也为在算法设计课里苦苦挣扎的同学们和明年要上这课学弟们给一个答案,欢迎指正。
#include <iostream>
#include <algorithm>
#define MAXN 500000
using namespace std;
int dp[MAXN]={0},dp2[MAXN]={0},n=0,m,l=0;//n为最少选择点数目,l为最右边界
struct Node
{
	int a, b, c;//结构体t方便之后连携排序
}t[MAXN];
bool cmp(Node t1,Node t2)
{
	return t1.b<t2.b;
}
long long lowbit(long long x) 
{
	return x & -x;
}
void add(int index, long long value) 
{
	while (index <= l) 
	{ 
		dp[index] += value; // 更新当前的块
		index += lowbit(index); // 加上一个自己的长度,补上空缺,得到下一个块
	}
}
long long sum(int index) {
	long long sum = 0;
	while (index > 0) {
		sum += dp[index];
		index -= lowbit(index);
	}
	return sum;
}
int main()
{
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>t[i].a>>t[i].b>>t[i].c;
		if(l<t[i].b) l=t[i].b;
	}
	sort(t+1,t+m+1,cmp);//将b作为判断从小到大携带a、c排序
	t[0].a=0;t[0].b=0;t[0].c=0;
	for(int i=1;i<=m;i++)
	{
		int b=t[i].b;
		if(t[i].a>t[i-1].b)
		{	
			for(int j=1;j<=t[i].c;j++)
			{
				add(b,1);
				dp2[b--]=1;
			}
		}
		else
		{
			if(sum(t[i-1].b)-sum(t[i].a-1)<t[i].c)
			{
				int p=t[i].c-(sum(t[i-1].b)-sum(t[i].a-1));
				for(int j=1;j<=p;j++)
				{
					for(;;)
					{
						if(dp2[b]==0)
						{
							add(b,1);
							dp2[b--]=1;
							break;
						}
						else
						{
							b--;
						}
					}
				}
			}
		}
	}
	n=sum(l);
	cout<<n;
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator