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()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i){
        dp[i] = i;
    }
    for(int i = 1; i*i <= n; ++i){
        for(int j = i*i; j <= n; ++j){
            dp[j] = min(dp[j], dp[j-i*i]+1);
        }
    }
    printf("%d\n", dp[n]);

    return 0;
}

URAL 1073 Square Country(DP)

时间: 2024-12-30 03:55:41

URAL 1073 Square Country(DP)的相关文章

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[

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;

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 完全背包

点击打开链接 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 (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 #

URAL1073——DP——Square Country

Description 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 the country has a right to buy land. A land is sold i

ural1097 Square Country 2

Square Country 2 Time limit: 1.0 secondMemory limit: 64 MB The Square Parliament of the Square country (recall the corresponding problem from the USU 2001 personal contest) has decreed that the National Square Park be created. Of course, the Park sho

Ural 1167 Bicolored Horses (DP)

题目地址:Ural 1167 感觉这题的思路类似于背包的做法.. 先预处理出来每个马与之前所有的马的0的数量和1的数量,用数组a[0][i]和a[1][i]来表示. 然后再用数组dp[i][j]来表示当前第i个马槽最右端为第j个马时的最小值. dp的时候先枚举马槽,再用n*n枚举当前的马槽要选用的马的区间.这样总时间复杂度是O(n*n*k). 代码如下: #include <iostream> #include <cstdio> #include <string> #i