Problem Description
There are n men ,every man has an ID(1…n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make some group. K men in a group can create K*K value. The value of an interval is sum of these value of groups. The people of same group’s id must be continuous. Now we chose an interval of men and want to know there should be how many groups so the value of interval is max.
Input
First line is T indicate the case number.
For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.
Then a line have n number indicate the ID of men from left to right.
Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].
Output
For every query output a number indicate there should be how many group so that the sum of value is max.
Sample Input
1 5 2 3 1 2 5 4 1 5 2 4
Sample Output
1 2
题意就是找区间内,连续的块,
例如 区间内是1 2 5,就是三块,
区间内是1 2 3,就是一块.
可以用离线化处理的莫队来做。在奇偶优化时候,出现了一点bug,回溯的时候删掉没有记录为-1而是清零了,改了一下午,实属sd。
莫队最详细介绍
未奇偶优化
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define mid ((l+r)>>1)
#define clr(x,v) memset(x,v,sizeof(x))
typedef long long ll;
const int N=1e5+7;
const int inf=0x3f3f3f3f;
const double eps=1e-6;
int t,n,m,unit,temp;
int a[N],ans[N],book[N];
struct node {
int l,r,id;
friend bool operator <(node a,node b) {
if(a.l/unit==b.l/unit) {
/* if((a.l/unit)&1) {
return a.rb.r;
}*/
return a.r<b.r;
}
else {
return a.l/unit<b.l/unit;
}
}
} query[N];
void add(int x) {
if(book[x-1]==0&&book[x+1]==0) {
temp++;
}
if(book[x-1]==1&&book[x+1]==1) {
temp--;
}
book[x]=1;
}
void del(int x) {
if(book[x-1]==0&&book[x+1]==0) {
temp--;
}
if(book[x-1]==1&&book[x+1]==1) {
temp++;
}
book[x]=0;
}
int main() {
//ios::sync_with_stdio(0);
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
unit=(int)sqrt((double)n);
for(int i=1; i<=m; i++) {
scanf("%d %d",&query[i].l,&query[i].r);
query[i].id=i;
}
sort(query+1,query+m+1);
int tl=1,tr=0;
temp=0;
clr(book,0);
for(int i=1; i<=m; i++) {
while(tr<query[i].r)
add(a[++tr]);
while(tr>query[i].r)
del(a[tr--]);
while(tl<query[i].l)
del(a[tl++]);
while(tl>query[i].l)
add(a[--tl]);
ans[query[i].id]=temp;
}
for(int i=1; i<=m; i++) {
printf("%d\n",ans[i]);
}
}
return 0;
}
奇偶优化
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define mid ((l+r)>>1)
#define clr(x,v) memset(x,v,sizeof(x))
typedef long long ll;
const int N=1e5+7;
const int inf=0x3f3f3f3f;
const double eps=1e-6;
int t,n,m,unit,temp;
int a[N],ans[N],book[N],belong[N];
struct node {
int l,r,id;
friend bool operator <(node a,node b) {
if(a.l/unit==b.l/unit) {
if((a.l/unit)&1) {
return a.r<b.r;
} else {
return a.r>b.r;
}
} else {
return a.l/unit<b.l/unit;
}
}
} query[N];
void add(int x) {
if(book[x-1]==0&&book[x+1]==0) {
temp++;
}
if(book[x-1]==1&&book[x+1]==1) {
temp--;
}
book[x]++;
}
void del(int x) {
if(book[x-1]==0&&book[x+1]==0) {
temp--;
}
if(book[x-1]==1&&book[x+1]==1) {
temp++;
}
book[x]--;
}
int main() {
//ios::sync_with_stdio(0);
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
unit=(int)sqrt((double)n);
for(int i=1; i<=m; i++) {
scanf("%d %d",&query[i].l,&query[i].r);
query[i].id=i;
}
sort(query+1,query+m+1);
int tl=1,tr=0;
temp=0;
clr(book,0);
for(int i=1; i<=m; i++) {
while(tr<query[i].r) {
add(a[++tr]);
// printf("add:%d\n",tr);
}
while(tr>query[i].r) {
del(a[tr--]);
// printf("del:%d\n",tr+1);
}
while(tl<query[i].l) {
del(a[tl++]);
// printf("del:%d\n",tl-1);
}
while(tl>query[i].l) {
add(a[--tl]);
// printf("add:%d\n",tl);
}
ans[query[i].id]=temp;
}
for(int i=1; i<=m; i++) {
printf("%d\n",ans[i]);
}
}
return 0;
}
对拍
参考教程
定义
什么是对拍? 当我们的程序过了样例,是否意味着它一定能AC呢?显然大多数情况下都是不行的。所以我们需要自己设计一些数据来测试我们的程序,但有的题目数据很大,我们肉眼无法看出程序计算的结果是否正确,手工计算又非常耗时,在紧张的比赛中,我们该怎么应对呢?于是有了对拍。 对拍简单的说就是当你写完一个题目的程序以后,再写一个暴力求解该题目的程序,然后自己生成一些测试数据,看同样的数据,两个程序输出的结果是否相同,不同意味着被对拍的程序有问题。以此来帮助你修改程序,提高通过率的方法,我们称为对拍。
1.生成数据插入data。in
2.准备正解(或者暴力解法)和待验证解法
分别输出到两个out
3.新建txt,改为。bat
遇到不一样会停下来,打开你的in文件,就可以知道不一样的数据了~