2 条题解

  • 0
    @ 2025-10-8 17:00:46
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N=1e3+10;
    LL f[N][N][2];
    struct node{LL x, t;} a[N];
    bool cmp(node n1, node n2){
    	return n1.x<n2.x;
    }
    int main(){
    	int n, H, ex; scanf("%d%d%d", &n, &H, &ex);
    	for(int i=1; i<=n; i++){
    		scanf("%lld%lld", &a[i].x, &a[i].t);
    	}
    	sort(a+1, a+n+1, cmp);
    	memset(f, 0x3f, sizeof(f));
    	f[1][n][0]=max(a[1].x, a[1].t);
    	f[1][n][1]=max(a[n].x, a[n].t);
    	for(int i=1; i<=n; i++){
    		for(int j=n; j>=i; j--){
    			f[i][j][0]=min({f[i][j][0], max(f[i-1][j][0]+a[i].x-a[i-1].x
    			, a[i].t), max(f[i][j+1][1]+a[j+1].x-a[i].x, a[i].t)});
    			
    			f[i][j][1]=min({f[i][j][1], max(f[i-1][j][0]+a[j].x-a[i-1].x
    			, a[j].t), max(f[i][j+1][1]+a[j+1].x-a[j].x, a[j].t)});
    		}
    	}
    	LL ans=1e14;
    	for(int i=1; i<=n; i++){
    		ans=min(ans, min(f[i][i][0], f[i][i][1])+abs(a[i].x-ex));
    	}
    	printf("%lld\n", ans);
    	return 0;
    }
    
    • 0
      @ 2025-10-8 17:00:36
      #include<bits/stdc++.h>
      using namespace std;
      typedef long long LL;
      const int N=1e3+10;
      LL f[N][N][2];
      struct node{LL x, t;} a[N];
      bool cmp(node n1, node n2){
      	return n1.x<n2.x;
      }
      int main(){
      	int n, H, ex; scanf("%d%d%d", &n, &H, &ex);
      	for(int i=1; i<=n; i++){
      		scanf("%lld%lld", &a[i].x, &a[i].t);
      	}
      	sort(a+1, a+n+1, cmp);
      	memset(f, 0x3f, sizeof(f));
      	f[1][n][0]=max(a[1].x, a[1].t);
      	f[1][n][1]=max(a[n].x, a[n].t);
      	for(int i=1; i<=n; i++){
      		for(int j=n; j>=i; j--){
      			f[i][j][0]=min({f[i][j][0], max(f[i-1][j][0]+a[i].x-a[i-1].x
      			, a[i].t), max(f[i][j+1][1]+a[j+1].x-a[i].x, a[i].t)});
      			
      			f[i][j][1]=min({f[i][j][1], max(f[i-1][j][0]+a[j].x-a[i-1].x
      			, a[j].t), max(f[i][j+1][1]+a[j+1].x-a[j].x, a[j].t)});
      		}
      	}
      	LL ans=1e14;
      	for(int i=1; i<=n; i++){
      		ans=min(ans, min(f[i][i][0], f[i][i][1])+abs(a[i].x-ex));
      	}
      	printf("%lld\n", ans);
      	return 0;
      } 
      • 1

      USACO(112)动态规划(区间型)4:提交作业P2339 [USACO04OPEN] Turning in Homework G

      信息

      ID
      2303
      时间
      1000ms
      内存
      128MiB
      难度
      7
      标签
      (无)
      递交数
      26
      已通过
      9
      上传者