Topcoder SRM 655 DIV1 500 FoldingPaper2 递归 + 枚举

题意:给你一张长W,宽H 的纸,问你能不能者成给定的大小, 每一次折纸只能折成整数大小。

解题思路:递推+枚举   枚举给定大小的长和宽,然后套进 W,H中求最小值  , 折纸策略最优是每次折半。

解题代码:

  1 // BEGIN CUT HERE
  2 /*
  3
  4 */
  5 // END CUT HERE
  6 #line 7 "FoldingPaper2.cpp"
  7 #include <cstdlib>
  8 #include <cctype>
  9 #include <cstring>
 10 #include <cstdio>
 11 #include <cmath>
 12 #include <algorithm>
 13 #include <vector>
 14 #include <string>
 15 #include <iostream>
 16 #include <sstream>
 17 #include <map>
 18 #include <set>
 19 #include <queue>
 20 #include <stack>
 21 #include <fstream>
 22 #include <numeric>
 23 #include <iomanip>
 24 #include <bitset>
 25 #include <list>
 26 #include <stdexcept>
 27 #include <functional>
 28 #include <utility>
 29 #include <ctime>
 30 using namespace std;
 31
 32 #define PB push_back
 33 #define MP make_pair
 34
 35 #define REP(i,n) for(i=0;i<(n);++i)
 36 #define FOR(i,l,h) for(i=(l);i<=(h);++i)
 37 #define FORD(i,h,l) for(i=(h);i>=(l);--i)
 38
 39 typedef vector<int> VI;
 40 typedef vector<string> VS;
 41 typedef vector<double> VD;
 42 typedef long long LL;
 43 typedef pair<int,int> PII;
 44
 45 int find(int now,int val)
 46 {
 47     if(now == val)
 48         return 0 ;
 49     if(val > now/2)
 50         return 1;
 51     if(now % 2 == 0 )
 52         return 1 + find(now/2,val);
 53     return 1 + find(now/2+1,val);
 54 }
 55 class FoldingPaper2
 56 {
 57         public:
 58         int solve(int W,int  H, int A)
 59         {
 60             int mi = 1e9;
 61             int ok = 0 ;
 62             for(int i = 1; i <= sqrt(A) + 1;i ++)
 63             {
 64                 if(A % i == 0 )
 65                 {
 66                   if(i <= W && A/i <= H)
 67                   {
 68                   mi = min(find(W,i)+find(H,A/i),mi);
 69                   ok = 1;
 70                   }
 71                   if(i <= H && A/i <= W)
 72                   {
 73                   mi = min(find(H,i)+find(W,A/i),mi);
 74                   ok = 1;
 75                   }
 76                 }
 77             }
 78             if(ok)
 79             return mi ;
 80             else return -1;
 81
 82         }
 83
 84 // BEGIN CUT HERE
 85     public:
 86     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
 87     private:
 88     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << ‘\"‘ << *iter << "\","; os << " }"; return os.str(); }
 89     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << ‘\"‘ << endl; cerr << "\tReceived: \"" << Received << ‘\"‘ << endl; } }
 90     void test_case_0() { int Arg0 = 5; int Arg1 = 3; int Arg2 = 12; int Arg3 = 1; verify_case(0, Arg3, solve(Arg0, Arg1, Arg2)); }
 91     void test_case_1() { int Arg0 = 2; int Arg1 = 2; int Arg2 = 3; int Arg3 = -1; verify_case(1, Arg3, solve(Arg0, Arg1, Arg2)); }
 92     void test_case_2() { int Arg0 = 4; int Arg1 = 4; int Arg2 = 1; int Arg3 = 4; verify_case(2, Arg3, solve(Arg0, Arg1, Arg2)); }
 93     void test_case_3() { int Arg0 = 127; int Arg1 = 129; int Arg2 = 72; int Arg3 = 8; verify_case(3, Arg3, solve(Arg0, Arg1, Arg2)); }
 94     void test_case_4() { int Arg0 = 1; int Arg1 = 100000; int Arg2 = 100000; int Arg3 = 0; verify_case(4, Arg3, solve(Arg0, Arg1, Arg2)); }
 95     void test_case_5() { int Arg0 = 1; int Arg1 = 1; int Arg2 = 2; int Arg3 = -1; verify_case(5, Arg3, solve(Arg0, Arg1, Arg2)); }
 96
 97 // END CUT HERE
 98
 99 };
