zoukankan      html  css  js  c++  java
  • usaco-2.2-subset-pass

    开始自己用的DFS居然超时,改用DP法,呵呵,秒杀!

    /*
    ID: qq104801
    LANG: C++
    TASK: subset
    */
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <cstdio>
    
    using namespace std;
    
    int n;
    size_t d[800]={0};
    
    void test()
    {    
        freopen("subset.in","r",stdin);
        freopen("subset.out","w",stdout);
        cin>>n;
        int sum=n*(n+1)/2;
        if(sum & 0x1u)
        {
            cout<<0<<endl;
            return;
        }
        sum>>=1;
        d[0]=1;
        for(int i=1;i<=n;++i)
            for(int j=sum;j>=i;--j)
                d[j]+=d[j-i];
        cout<<d[sum]/2<<endl;
    }
    
    int main () 
    {        
        test();        
        return 0;
    }

    testdata:

    USER: cn tom [qq104801]
    TASK: subset
    LANG: C++
    
    Compiling...
    Compile: OK
    
    Executing...
       Test 1: TEST OK [0.005 secs, 3372 KB]
       Test 2: TEST OK [0.005 secs, 3372 KB]
       Test 3: TEST OK [0.008 secs, 3372 KB]
       Test 4: TEST OK [0.005 secs, 3372 KB]
       Test 5: TEST OK [0.005 secs, 3372 KB]
       Test 6: TEST OK [0.005 secs, 3372 KB]
       Test 7: TEST OK [0.005 secs, 3372 KB]
    
    All tests OK.
    
    Your program ('subset') produced all correct answers! This is your submission #3 for this problem. Congratulations!
    
    Here are the test data inputs:
    
    ------- test 1 ----
    7
    ------- test 2 ----
    15
    ------- test 3 ----
    24
    ------- test 4 ----
    31
    ------- test 5 ----
    36
    ------- test 6 ----
    39
    ------- test 7 ----
    37
    
    Keep up the good work!
    Thanks for your submission!
    /***********************************************

    看书看原版,原汁原味。

    不会英文?没关系,硬着头皮看下去慢慢熟练,才会有真正收获。

    没有原书,也要网上找PDF来看。

    网上的原版资料多了去了,下载东西也到原始下载点去看看。

    你会知其所以然,呵呵。

    ***********************************************/

  • 相关阅读:
    day02
    Hive_分区排序(Distribute By)
    flink添加水位线
    SparkSQL读写JDBC
    spark累加器及UDTF
    datax同步json中文乱码问题
    mysql踩过的坑
    spark算子
    spark分区计算方式
    git操作
  • 原文地址:https://www.cnblogs.com/dpblue/p/3955625.html
Copyright © 2011-2022 走看看