Vijos P1159 Yuelu Mountain to fetch water

Source: Internet
Author: User
Tags pack stdin
P1159 Yuelu Mountain to fetch water Accepted
Description

Today the weather is fine, good scenery everywhere, good scenery. Butterflies busy Ah, bees are busy, information group of students more busy. Recently, because of XX reason, everyone had to go to Yuelu Mountain to lift water. 55555555~, so tired.

Information Group has a capacity of Q liters of large cylinders, because everyone is very conscious, not willing to waste water, so every time will just fill the cylinder. However, the information group does not have a bucket (or scoop) to scoop up the water, as a member of the life in the group, you have to shoulder the responsibility, to the new good to buy barrels.

The new one has p kinds of barrels, each barrel has an infinite number of ^_^, and the same price. Since everyone is very frugal, you must try to buy less barrels. If you have a variety of options, you have to choose the "smaller" option, that is, the collection of these two scenarios (composed of buckets of different sizes) in ascending order, compare the first bucket, select one of the smaller volume of the first bucket. If the first bucket is the same, compare the second bucket and choose the method as above. Otherwise, this comparison continues until the two buckets in the comparison are not consistent. For example, the collection {3,5,7, three} is better than the collection {3,6,7,8}.

In order to fill the tank full of water, you can first from the well of Yuelu Mountain to fill the bucket with water to bring back, and then poured into the cylinder. In order not to be very troublesome or to waste precious water resources, we will never pour out the water in the tank or pour the water in the bucket, nor will the water in the bucket be poured back into the well, (this would pollute the wells). Of course, a bucket can be used multiple times. For example, a bucket with a volume of 1 litres can fill a large cylinder of any capacity with water. and other combinations are going to be troublesome. format Input Format

Line 1th 1 Number q (q<=20000).

Line 2nd 1 number P (p<=100).

The next P line, one number per line, followed by the volume of each bucket. output Format

A total of 1 lines, every two numbers separated by a space, the 1th number k is the minimum number of barrels, the next K number from small to large output the capacity of each bucket. Example 1 sample input 1[copy]

3
3
5
7
sample Output 1[copy]
2 3 5
Limit

Each test point 1s source

From Facer


Originally thought is just a complete backpack, look for the path can, and then found that the path found is not the smallest dictionary order,

Then think of the path certainly not very much, so first completely backpack, and then find the path, and then found not run out ...

Then on various optimizations, various WA ...


Later looked at the other people's solution, the original is Dfsid,, that is, in the depth of a small place there must be the best solutions, so the DFS complexity is very high, then each enumeration depth, from small to large, in the specified time will find the optimal solution


So this problem can be enumerated each time with a few barrels, from 1~n, then DFS from n buckets to select I, and then complete backpack this I bucket can,,,

Evaluation Results

Compilation succeeded

Test data #0: Accepted, time = 0 ms, MEM = 3100 KiB, score = 10

Test data #1: Accepted, time = ms, Mem = 3104 KiB, score = 10

Test data #2: Accepted, time = 0 ms, mem = 3104 KiB, score = 10

Test data #3: Accepted, time = 0 ms, MEM = 3100 KiB, score = 10

Test data #4: Accepted, time = 0 ms, MEM = 3100 KiB, score = 10

Test data #5: Accepted, time = 0 ms, MEM = 3100 KiB, score = 10

Test data #6: Accepted, time = 546 ms, MEM = 3104 KiB, score = 10

Test data #7: Accepted, time = ms, Mem = 3104 KiB, score = 10

Test data #8: Accepted, time = ms, Mem = 3104 KiB, score = 10

Test data #9: Accepted, Time = 640 ms, MEM = 3100 KiB, score = 10

Accepted, time = 1278 ms, MEM = 3104 KiB, score = 100

Code:

#include #include #include #include #include #include #include #include, #include, #include #include #include #i Nclude #include #include #include #include #include #define MAXN (200000 +) #define INF 0x3f3f3f3f #define PI aco
S ( -1.0) using namespace std;

