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

用了差分约束,但是老WA检查不出为什么了。大牛帮看一下,谢谢!

Posted by yunyifly at 2008-05-28 00:17:19 on Problem 1752
不知道是不是题目理解错了。
示例输出是:
19
-5
-4
-3
-2
-1
0
4
5
6
7
8
15
18
19
20
21
25
26
27
我的输出是:
19
-5
-4
-3
-2
-1
0
4
5
6
7
8
14
15
19
20
21
25
26
27

差别在于14和18。
按我对题意的理解来看,应该两种答案都是可以的。

代码如下:

#include <stdio.h>

#define INF (1<<30)

struct Edges {
	int b;
	int e;
};

int pathpath[20002];

#define MAX 10000

int min,max;
int n,k;

Edges e[1000];
Edges es[1000];

int elen;
int eslen;

void bellman_ford() {
	int i,j;
	int nv=max-min+1;
	int *path=pathpath+MAX;
	for(i=min;i<=max;i++)
		path[i]=0;
	bool over;
	for(i=1;i<nv;i++) {
		over=true;
		//区间内至少要有k个广告的
		for(j=0;j<elen;j++) {
			if(path[e[j].e]<path[e[j].b]+k) {
				over=false;
				path[e[j].e]=path[e[j].b]+k;
			}
		}
		//区间内每个板上都放广告
		for(j=0;j<eslen;j++) {
			int temp;
			for(temp=es[j].b-1;temp<es[j].e;temp++) {
				//要求x[temp+1]>=x[temp]+1
				if(path[temp+1]<path[temp]+1) {
					over=false;
					path[temp+1]=path[temp]+1;
				}
			}
		}
		for(j=min;j<max;j++) {
			//要求x[j+1]>=x[j]
			if(path[j]>path[j+1]) {
				path[j+1]=path[j];
				over=false;
			}
		}
		for(j=max;j>min;j--) {
			//要求x[j]<=x[j-1]+1
			if(path[j]-1>path[j-1]) {
				path[j-1]=path[j]-1;
				over=false;
			}
		}
		if(over) break;
	}
	
	printf("%d\n",path[max]);
	for(i=min;i<max;i++) {
		if(path[i+1]-path[i]==1) {
			printf("%d\n",i);
		}
	}
}


int main() {
	scanf("%d%d",&k,&n);
	min=10000;
	max=-10000;
	int i,a,b;
	elen=0;
	eslen=0;
	int swaptemp;
	for(i=0;i<n;i++) {
		scanf("%d%d",&a,&b);
		if(a>b) {
			swaptemp=a;
			a=b;
			b=swaptemp;
		}
		//分成两种边
		if(b-a+1<k) {
			//一种是区间大小小于k的
			//需要在区间内的每个板上都放广告
			es[eslen].b=a;
			es[eslen].e=b+1;
			eslen++;
		}
		else {
			//另一种则是在区间内至少有k个广告就可以了
			e[elen].b=a;
			e[elen].e=b+1;
			elen++;
		}
		if(min>a) min=a;
		if(max<b+1) max=b+1;
	}
	bellman_ford();
	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