Greedy:Graveyard Design(POJ 2100)

              

               墓地

  题目大意,给定一个整数,要你找出他的平方和组合

  太简单了。。。。不过一开始我储存平方和想降低时间,后来发现会超内存,直接用时间换空间了,游标卡尺法

  

 1 #include <iostream>
 2 #include <functional>
 3 #include <algorithm>
 4 #define MAX_N 10000001
 5
 6 using namespace std;
 7 typedef long long LL_INT;
 8
 9 static int store[MAX_N][2];//只储存开始和结尾
10
11 void Inivilize(void);
12
13 int main(void)
14 {
15     LL_INT n, sum, tmp;
16     int    s, t, num;
17
18     while (~scanf("%lld", &n))
19     {
20         sum = num = 0; s = t = 1;
21         while (1)
22         {
23             while ((tmp = (LL_INT)t*(LL_INT)t) <= n && sum < n)
24             {
25                 sum += tmp;
26                 t++;
27             }
28             if (sum == n)
29             {
30                 store[num][0] = s; store[num][1] = t;
31                 num++;
32             }
33             if (sum < n)
34                 break;
35             sum -= (LL_INT)s*(LL_INT)s; s++;
36         }
37         printf("%d\n", num);
38         for (int i = 0; i < num; i++)
39         {
40             printf("%d ", store[i][1] - store[i][0]);
41             for (int j = store[i][0]; j < store[i][1]; j++)
42                 printf("%d ", j);
43             printf("\n");
44         }
45     }
46     return EXIT_SUCCESS;
47 }

  

时间: 2024-10-12 04:05:56

Greedy:Graveyard Design(POJ 2100)的相关文章

Graveyard Design POJ - 2100

King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves. After a con

poj 2100 Graveyard Design

Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 7357   Accepted: 1816 Case Time Limit: 2000MS Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard m

POJ2100 Graveyard Design(尺取法)

POJ2100 Graveyard Design 题目大意:给定一个数n,求出一段连续的正整数的平方和等于n的方案数,并输出这些方案,注意输出格式: 循环判断条件可以适当剪支,提高效率,(1^2+2^2+..n^2)=n*(n+1)*(2n+1)/6; 尺取时一定要注意循环终止条件的判断. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <

poj 2100(尺取法)

Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6107   Accepted: 1444 Case Time Limit: 2000MS Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard m

poj 2100 尺取法(尺度法)

poj 2100 尺取法(尺度法) 题意 给你一个数N,然后找到一个连续的序列,使得这个序列中的数的平方和等于N. 输出需要注意的是第一行为解的个数,剩下的每行先输出序列的长度,然后输出序列.按照序列的长度进行降序输出. 解题思路 没啥好说的,使用尺度法,进行枚举各个区间上的数. 需要注意的是数字1的答案为: 1 1 1 代码实现 #include<cmath> #include<cstdio> #include<cstring> #include<algorit

poj 2100 Graveyard Design(尺取法)

Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves.

Greedy:Cow Acrobats(POJ 3045)

牛杂技团 题目大意:一群牛想逃跑,他们想通过搭牛梯来通过,现在定义risk(注意可是负的)为当前牛上面的牛的总重量-当前牛的strength,问应该怎么排列才能使risk最小? 说实话这道题我一开始给书上的二分法给弄懵了,后来看了一下题解发现根本不是这么一回事,其实就是个简单的贪心法而已. 这题怎么贪心呢?就是按w+s从小到大排序就好了,证明一下: 1.先证明如果不满足w+s的序列性,无论谁在谁的上面,都会违反题设:(设A在B的上面) 如果 A.s+A.w<B.s+B.w 则如果B.s<m+A

Greedy:Bound Found(POJ 2566)

   神奇密码 题目大意:就是给你一个数组,要你找出连续的数的绝对值的和最接近t的那一串,并且要找出数组的上界和下界的下标,并显示他们的和 因为这一题的数有正有负,所以必须要先把和求出来,然后排序,然后利用a(s,t)=sum(t)-sum(s)找出目标 1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 5 using namespace std; 6 7 //pair<i

Greedy:Yogurt factory(POJ 2393)

酸奶工厂 题目大意:酸奶工厂每个星期都要制造酸奶,成本每单位x,然后每个星期要生产y,然后酸奶厂有个巨大的储存室,可以无限储存酸奶,而且酸奶的品质不会变坏,每天储存要每单位花费S,求最小的成本. 简直是初中生数学题,贪心法即可,假如当前酸奶成本是X,则如果储存酸奶,则下k个星期的(如果还使用这批酸奶),则成本为x+k*s,对比k个星期的成本就可以确定用哪个了. 水题 1 #include <iostream> 2 #include <functional> 3 #include &