poj 2100 Graveyard Design (two pointers ,尺取法)

不多说,直接代码。

/*************************************************************************
	> File Name: code/poj/2100.cpp
	> Author: 111qqz
	> Email: rkz2013@126.com 
	> Created Time: 2015年09月25日 星期五 00时42分49秒
 ************************************************************************/
 1#include<iostream>
 2#include<iomanip>
 3#include<cstdio>
 4#include<algorithm>
 5#include<cmath>
 6#include<cstring>
 7#include<string>
 8#include<map>
 9#include<set>
10#include<queue>
11#include<vector>
12#include<stack>
13#include<cctype>
14#define y1 hust111qqz
15#define yn hez111qqz
16#define j1 cute111qqz
17#define ms(a,x) memset(a,x,sizeof(a))
18#define lr dying111qqz
19using namespace std;
20#define For(i, n) for (int i=0;i<int(n);++i)  
21typedef long long LL;
22typedef double DB;
23const int inf = 0x3f3f3f3f;
24LL n;
25LL maxn;
26vector<LL>ans;
1void solve()
2{
3    LL head = 1,tail = 1;
4    LL sum = 0 ;
1    while (tail<=maxn)
2    {
3	sum = sum + tail*tail;
 1	if (sum>=n)
 2	{
 3	    while (sum>n)//主要是while,因为可能要减掉多个才能小于n
 4	    {
 5		sum -= head*head;
 6		head++;
 7	    }
 8	    if (sum==n)
 9	    {//因为要先输出答案个数...所以必须存起来延迟输出...
10		ans.push_back(head);
11		ans.push_back(tail);
12	    }
13	}
14	tail++;
15    }
16    LL sz = ans.size();
17    printf("%lld\n",sz/2);
18    for (LL i = 0 ; i<ans.size() ; i = i +2)
19    {
20	printf("%lld",ans[i+1]-ans[i]+1);
21	for ( LL j = ans[i] ; j <=ans[i+1] ; j++) printf(" %lld",j);
22	printf("\n");
23    }
1}
2int main()
3{
4  #ifndef  ONLINE_JUDGE 
5   freopen("in.txt","r",stdin);
6  #endif
7    scanf("%lld",&n);  
8    maxn = ceil(sqrt(n));
9    solve();
1 #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4	return 0;
5}