typedef long long int LLI;
int DP[MAXN];
int pack[maxn],cnt = 1;
int A[MAXN];
BOOL Flag[maxn],ll = false;

int q,n;
    BOOL Solve () {memset (dp,0,sizeof (DP));  for (int i = 1; i < cnt; i + +) {for (int j = pack[i]; j <= Q; j + +) {Dp[j] = max (Dp[j-pack[i])
        + pack[i],dp[j]);
    }} if (dp[q] = = q) return true;
return false;
        } void Dfs (int p,int lim) {if (P >= Lim) {if (Solve ()) LL = true;
    Return
        } for (int i = 1; I <= n; i + +) {if (flag[i] = = true) continue;
        Flag[i] = true;
        Pack[cnt + +] = A[i];
        DFS (p + 1,lim);
        if (LL = = true) return;
        CNT--;
    Flag[i] = false; }} int Main (){//Freopen ("In.txt", "R", stdin);//Freopen ("OUT.txt", "w", stdout);
    scanf ("%d%d", &q,&n);
    for (int i = 1; I <= n; i + +) scanf ("%d", &a[i]);
    memset (flag,false,sizeof (flag));
    Sort (A + 1,a + n + 1);
        for (int i = 1; I <= n; i + +) {DFS (0,i);
            if (LL = = True) {printf ("%d", cnt-1);
            for (int j = 1; j < Cnt-1; J + +) {printf ("%d", pack[j]);
            } printf ("%d\n", pack[cnt-1]);
        Break
}} return 0;
 }

=========================================================================

Also enclosed are my various WA codes:

#include #include #include #include #include #include #include #include, #include, #include #include #include #i Nclude #include #include #include #include #include #define MAXN (200000 +) #define INF 0x3f3f3f3f #define PI aco
S ( -1.0) using namespace std;

typedef long long int LLI;
int A[MAXN];
int DP[110][MAXN];
int vec[maxn],v = 0;
int re[maxn],cnt = 0;

int COPY[MAXN];
    BOOL Compare (int p) {if (P > CNT) return true;
    for (int i = 0; i < p && i < cnt; i + +) {if (Re[i] > Copy[i]) return false;
    } if (P < CNT) return false;
return true;
    } void Dfs (int x,int y) {//printf ("%d%d%d,%d%d\n", X,y,dp[x][y],dp[x-1][y],dp[x][y-a[x]] + a[x]);
        if (x < 1 | | Y < 0) {for (int i = 0; i < V; i + +) copy[i] = Vec[i];
        Sort (copy,copy + V);
int p = Unique (copy,copy + V)-Copy;
printf ("------");
for (int i = 0; i < P; i++) {//printf ("%d", copy[i]);       // }//printf ("\ n"); if (cnt = = 0 | |!
            Compare (p)) {for (int i = 0; i < p; i + +) re[i] = Copy[i];
        CNT = p;
    } return;
    } if (dp[x][y] = = Dp[x-1][y]) {DFS (x-1,y);
        } if (dp[x][y] = = Dp[x][y-a[x]] + a[x]) {vec[v + +] = a[x];
        DFS (x,y-a[x]);
    V--;
    }} int main () {//Freopen ("In.txt", "R", stdin);//Freopen ("OUT.txt", "w", stdout);
    int n,q,x;
    scanf ("%d%d", &q,&n);
    for (int i = 1; I <= n; i + +) scanf ("%d", &a[i]);
    Sort (A + 1,a + n + 1);
        for (int i = 1; I <= n; i + +) {for (int j = 0; J < A[i]; j + +) Dp[i][j] = Dp[i-1][j];
        for (int j = a[i]; j <= Q; j + +) Dp[i][j] = max (Dp[i-1][j],dp[i][j-a[i] + a[i]);
            if (dp[i-1][q] = = q) {x = i;
        Break
    } else x = i;
    } dfs (X,Q);
    printf ("%d", CNT); for (int i = 0; i <cnt-1;
    i + +) printf ("%d", re[i]);
    printf ("%d\n", re[cnt-1]);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.