NC25141. [USACO 2006 Ope G]The County Fair
描述
输入描述
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.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"; }