01背包 URAL 1073 Square Country

题目传送门

 1 /*
 2     题意:问n最少能是几个数的平方和
 3     01背包:j*j的土地买不买的问题
 4     详细解释:http://www.cnblogs.com/vongang/archive/2011/10/07/2200721.html
 5 */
 6 #include <cstdio>
 7 #include <algorithm>
 8 #include <cmath>
 9 #include <cstring>
10 using namespace std;
11
12 const int MAXN = 6e4 + 10;
13 const int INF = 0x3f3f3f3f;
14 int dp[MAXN];
15
16 int main(void)        //URAL 1073 Square Country
17 {
18     //freopen ("I.in", "r", stdin);
19
20     int n;
21     while (scanf ("%d", &n) == 1)
22     {
23         memset (dp, 0, sizeof (dp));
24         for (int i=1; i<=n; ++i)
25         {
26             dp[i] = dp[i-1] + 1;
27             for (int j=2; j<=sqrt (n*1.0); ++j)
28             {
29                 if (i >= j * j)    dp[i] = min (dp[i], dp[i-j*j] + 1);
30                 else    break;
31             }
32         }
33
34         printf ("%d\n", dp[n]);
35     }
36
37
38     return 0;
39 }
时间: 2024-11-05 14:40:43

01背包 URAL 1073 Square Country的相关文章

URAL 1073 Square Country(DP)

Square Country 大意: 买一块边长为 a 的正方形地需要的钱数是 a^2, 现在输入N为钱的数目,求最少购买地的块数可以凑够N. 思路:DP,由背包思想推出来的dp[i] = min(dp[i], dp[j-i*i]+1);  方块都是由正方形组成的,所以是i*i,循环的时候也是i*i. #include <stdio.h> #define min(a, b) ((a) > (b) ? (b) :(a)) int n; int dp[60005]; int main() {

URAL 1073 Square Country(DP)

题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 :其实就是求x12+x22+--+Xn2中的最小的n. 1 //1073 2 #include <stdio.h> 3 #include <iostream> 4 #include <math.h> 5 6 using namespace std ; 7 8 int a[

ural 1073. Square Country 完全背包

点击打开链接 1073. Square Country Time limit: 1.0 second Memory limit: 64 MB There live square people in a square country. Everything in this country is square also. Thus, the Square Parliament has passed a law about a land. According to the law each citiz

ural 1073. Square Country

1073. Square Country Time limit: 1.0 secondMemory limit: 64 MB There live square people in a square country. Everything in this country is square also. Thus, the Square Parliament has passed a law about a land. According to the law each citizen of th

Ural 1073 Square Country (DP)

题目地址:Ural 1073 DP水题.也可以说是背包. #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include

URAL 1698. Square Country 5(记忆化搜索)

题目链接 题意 : 自守数的定义:如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数.例如5*5=25,则5就是自守数.让你求不超过n位的自守数有多少 思路 : 实际上,自守数还有两个性质:以他为后几位的两个数相乘,乘积的后几位仍是这个自守数.刚好n位的自守数一定是两个,当然1位的时候0和1是没有算进去的,但是题目中1是要加上的,所以从第0位深搜枚举到第n位.还有另一种方法,因为一个数是自守数当且仅当这个数是另一个自守数的后缀,2000位的自守数只有两个,所以存下来直接数也行 1 #

hdu 3466 Proud Merchants(0-1背包+排序)

题目来源:hdu 3466 Proud Merchants Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 3595 Accepted Submission(s): 1500 Problem Description Recently, iSea went to an ancient country. For

poj 3345 Bribing FIPA 【树形dp + 01背包】

Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 1337 Description There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (Interna

HDU 3446 有贪心思想的01背包

Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4500    Accepted Submission(s): 1873 Problem Description Recently, iSea went to an ancient country. For such a long time, it was