100
101 // BEGIN CUT HERE
102 int main()
103 {
104         FoldingPaper2 ___test;
105         ___test.run_test(-1);
106         return 0;
107 }
108 // END CUT HERE

时间: 2024-12-08 22:57:57

Topcoder SRM 655 DIV1 500 FoldingPaper2 递归 + 枚举的相关文章

Topcoder SRM 655 DIV1 250 CountryGroupHard

题意:不同国家的人站在一起,同一个国家的人相邻站,给你一个数组,表示 这个位置属于的国家有几个人,0可以是任何数字,问你能不能确定这个数组(只有一个解) 解题思路:搜索 解题代码: 1 // BEGIN CUT HERE 2 /* 3 4 */ 5 // END CUT HERE 6 #line 7 "CountryGroupHard.cpp" 7 #include <cstdlib> 8 #include <cctype> 9 #include <cst

Topcoder SRM 563 Div1 500 SpellCards

题意 [题目链接]这怎么发链接啊..... 有\(n\)张符卡排成一个队列,每张符卡有两个属性,等级\(li\)和伤害\(di\). 你可以做任意次操作,每次操作为以下二者之一: 把队首的符卡移动到队尾. 使用队首的符卡,对敌人造成\(d_i\)点伤害,并丢弃队首的\(l_i\)张符卡(包括你所使用的符卡).如果队列不足\(l_i\)张符卡那么你不能使用. 求出造成的伤害的总和的最大值. \(1<=n<=50, 1<=li<=50, 1<=di<=10000\) Sol

Topcoder SRM 643 Div1 250&lt;peter_pan&gt;

Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*......*pn,我们假设p0,p1,...,pn是单调不降的,那么v里存储的是下标为偶数 的N的质因数p0,p2,p4,...,p(2k).现在要求写一个程序,返回一个vector<long long>ans; ans里存储的是p0,p1,p2,...,pn. Limits Time Limit(m

SRM 618 DIV1 500

非常棒的组合问题,看了好一会,无想法.... 有很多做法,我发现不考虑顺序的最好理解,也最好写. 结果一定是两种形式 A....A   dp[n-1] A...A...A sgma(dp[j]*dp[n-j-1])( 1<=j<=n-2) 最后乘上n! 什么时候才能自己在比赛中做出一个500分来啊!!! class LongWordsDiv1 { public : int count(int n) { int i,j; fact[0] = 1; for(i = 1; i <= n; i

Topcoder SRM 619 DIv2 500 --又是耻辱的一题

这题明明是一个简单的类似约瑟夫环的问题,但是由于细节问题迟迟不能得到正确结果,结果比赛完几分钟才改对..耻辱. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define ll long long using namespace std; #define NN 370000 class Choo

Topcoder SRM 648 Div1 250

Problem 给一个长度为N的"AB"字符串S,S只含有两种字符'A' 或 'B',设pair(i,j)(0=<i<j<N)表示一对 i,j 使得S[i]='A',S[j]='B'.现给定一个K,求字符串S,使得pair(i,j)的个数恰好为K.若不存在,则返回空串. Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: [2, 50] K: [0 , N*(N-1)/2 ] Solution 若K>(N/2

Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

Problem Statement      The Happy Letter game is played as follows: At the beginning, several players enter the field. Each player has a lowercase English letter on their back. The game is played in turns. In each turn, you select two players with dif

topcoder srm 738 div1 FindThePerfectTriangle(枚举)

Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle with the following properties: The coordinates of each vertex are integers between 0 and 3000, inclusive. The perimeter of the triangle must be exactly

求拓扑排序的数量,例题 topcoder srm 654 div2 500

周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are numbered from 0 to N-1. Some pairs of rooms are connected by bidirectional passages. The passages have the topology of a tree. That is, there are exactly N-