尺取法

poj2100

题意

给出一个n,如果一段连续自然数的平方的和等于n,那么输出这一段,找出满足条件的所有段。

题解

非常简单的尺取法模板题。

代码

/***    author:  zxwsbg    ***/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
using namespace std;

#define INIT(x) memset(x,0,sizeof(x))
#define eps 1e-8
#define next next_
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int maxn = 200005;
const int N = 105;

inline void read(int &x) {
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}

ll n,k,sum,tot,l,r;
inline ll f(ll x) {return x*x;}
vector<pair<ll,ll> > a;

int main() {
	ios::sync_with_stdio(false);
	l = r = sum = 1;
	cin >> n;
	while(f(l)<=n) {
		while(sum<n) {
			r++;
			sum += f(r);
		}
		if(sum==n) a.push_back({l,r});
		sum -= f(l);
		l++;
	}	
	cout<<a.size()<<endl;
	for(int i=0;i<a.size();i++) {
		cout<<a[i].second-a[i].first+1;
		for(int j=a[i].first;j<=a[i].second;j++)
			cout<<' '<<j;
		cout<<endl;
	}
	return 0;
}

hdu6119

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=6119

题意

给定n个区间,表示小熊在这些区间里面每天都会签到。然后给m张补签卡,问小熊最长可以连续签到多久。

题解

首先,假设区间之间不相交,这个问题就是一个非常裸的双指针问题。
然后考虑有些线段相交的问题,可以直接把两个相交的线段合并成一个大的线段,然后便转化成了线段不相交的问题。

代码

/***    author:  zxwsbg    ***/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
using namespace std;

#define INIT(x) memset(x,0,sizeof(x))
#define eps 1e-8
#define next next_
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int maxn = 200005;
const int N = 105;

inline void read(int &x) {
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}

struct node{
	int l,r;
	bool operator < (const node &b) const {
		if(l==b.l) return r < b.r;
		return l < b.l;
	}
}a[maxn];
int n,k,m,l[maxn],r[maxn],sum,cnt=1;

void merge() {
	cnt = 1;
	l[cnt] = a[0].l, r[cnt] = a[0].r;
	for(int i=1;i<n;i++) {
		if(a[i].l - r[cnt] > 1) {
			l[++cnt] = a[i].l;
			r[cnt] = a[i].r;
		}
		else r[cnt] = max(r[cnt],a[i].r);
	}	
}

int main() {
	ios::sync_with_stdio(false);
	while(cin >> n >> m) {
		for(int i=0;i<n;i++) cin >> a[i].l >> a[i].r;
		sort(a,a+n);
		merge();
		int i = 1, j = 2, gap = 0;
		int ans = r[1] - l[1] + 1 + m;
		while(j<=cnt) {
			gap += l[j] - r[j-1] - 1;
			while(gap>m) {
				i++;
				gap -= l[i] - r[i-1] - 1;
			} 
			ans = max(ans,r[j]-l[i]+1+m-gap);
			j++;
		}
		cout<<ans<<endl;
	}	
	return 0;
}

```
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

总想玩世不恭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值