HDU 1455 sticks

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1455

The question is: there are a pile of wooden sticks with different lengths. They are made up of neat wooden sticks, and the minimum length is required.

Well, the first time this question was done was the day before the training. At that time, I silently used one of the few algorithms I had learned: greedy. There is no unexpected wa.

Stopped.

After 10 days, I suddenly went back to the question and thought I could use the search I learned over the past few days. Then, I knocked on a DFS and Wa. At that time, I felt that I was doing the right thing. Then I went to ask the team leader. The team leader asked me to compare the AC code data, result. I found my own mistake. Alas, I am not familiar with DFS. I forgot to recover vis.

The second DFS question in my life

Bytes

#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;typedef long long LL;#define MIN(a,b) (a > b ? b : a)int N,cnt,flag,V,ok;int aa[100],ans[100],vis[100];int cmp(int a,int b){      return a > b ;}int get(int x){      int c = 0;      for (int i=1;i<=x;i++)            if (x % i == 0)ans[c++] = i;      return c;}void show(){      for (int i=0;i<N;i++)            printf("%d ",aa[i]);      printf("\n");}void DFS(int v,int t,int sum){      if (sum == 0){flag = 1;return ;}      for (int i=t;i<N && !flag;i++)      {            if (v - aa[i] >= 0 && vis[i] == 0)            {                  vis[i] = 1;                  if (v == aa[i])DFS(V,0,sum-aa[i]);                  else DFS(v-aa[i],i+1,sum-aa[i]);                  vis[i] = 0;                  if (v == aa[i] || v == V)return ;                  while (aa[i] == aa[i+1])i++;            }      }}int main(){      int cc = 1;      while (scanf("%d",&N) && N)      {            int sum = 0;            memset(aa,0,sizeof(aa));            for (int i=0;i<N;i++)            {                  scanf("%d",&aa[i]);                  sum += aa[i];            }            int m = get(sum);            sort(aa,aa+N,cmp);           // show();            for (int i=0;i<m;i++)            {                  if (aa[0] > ans[i])continue ;                  memset(vis,0,sizeof(vis));                  V = ans[i];                  flag = 0;                  DFS(ans[i],0,sum);                  if (flag )                  {                        printf("%d\n",ans[i]);                        break ;                  }            }      }      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.