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 in squares, surely. Moreover, a length of a square side must be a positive integer amount of meters. Buying a square of land with a side a one pays a2 quadrics (a local currency) and gets a square certificate of a landowner.

One citizen of the country has decided to invest all of his N quadrics into the land. He can, surely, do it, buying square pieces 1 × 1 meters. At the same time the citizen has requested to minimize an amount of pieces he buys: "It will be easier for me to pay taxes," — he has said. He has bought the land successfully.

Your task is to find out a number of certificates he has gotten.

Input

The only line contains a positive integer N ≤ 60 000 , that is a number of quadrics that the citizen has invested.

Output

The only line contains a number of certificates that he has gotten.

Sample Input

input output
344
3

大意:你现在有n块土地,要把它分成几块正方形,问最少的正方形数目,原本以为可以贪心做,倒着过来,不过12(4+4+4 = 3) 9+1+1+1 = 4这组数据有问题

动态转移方程  dp[i] = min(dp[i],dp[i-j*j)+1)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    int dp[60010];
    int n;
    while(~scanf("%d",&n)){
        memset(dp,0,sizeof(dp));
        for(int i = 1; i <= n ;i++){
            dp[i] = dp[i-1] + 1;
            for(int j = 1; j <= sqrt((double)n+0.5); j++)
                if(j*j <= i)
                dp[i] = min(dp[i],dp[i-j*j] + 1);
        }
    printf("%d\n",dp[n]);
    }
    return 0;
}

  

时间: 2024-08-11 09:55:33

URAL1073——DP——Square Country的相关文章

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(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

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

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;

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 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 #

[Leetcode221]最大面积 Maximal Square

[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 [思路] dp square面积和三个有关 注意特殊空集条件 [代码] class Solution { public int