/ Vijos / 讨论 / 采药 /

我是女生,刚学,搜索写的没有超时,但是wa了,有人能看看吗

package yrh;
    
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Yrh2 
    {
        public static int n = 0, t_max = 0;
        public static int [] f = new int [11111],t = new int [1111], value = new int [1111];
        public static int ans = -1;
        
        public static void init()
        {
            Scanner sc = new Scanner(System.in);
            t_max = sc.nextInt();
            n = sc.nextInt();
            int [] f = new int [11111];
            int ans = -1;
            Arrays.fill(f, -1);
            f[0] = 0;
            for (int i = 1; i <= n; i++)
            {
                int t = sc.nextInt(),value = sc.nextInt(),j = 0;
                for (j = t_max; j >= t; j--)
                    if (f[j] < f[j-t] + value)
                    {
                        f[j] = f[j-t] + value;
                        if (ans < f[j])
                            ans = f[j];
                    }
            }
            System.out.print(ans);
        }
        
        public static void dfs(int pre, int t_now, int value_now)
        {
            int i = 0;
            f[t_now] = value_now;
            for (i = pre; i <= n; i++)
            {
                if ((t_now + t[i] <= t_max)&&(f[t_now + t[i]] < f[t_now] + value[i]))
                {
                    dfs(i + 1,t_now + t[i],f[t_now] + value[i]);
                }
            }
        }
        public static void Memory_search()
        {
            Scanner sc = new Scanner(System.in);
            t_max = sc.nextInt();
            n = sc.nextInt();
            Arrays.fill(f, -1);
            f[0] = 0;
            for (int i = 1; i <= n; i++)
            {
                t[i] = sc.nextInt();
                value[i] = sc.nextInt();
            }
            dfs(1,0,0);
            for (int i = t_max; i >= 0; i--)
                if (ans < f[i])
                    ans = f[i];
            System.out.print(ans);
        }
        public static void main(String[] arg0)
        {
        //  init();
            Memory_search();
        }
    }

