ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1394. Ships. Version 2

RE #10 with recursion alg!!!
Posted by YuFeng 15 Jul 2017 19:45
#include <stdio.h>
#include <algorithm>
#include <stack>
using namespace std;
#define MAXN 100
#define MAXM 10
int ship[MAXN];
int row[MAXM];
int exi[MAXN];
int N, M;

int cmp(const void* a, const void* b);
void divid(int m);
int main()
{
    scanf("%d%d", &N, &M);
    for(int i=0; i<N; i++)
        scanf("%d", &(ship[i]));
    for(int i=0; i<M; i++)
        scanf("%d", &(row[i]));

    for(int i=0; i<N; i++)
        exi[i] = 1;

    qsort(ship, N, sizeof(int), cmp);
    divid(M);

    return 0;
}

int cmp(const void* a, const void* b)
{
    int* x = (int*)a;
    int* y = (int*)b;
    return *y - *x;
}

void divid(int m)
{
    if(m > 1)
        divid(m-1);

    stack<int> s;
    int tot = row[m-1];
    int hav = 0;
    int i = 0;
    for(;;){
        while(i<N && (!exi[i] || ship[i]>(tot-hav))) i++;
        if(i >= N){
            for(;;){
                i = s.top();
                s.pop();
                hav -= ship[i];
                exi[i] = 1;
                i++;
                if(i < N)
                    break;
            }
            continue;
        }
        s.push(i);
        hav +=  ship[i];
        exi[i] = 0;
        i++;
        if(hav == tot){
            int sh_ind;
            int siz = s.size();
            printf("%d\n", siz);
            while(!s.empty()){
                sh_ind = s.top();
                s.pop();
                printf("%d ", ship[sh_ind]);
            }
            printf("\n");
            return;
        }
    }
}

####################################################
I used recursion alg. And I know there is error in this code because I didn't deal with the situation I called "data confliction" such as "5 = 2+3".But how to IMPROVE it ???

email:1813484947@qq.com



Edited by author 15.07.2017 20:00

Edited by author 16.07.2017 15:23