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

不小心刷了个最优解,来发一波code!!!

Posted by ACAccepted at 2019-05-11 09:59:11 on Problem 3038 and last updated at 2019-05-11 09:59:31
#include <cstdio>
#include <algorithm>
using namespace std;

int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0' || c>'9'){if (c=='-')f=-1;c=getchar();}
	while (c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-48;c=getchar();}
	return x*f;
}

const int MAXN=50005;
int n,m,c,ans,len1,len2,sum,tot,rest,last;

struct cow
{
	int l,r,w;
}cow1[MAXN],cow2[MAXN];

struct plane
{
	int to,num;
	
	void operator = (struct cow tmp)
	{
		to=tmp.r;num=tmp.w;
	}
}a[MAXN],tmp[MAXN];

bool cmp_cow1(struct cow a,struct cow b)
{
	return a.l<b.l;
}

bool cmp_cow2(struct cow a,struct cow b)
{
	return a.l>b.l;
}

bool cmpl(struct plane a,struct plane b)
{
	return a.to<b.to;
}

bool cmpr(struct plane a,struct plane b)
{
	return a.to>b.to;
}

inline struct cow make_cow(int l,int r,int w)
{
	struct cow tmp;
	tmp.l=l;tmp.r=r;tmp.w=w;
	return tmp;
}

inline void init()
{
	int l,r;
	n=read();m=read();c=read();
	for (int i=1;i<=n;i++)
	{
		l=read();r=read();
		if (l<r) cow1[++len1]=make_cow(l,r,read());
		else cow2[++len2]=make_cow(l,r,read());
	}
	sort(cow1+1,cow1+len1+1,cmp_cow1);
	sort(cow2+1,cow2+len2+1,cmp_cow2);
}

int main()
{
	init();
	last=0;
	for (int i=1;i<=m;i++)
	{
		sum=0;
		for (int j=1;j<=tot;j++)
		{
			if (a[j].to==i)ans+=a[j].num;
			else tmp[++sum]=a[j];
		}
		for (int j=last+1;j<=len1;j++)
		{
			if (cow1[j].l==i)tmp[++sum]=cow1[last=j];
			else break;
		}
		sort(tmp+1,tmp+sum+1,cmpl);
		tot=0;rest=c;
		for (int j=1;j<=sum;j++)
		{
			if (tmp[j].num<rest)a[++tot]=tmp[j],rest-=tmp[j].num;
			else {a[++tot]=tmp[j];a[tot].num=rest;break;}
		}
	}
	last=0;
	for (int i=m;i>=1;i--)
	{
		sum=0;
		for (int j=1;j<=tot;j++)
		{
			if (a[j].to==i)ans+=a[j].num;
			else tmp[++sum]=a[j];
		}
		for (int j=last+1;j<=len2;j++)
		{
			if (cow2[j].l==i)tmp[++sum]=cow2[last=j];
			else break;
		}
		sort(tmp+1,tmp+sum+1,cmpr);
		tot=0;rest=c;
		for (int j=1;j<=sum;j++)
		{
			if (tmp[j].num<rest)a[++tot]=tmp[j],rest-=tmp[j].num;
			else {a[++tot]=tmp[j];a[tot].num=rest;break;}
		}
	}
	printf("%d\n",ans);
	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