69 条评论

  • @ 2015-04-16 19:40:17

    刚学?你5年前注册的号??!!

  • @ 2015-04-16 19:37:40

    半年不来vijos了,今天来签个到

  • @ 2015-04-16 19:32:55

    非记忆化搜索不可能过的,死心吧

  • @ 2015-04-16 19:31:25

    您好,为什么要强调您是女生呢

  • @ 2015-04-15 17:56:09

    什么鬼

  • @ 2015-04-15 17:56:04

    擦。。

  • @ 2015-04-15 17:55:52
        #include <cstdio>
        #include <algorithm>
        #include <cstring>
        using namespace std;
        typedef long long LL;
        
        struct Change{
            int x , y , z , s1 , s2;
        };
        
        const int N = 100010;
        Change a1[4 * N] , a2[4 * N];
        LL f[8 * N] , ans;
        int t;
        
        bool Dox(Change A , Change B){return A.x < B.x;}
        
        LL Min(LL x , LL y){
            if (x == -1) return y;
            if (y == -1) return x;
            return min(x , y);
        }
        
        LL Ask(int s , int l , int r , int x , int y){
            if (l == x && r == y) return f[s];
            int mid = (l + r) / 2;
            if (y <= mid) return Ask(s * 2 , l , mid , x , y);
            else if (x > mid) return Ask(s * 2 + 1 , mid + 1 , r , x , y);
            else return Min(Ask(s * 2 , l , mid , x , mid) , Ask(s * 2 + 1 , mid + 1 , r , mid + 1 , y));
        }
        
        void Putin(int s , int l , int r , int x , LL z){
            if (l == x && r == x){
                f[s] = Min(z , f[s]);
                return;
            }
            int mid = (l + r) / 2;
            if (x <= mid) Putin(s * 2 , l , mid , x , z);
            else Putin(s * 2 + 1 , mid + 1 , r , x , z);
            f[s] = Min(f[s * 2] , f[s * 2 + 1]);
        }
        
        void Work(){
            int n , m;
            scanf("%d %d" , &n , &m);
            memset(a1 , 0 , sizeof(a1));
            memset(a2 , 0 , sizeof(a2));
            int t = 0;
            for(int i = 1; i <= n; i ++)
                scanf("%d %d %d" , &a1[i].x , &a1[i].y , &a1[i].z);
            if (m == 1){
                ans = 0;
                return;
            }
            sort(a1 + 1 , a1 + 1 + n , Dox);
            for(int i = 1; i <= n; i ++){
                a2[i].x = a1[i].x; a2[i + n].x = a1[i].y;
                a2[i].s1 = i; a2[i].s2 = 1;
                a2[i + n].s1 = i; a2[i + n].s2 = 2;
            }
            sort(a2 + 1 , a2 + 1 + 2 * n , Dox);
            t = 1; int last = 1; bool bb = 1;
            for(int i = 1; i <= 2 * n; i ++){
                if (a2[i].x != last){
                    t ++;
                    if (bb && a2[i].x >= m){
                        bb = 0;
                        m = t;
                        if (a2[i].x > m) t ++;
                    }
                    last = a2[i].x;
                }
                if (a2[i].s2 == 1) a1[a2[i].s1].x = t;
                else a1[a2[i].s1].y = t;
            }
            memset(f , 255 , sizeof(f));
            Putin(1 , 1 , t , 1 , 0);
            a1[0].x = 1; ans = -1;
            bb = 1;
            for(int i = 1; i <= n; i ++) if (a1[i].y == 1){
                bb = 0;
            }
            for(int i = 1; i <= n; i ++){
                LL tt = -1;
                if (a1[i - 1].x >= a1[i].y) tt = Ask(1 , 1 , t , a1[i].y , a1[i - 1].x);
                if (tt != -1){
                    tt += a1[i].z;
                    Putin(1 , 1 , t , a1[i].x , tt);
                    if (a1[i].x >= m) ans = Min(ans , tt);
                }
            }
            ans = ans;
        }
        
        int main(){
            freopen("a.in" , "r" , stdin);
            freopen("a.out" , "w" , stdout);
        
            scanf("%d" , &t);
            for(int i = 1; i <= t; i ++){
                Work();
                printf("Case #%d: %lld\n" , i , ans);
            }
        
            return 0;
        }
        ```
    
  • @ 2015-04-15 17:53:28

    您好,为什么要强调您是女生呢

  • @ 2015-04-08 20:24:56

    您好,为什么要强调您是女生呢

  • @ 2015-04-04 20:24:08

    您好,为什么要强调您是女生呢

  • @ 2015-04-04 16:50:27

    您好,为什么要强调您是女生呢

  • @ 2015-04-03 16:41:19

    =。=要大叔给你教嘛,大叔水平可高呢

  • @ 2015-04-03 05:30:03

    您好,为什么要强调您是女生呢

  • @ 2015-03-31 18:47:37

    =.=女生果然令人浮想联翩...

  • @ 2015-03-29 19:17:58

    搜索什么鬼《《
    另外……您好,为什么要强调您是女生呢

  • @ 2015-03-29 09:55:17

    您好,为什么要强调您是女生呢

    • @ 2015-03-29 14:55:12

      否则大家都不会理睬她的吧.

    • @ 2015-10-22 16:27:32

      小岛也是可爱的女孩子 prprpr

  • @ 2015-03-28 21:59:42

    类名换成Main试试看

  • @ 2015-03-28 21:59:27

    我看到标题果断戳进来

    • @ 2015-04-03 17:14:59

      您的Email居然还是.tk的我也是醉了

  • @ 2015-03-28 13:04:12

    您好,为什么要强调您是女生呢

    • @ 2015-03-28 16:02:51

      看到标题我的想法同上

信息

ID
1104
难度
4
分类
动态规划 | 背包 点击显示
标签
递交数
16859
已通过
6541
通过率
39%
被复制
39
上传者