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秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#include<cctype>
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define ms(a,x) memset(a,x,sizeof(a))
#define lr dying111qqz
using namespace std;
#define For(i, n) for (int i=0;i<int(n);++i)  
typedef long long LL;
typedef double DB;
const int inf = 0x3f3f3f3f;
LL n;
LL maxn;
vector<LL>ans;


void solve()
{
    LL head = 1,tail = 1;
    LL sum = 0 ;

    while (tail<=maxn)
    {
	sum = sum + tail*tail;

	if (sum>=n)
	{
	    while (sum>n)//主要是while,因为可能要减掉多个才能小于n
	    {
		sum -= head*head;
		head++;
	    }
	    if (sum==n)
	    {//因为要先输出答案个数...所以必须存起来延迟输出...
		ans.push_back(head);
		ans.push_back(tail);
	    }
	}
	tail++;
    }
    LL sz = ans.size();
    printf("%lld\n",sz/2);
    for (LL i = 0 ; i<ans.size() ; i = i +2)
    {
	printf("%lld",ans[i+1]-ans[i]+1);
	for ( LL j = ans[i] ; j <=ans[i+1] ; j++) printf(" %lld",j);
	printf("\n");
    }
    
}
int main()
{
  #ifndef  ONLINE_JUDGE 
   freopen("in.txt","r",stdin);
  #endif
    scanf("%lld",&n);  
    maxn = ceil(sqrt(n));
    solve();
   
 #ifndef ONLINE_JUDGE  
  fclose(stdin);
  #endif
	return 0;
}