列表

详情


NC25141. [USACO 2006 Ope G]The County Fair

描述

Every year, Farmer John loves to attend the county fair. The fair has N booths (1 <= N <= 400), and each booth i is planning to give away a fabulous prize at a particular time P(i) (0 <= P(i) <= 1,000,000,000) during the day. Farmer John has heard about this and would like to collect as many fabulous prizes as possible to share with the cows. He would like to show up at a maximum possible number of booths at the exact times the prizes are going to be awarded.
Farmer John investigated and has determined the time T(i,j) (always in range 1..1,000,000) that it takes him to walk from booth i to booth j. The county fair's unusual layout means that perhaps FJ could travel from booth i to booth j by a faster route if he were to visit intermediate booths along the way. Being a poor map reader, Farmer John never considers taking such routes -- he will only walk from booth i to booth j in the event that he can actually collect a fabulous prize at booth j, and he never visits intermediate booths along the way. Furthermore, T(i,j) might not have the same value as T(j,i) owing to FJ's slow walking up hills.
Farmer John starts at booth #1 at time 0. Help him collect as many fabulous prizes as possible.

输入描述

Line 1: A single integer, N.
Lines 2..1+N: Line i+1 contains a single integer, P(i).
Lines 2+N..1+N+: These  lines each contain a single integer T(i,j) for each pair (i,j) of booths. The first N of these lines respectively contain T(1,1), T(1,2), ..., T(1,N). The next N lines contain T(2,1), T(2,2), ..., T(2,N), and so on. Each T(i,j) value is in the range 1...1,000,000 except for the diagonals T(1,1), T(2,2), ..., T(N,N), which have the value zero.

输出描述

Line 1: A single integer, containing the maximum number of prizes Farmer John can acquire.

示例1

输入:

4
13
9
19
3
0
10
20
3
4
0
11
2
1
15
0
12
5
5
13
0

输出:

3

说明:

There are 4 booths. Booth #1 is giving away a prize at time 13, booth #2 at time 9, booth #3 at time 19, and booth #4 at time 3.
Farmer John first walks to booth #4 and arrives at time 3, just in time to receive the fabulous prize there. He them walks to booth 2 (always walking directly, never using intermediate booths!) and arrives at time 8, so after waiting 1 unit of time he receives the fabulous prize there. Finally, he walks back to booth #1, arrives at time 13, and collects his third fabulous prize.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 28ms, 内存消耗: 996K, 提交时间: 2019-08-06 15:52:29

#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
 
typedef struct
{
int a,b;
}node;
 
bool cmp(const node& x,const node& y)
{
return x.a<y.a;
}
 
int d[450],cap[450][450];
node p[450];
 
int main()
{
int n,i,j,k,ans;
while(scanf("%d",&n)==1)
{
memset(d,-INF,sizeof(d));
ans=0;
for(i=1;i<=n;i++)
{
p[i].b=i;
scanf("%d",&p[i].a);
}
sort(p+1,p+1+n,cmp);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cap[i][j]);
for(k=1;p[k].b!=1;k++);
for(i=1;i<=n;i++)
if(cap[p[k].b][p[i].b]<=p[i].a)
d[i]=1;
for(i=1;i<=n;i++)
{
for(j=1;j<i;j++)
{
if(p[j].a+cap[p[j].b][p[i].b]<=p[i].a)d[i]=d[i]>d[j]+1?d[i]:d[j]+1;
}
ans=ans>d[i]?ans:d[i];
}
printf("%d\n",ans);
}
}

C++11(clang++ 3.9) 解法, 执行用时: 60ms, 内存消耗: 1888K, 提交时间: 2019-06-16 10:10:53

#include<bits/stdc++.h>
using namespace std;
int dis[410][410],f[410];
struct ljy{
	int time,date;
}a[410];
bool operator <(const ljy &x,const ljy &y){
	return x.time<y.time;
}
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].time;
		a[i].date=i;
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	cin>>dis[i][j];
	sort(a+1,a+n+1);
	int ans=0;
	for(int i=1;i<=n;i++){
		if(dis[1][a[i].date]<=a[i].time)f[i]=1;
		for(int j=1;j<i;j++)
		if(a[j].time+dis[a[j].date][a[i].date]<=a[i].time)f[i]=max(f[i],f[j]+1);
		ans=max(ans,f[i]); 
	}
	cout<<ans<<"\n";
} 

上一题