codeforces 189A

原题连接:

  http://codeforces.com/problemset/problem/189/A

题意:

  给你一个长度为n的布条,按给定的a,b,c三种长度切布条(得到的必须满足a,b,c三种之一)能得到的最多布条数;

思路:

  (1)正常思考:那就是a,b,c之中先按最短的来切,然后第二短的,最后按最长的

  (2)但会有一些特殊的数据,如:53 10 11 23;(1)的想法得到的是4(即3给10,1个23),但正确答案是5(即3个11,2个10);

  (3)根据(2)的特殊情况,那么就在添加一种切的方法:a,b,c之中先按第二短的来切,然后最短的,最后按最长的;最后取一下最大值即可;

  ps:还有一种用动态规划的方式,怎奈本人水平过低,不会!!!!!!!!

  1 #include<cstdio>
  2 #include<string>
  3 #include<iostream>
  4 #include<algorithm>
  5
  6 using namespace std;
  7
  8 int main()
  9 {
 10     int n,a[3],d1=1,d2=1;
 11     int la,lb,lc,i,j,k,x1,x2,x3,flag=0;
 12     cin>>n>>a[0]>>a[1]>>a[2];
 13     sort(a,a+3);
 14     la=n/a[0];
 15     lb=n/a[1];
 16     lc=n/a[2];
 17     for(i=la;i>=0;i--)
 18     {
 19     //    printf("i=%d\n",i);
 20         x1=n-i*a[0];
 21     //    printf("!x1=%d\n",x1);
 22         if(x1==0)
 23         {
 24             d1=i;
 25             flag=1;
 26             break;
 27         }
 28         for(j=0;j<=lb;j++)
 29         {
 30         //    printf("j=%d\n",j);
 31             x2=x1-j*a[1];
 32         //    printf("@x2=%d\n",x2);
 33             if(x2==0)
 34             {
 35                 d1=i+j;
 36                 flag=1;
 37                 break;
 38             }
 39             if(x2<0)
 40                 break;
 41             for(k=0;k<=lc;k++)
 42             {
 43             //    printf("k=%d\n",k);
 44                 x3=x2-k*a[2];
 45             //    printf("#x3=%d\n",x3);
 46                 if(x3==0)
 47                 {
 48                     d1=i+j+k;
 49                     flag=1;
 50                     break;
 51                 }
 52                 if(x3<0)
 53                     break;
 54             }
 55             if(flag)
 56                 break;
 57         }
 58         if(flag)
 59             break;
 60     }
 61     flag=0;
 62     for(i=lb;i>=0;i--)
 63     {
 64     //    printf("i=%d\n",i);
 65         x1=n-i*a[1];
 66     //    printf("!x1=%d\n",x1);
 67         if(x1==0)
 68         {
 69             d2=i;
 70             flag=1;
 71             break;
 72         }
 73         for(j=0;j<=la;j++)
 74         {
 75     //        printf("j=%d\n",j);
 76             x2=x1-j*a[0];
 77     //        printf("@x2=%d\n",x2);
 78             if(x2==0)
 79             {
 80                 d2=i+j;
 81                 flag=1;
 82                 break;
 83             }
 84             if(x2<0)
 85                 break;
 86             for(k=0;k<=lc;k++)
 87             {
 88     //            printf("k=%d\n",k);
 89                 x3=x2-k*a[2];
 90     //            printf("#x3=%d\n",x3);
 91                 if(x3==0)
 92                 {
 93                     d2=i+j+k;
 94                     flag=1;
 95                     break;
 96                 }
 97                 if(x3<0)
 98                     break;
 99             }
100             if(flag)
101                 break;
102         }
103         if(flag)
104             break;
105     }
106 //    cout<<"d1="<<d1<<" d2="<<d2<<endl;
107     if(d1<d2)
108         d1=d2;
109     cout<<d1<<endl;
110
111     return 0;
112 }

欢迎大家评论!!!

时间: 2025-01-07 07:14:54

codeforces 189A的相关文章

CodeForces 189A Cut Ribbon

Cut Ribbon Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 189A64-bit integer IO format: %I64d      Java class name: (Any) Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way

CodeForces 189A 166E 【DP &#183;水】

非常感谢 Potaty 大大的援助使得我最后A出了这两题DP ================================== 189A : 求切分后的ribbon最多的数目,不过要求切分后只能存在a or b or c 的长度 O(n)的效率:遍历下来求 f[i - a].f[i - b]. f[i - c] 中的最大值 如果i - a || b || c 的值小于0那么跳过 来一张图,过程非常清晰 当然,初始化对f 数组置-INF,否则可能出错 //#pragma comment(lin

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Codeforces Round #408 (Div. 2) B

Description Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x?=?i. The probl