首页 > 学院 > 开发设计 > 正文

玲珑杯round11-B:萌萌哒的第二题

2019-11-06 06:56:10
字体:
来源:转载
供稿:网友

B -- 萌萌哒的第二题

Time Limit:5s Memory Limit:128MByte

Submissions:681Solved:188

DESCRipTION

一条东西走向的河两边有都排着工厂,北边有n间工厂A提供原材料,南边有n间工厂B进行生产。现在需要在工厂A和工厂B之间建运输桥以减少运输成本。可是每个工厂B只能接受最多6个工厂A提供的材料能满足生产,而且建立的运输桥之间不能有交叉,北边的工厂A由西向东编号1~n,南边的工厂B也是一样,不能交叉的意思是如果a号工厂A跟b号工厂B之间建立了运输桥,那么不能存在c、d(c < a 且d > b) 使得c号工厂A和d号工厂b之间建立运输桥,每个工厂A只能给一个工厂B提供材料,每个工厂B只能由一间工厂A提供材料,请问在满足上述条件的情况下最多能建立多少个运输桥。(每个工厂最多有6个选择,但只能选一个)

INPUT包含多组测试数据(<=15),其中每组测试数据: 第一行一个整数n(1<= n <= 10^5) 接下来n行,第i+1行6个整数表示i号工厂B能接受的6个工厂A的编号,保证所有编号的范围是1~n,可能重复(请看样例)。OUTPUT每组数据输出一行一个整数表示最多能建立的运输桥数量。SAMPLE INPUT31 2 3 1 2 32 2 2 2 2 21 3 1 3 1 361 2 3 4 5 61 2 3 4 5 61 2 3 4 5 61 2 3 4 5 61 2 3 4 5 61 1 1 1 1 1SAMPLE OUTPUT35

题解:http://www.ifrog.cc/acm/solution/16

标称貌似是最长递增子序列,我直接用线段树了,比较无脑,虽然复杂度都是nlogn,但是后者常数大,

不加输入挂会TLE....

下面是TLE的程序,输入改成输入挂可以AC

/* http://www.ifrog.cc/acm/PRoblem/1097?contest=1013&no=1 */#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int ans, road[100005][9], dp[100005][9], tre[444444], now[100005];void Update(int l, int r, int x, int id, int c){	int m;	m = (l+r)/2;	if(l==r)	{		tre[x] = c;		return;	}	if(id<=m)		Update(l, m, x*2, id, c);	else		Update(m+1, r, x*2+1, id, c);	tre[x] = max(tre[x*2], tre[x*2+1]);}void Query(int l, int r, int x, int a, int b){	int m;	m = (l+r)/2;	if(l>=a && r<=b)	{		ans = max(ans, tre[x]);		return;	}	if(a<=m)		Query(l, m, x*2, a, b);	if(b>=m+1)		Query(m+1, r, x*2+1, a, b);}int main(void){	int n, i, j, bet;	while(scanf("%d", &n)!=EOF)	{		memset(tre, 0, sizeof(tre));		for(i=1;i<=n;i++)		{			for(j=1;j<=6;j++)			{				scanf("%d", &road[i][j]);				dp[i][j] = 0;			}			sort(road[i]+1, road[i]+7);			now[i] = 0;		}		bet = 0;		for(i=1;i<=n;i++)		{			for(j=6;j>=1;j--)			{				if(j!=6 && road[i][j]==road[i][j+1])				{					dp[i][j] = dp[i][j+1];					continue;				}				ans = 0;				if(road[i][j]==1)					dp[i][j] = 1;				else				{					Query(1, n, 1, 1, road[i][j]-1);					dp[i][j] = ans+1;				}				if(now[road[i][j]]<dp[i][j])				{					now[road[i][j]] = dp[i][j];					Update(1, n, 1, road[i][j], dp[i][j]);				}				bet = max(bet, dp[i][j]);			}		}		printf("%d/n", bet);	}	return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表