1 条题解

  • 0
    @ 2021-6-15 14:36:34

    C++ :

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    struct P{
        int x,y,v,t;
    }p[200];
    int cross(P a,P b){
        return a.x*b.y-a.y*b.x;
    }
    int dis(P a){
        return a.x*a.x+a.y*a.y;
    }
    bool cmp(P a,P b){
        int tmp=cross(a,b);
        if(tmp<0)return true;
        if(tmp>0)return false;
        return dis(a)<dis(b);
    }
    int n,T;
    int dp[2][40001];
    int main(){
    	//freopen("gold.in","r",stdin);
    	//freopen("gold.out","w",stdout);
        int cas=0;
        while(scanf("%d%d",&n,&T)!=EOF){
            for(int i=0;i<n;i++)
                scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].t,&p[i].v);
            sort(p,p+n,cmp);
            int pre=0,cur=1;
            memset(dp,0,sizeof(dp));
            for(int i=0;i<n;i++){
                if(i>0&&cross(p[i],p[i-1])!=0){
                    swap(pre,cur);
                    memcpy(dp[cur],dp[pre],sizeof(dp[pre]));
                }
                else if(i>0){
                    p[i].v+=p[i-1].v;
                    p[i].t+=p[i-1].t;
                }
                for(int t=p[i].t;t<=T;t++)
                    dp[cur][t]=max(dp[cur][t],dp[pre][t-p[i].t]+p[i].v);
            }
            printf("%d\n",dp[cur][T]);
        }    
    }  
    
    

    Java :

    import java.util.*;
    import java.math.*;
    public class Main {
        static int n, T;
        static Point[] a = new Point[201];
        static int[][] dp = new int[2][40010];
        static class Point implements Comparable{
            int x, y, t, v;
            Point(int x,int y,int t,int v){
                this.x = x;
                this.y = y;
                this.t = t;
                this.v = v;
            }
            @Override
            public int compareTo(Object arg0) {
                Point tmp = (Point)arg0;
                int t = x*tmp.y - y*tmp.x;
                if(t == 0){
                    int X = x*x + y*y;
                    int Y = tmp.x*tmp.x+tmp.y*tmp.y;
                    if(X < Y) return -1;
                    else if(X == Y) return 0;
                    else return 1;
                }
                return t;
            }
        }
        static boolean line(int i,int j){
            int t = a[i].x*a[j].y - a[i].y*a[j].x;
            return t == 0;
        }
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int ca = 0;
            while(in.hasNext()){
                n = in.nextInt();
                T = in.nextInt();
                for(int i = 0;i < n; ++i){
                    a[i] = new Point(in.nextInt(),in.nextInt(),in.nextInt(),in.nextInt());
                }
                for(int i = 0;i <= T; ++i) dp[0][i] = dp[1][i] = 0;
                Arrays.sort(a, 0, n);
                int weight = 0, value = 0,t = 0;
                for(int i = 0;i < n; ++i){
                    if(i-1 >= 0 && line(i-1,i)){
                        weight += a[i].t;
                        value += a[i].v;
                    }else{
                        t ^= 1;
                        weight = a[i].t;
                        value = a[i].v;
                    }
                    for(int j = T;j >= 0; --j){
                        dp[t][j] = Math.max(dp[t][j], dp[t^1][j]);
                        if(j >= weight) 
                            dp[t][j] = Math.max(dp[t][j], dp[t^1][j-weight] + value);
                    }
                }
                int ans = 0;
                for(int i = T;i >= 0; --i)ans = Math.max(ans, dp[t][i]);
                System.out.println(  ans);
            }
            
    
        }
    
    }
    
    
    • 1

    信息

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