题解

85 条题解

  • -1
    @ 2016-09-13 09:44:19

    刷了这么久,总算有点长进啊

    #include <set>
    #include <map>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <cctype>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cassert>
    #include <cstdlib>
    #include <cstring>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    #define maxn (10000 + 20)
    #define inf 0x3f3f3f3f
    #define pi acos(-1.0)
    using namespace std;
    typedef long long int LLI;
    
    int dp[maxn];
    int k[maxn],m[maxn];
    
    int main() {
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        int v,n,c;
        memset(dp,-1,sizeof(dp));
        dp[0] = 0;
        scanf("%d%d%d",&v,&n,&c);
        for(int i = 1; i <= n; i++)
            scanf("%d%d",&k[i],&m[i]);
        for(int i = 1; i <= n; i ++) {
            for(int j = v; j >= 0; j --) {
                if(dp[j] == -1) continue;
                int p = min(j + k[i],v);
                if(dp[p] == -1) dp[p] = dp[j] + m[i];
                else            dp[p] = min(dp[p],dp[j] + m[i]);
            }
        }
        if(dp[v] > c || dp[v] == -1)  {
            printf("Impossible\n");
        } else {
            printf("%d\n",c - dp[v]);
        }
        return 0;
    }
    
    
  • -1
    @ 2015-10-25 21:27:54

    记录信息
    评测状态 Accepted
    题目 P1625 精卫填海(HOI)
    递交时间 2015-10-25 21:26:51
    代码语言 Pascal
    评测机 VijosEx
    消耗时间 917 ms
    消耗内存 892 KiB
    评测时间 2015-10-25 21:26:53
    评测结果
    编译成功

    Free Pascal Compiler version 2.6.4 [2014/03/06] for i386
    Copyright (c) 1993-2014 by Florian Klaempfl and others
    Target OS: Win32 for i386
    Compiling foo.pas
    foo.pas(18,22) Warning: Variable "f" does not seem to be initialized
    foo.pas(3,9) Note: Local variable "k" not used
    foo.pas(3,16) Note: Local variable "l" not used
    foo.pas(3,18) Note: Local variable "m" not used
    Linking foo.exe
    26 lines compiled, 0.1 sec , 28224 bytes code, 1644 bytes data
    1 warning(s) issued
    3 note(s) issued
    测试数据 #0: Accepted, time = 15 ms, mem = 888 KiB, score = 10
    测试数据 #1: Accepted, time = 15 ms, mem = 888 KiB, score = 10
    测试数据 #2: Accepted, time = 46 ms, mem = 888 KiB, score = 10
    测试数据 #3: Accepted, time = 31 ms, mem = 888 KiB, score = 10
    测试数据 #4: Accepted, time = 15 ms, mem = 892 KiB, score = 10
    测试数据 #5: Accepted, time = 109 ms, mem = 888 KiB, score = 10
    测试数据 #6: Accepted, time = 156 ms, mem = 888 KiB, score = 10
    测试数据 #7: Accepted, time = 109 ms, mem = 888 KiB, score = 10
    测试数据 #8: Accepted, time = 203 ms, mem = 892 KiB, score = 10
    测试数据 #9: Accepted, time = 218 ms, mem = 888 KiB, score = 10
    Accepted, time = 917 ms, mem = 892 KiB, score = 100
    代码
    program vijos1625
    var
    i,j,c,k,vt,n,l,m:longint;
    w,v:Array[0..10000] of longint;
    f:array[0..10000] of longint;
    function max(a,b:longint):longint;
    begin
    if a>b then exit(a);exit(b);
    end;
    begin
    // assign(input,'1.in');reset(input);
    //assign(output,'1.out');rewrite(output);
    readln(vt,n,c);
    for i:=1 to n do
    readln(w[i],v[i]);
    for i:=1 to n do
    for j:=c downto v[i] do
    f[j]:=max(f[j],f[j-v[i]]+w[i]);
    for i:=1 to c do
    if f[i]>=vt then
    begin
    writeln(c-i);
    exit;
    end;
    writeln('Impossible');
    //close(input);close(output);
    end.
    01背包,做完从头到尾扫一遍就好

  • -1
    @ 2014-11-04 20:26:15

    悲剧的我没有看到Impossible要首字母大写

  • -1
    @ 2014-11-04 10:53:56

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;

    struct Stone{int v,c;}s[10005];

    int v,n,c,Ans,f[10005];

    int main()
    {
    scanf("%d%d%d",&v,&n,&c);
    for(int i=1;i<=n;i++)
    scanf("%d%d",&s[i].v,&s[i].c);
    Ans=1e9;
    for(int i=1;i<=n;i++)
    for(int j=c;j>=s[i].c;j--)
    {
    f[j]=max(f[j],f[j-s[i].c]+s[i].v);
    if(f[j]>=v) Ans=min(Ans,j);
    }
    if(Ans>c) puts("Impossible");
    else printf("%d\n",c-Ans);
    return 0;
    }

  • -1
    @ 2013-07-14 17:37:41

    测试数据 #0: Accepted, time = 0 ms, mem = 660 KiB, score = 10

    测试数据 #1: Accepted, time = 0 ms, mem = 660 KiB, score = 10

    测试数据 #2: Accepted, time = 15 ms, mem = 660 KiB, score = 10

    测试数据 #3: Accepted, time = 0 ms, mem = 664 KiB, score = 10

    测试数据 #4: Accepted, time = 0 ms, mem = 660 KiB, score = 10

    测试数据 #5: Accepted, time = 62 ms, mem = 664 KiB, score = 10

    测试数据 #6: Accepted, time = 46 ms, mem = 660 KiB, score = 10

    测试数据 #7: Accepted, time = 78 ms, mem = 660 KiB, score = 10

    测试数据 #8: Accepted, time = 109 ms, mem = 660 KiB, score = 10

    测试数据 #9: Accepted, time = 171 ms, mem = 656 KiB, score = 10

    Accepted, time = 481 ms, mem = 664 KiB, score = 100

信息

ID
1625
难度
5
分类
动态规划 | 背包 点击显示
标签
递交数
3731
已通过
1353
通过率
36%
被复制
12
上传者