云智第一节学习记录
第一节-前缀和与差分,二分答案,双指针,整体二分
- 第一节题单
A 题:P4552 [Poetize6] IncDec Sequence
花了
既然标签里都写了差分那肯定是用差分的嘛,问题是思路想了半天发现不需要模拟计算过程……
如果序列的每个数都相等,那么它们的差分数组
事实上差分的区间加法/减法就能实现,差分的区间加减就是令
当时还想着直接模拟,没想到直接统计正数和与负数和就行了,因为差分数组第
那么答案
难么答案
代码如下:
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e5+5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int a[N],c[N];
long long b[2];
signed main() {
//fast();
int n,p;
in(n);
rep(i,1,n)in(a[i]),c[i]=a[i]-a[i-1];
rep(i,2,n){
b[c[i]>0]+=c[i];
}
b[0]=-b[0];
if(b[0]<b[1])swap(b[0],b[1]);
cout<<b[0]<<'\n'<<b[0]-b[1]+1;
return 0;
}
B 题:U360489 灵茶八题 - 子数组 ^w+
考验二进制思想。
不难想到直接的
暴力代码如下:
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e5+5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n,a[N],c[23],last;
signed main() {
//fast();
in(n);
rep(i,1,n)in(a[i]);
long long ans=0;
rep(i,1,n) {
int sum=a[i],h=a[i];
ans+=a[i];
rep(j,i+1,n) {
sum^=a[j];
ans+=sum;
h+=sum;
}
last=h;
}
cout<<ans;
return 0;
}
在暴力代码的基础上进行优化。
不难发现,这是一个前缀异或和,
而
在第一遍前缀和计算出
AC 代码如下
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e5+5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n,a[N],c[23],last;
signed main() {
//fast();
in(n);
rep(i,1,n)in(a[i]);
int ans=0;
int sum=0;
rep(i,1,n) {
sum^=a[i];
rep(j,0,21) {
c[j]+=bool(sum&(1<<j));
}
ans+=sum;
}
sum=ans;
rep(i,2,n) {
sum-=a[i-1];
rep(j,0,21) {
if(a[i-1]&(1<<j)) {
c[j]--;
sum-=((c[j]-(n-i+1-c[j])))*(1<<j);
c[j]=(n-i+1-c[j]);
}
}
ans+=sum;
}
cout<<ans;
return 0;
}
C 题:P9681 幽默的世界。
这道题暴力代码复杂的很高,而且直接枚举的指数达到了惊人的四次方!因此不能暴力。
事实上,一个幽默的序列
用一个数组
可以先用一个前缀和实现区间求和,以判断
计算完所有的答案后再用一个前缀和记录答案。
对于每次查询,用二分查找找到大于等于
代码实现
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 2e5+5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n,q,tot,a[N],er[N],ans[N],d[N];
long long f[N];
vector<int>v[N];
int low(int x){
int l=1,r=tot,Ans=1;
while(l<=r){
int mid=(l+r)>>1;
if(er[mid]<=x)Ans=mid,l=mid+1;
else r=mid-1;
}
return Ans;
}
signed main() {
//fast();
in(n),in(q);
rep(i,1,n) {
in(a[i]),f[i]=f[i-1]+a[i];
if(a[i]>0)er[++tot]=i;
}
rep(i,1,tot) {
rep(j,er[i-1]+1,er[i]) {
if(f[er[i]]-f[j-1]>0) {
v[er[i]].push_back(j);
ans[er[i]]++;
}
}
}
rep(i,1,tot)d[i]=d[i-1]+ans[er[i]];
while(q--) {
int l,r,h=0;
in(l),in(r);
int ls=lower_bound(er+1,er+tot+1,l)-er,rs=low(r);
auto anl=lower_bound(v[er[ls]].begin(),v[er[ls]].end(),l)-v[er[ls]].begin();
if(l<=er[ls]&&er[ls]<=r)
cout<<d[rs]-d[ls-1]-anl<<'\n';
else puts("0");
}
return 0;
}
D 题:[ABC203D] Pond
这道题怎么说呢,这题我第一眼想到的是:主席树求二维区间第
还是考虑二分吧。
不难想到答案是单调的,但难点在于如何在
代码如下:
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e3+5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n,k,a[N][N],b[N][N];
int p;
bool check(int x) {
rep(i,1,n){
rep(j,1,n){
b[i][j]=b[i-1][j]+b[i][j-1]-b[i-1][j-1]+bool(a[i][j]<=x);
if(i>=k&&j>=k){
if(b[i][j]-b[i-k][j]-b[i][j-k]+b[i-k][j-k]>p)return true;
}
}
}
return false;
}
signed main() {
//fast();
in(n),in(k);
int l=0,r=INT_MIN;
p=((k*k)>>1)-(!((k*k)&1));
rep(i,1,n) {
rep(j,1,n) {
in(a[i][j]);
if(a[i][j]>r)r=a[i][j];
}
}
int ans=r;
while(l<=r) {
int mid=(l+r)>>1;
if(check(mid))ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans;
return 0;
}
E 题:[ABC203E] White Pawn
如果直接模拟肯定会炸,因为
思路与其他题解差不多,枚举每行的黑子,统计该行同列的黑子和左上或左下的黑子后再统一进行转移。如果在统计过程中就进行转移会影响后续的统计,因此需全部统计完后再进行状态转移,也就是完成该行的删添。
代码如下
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 2e5+5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n,m;
int a[N],b[N],c[N],top1,top2;
vector<int>sum[N];
set<int>s;//还是被迫用了红黑树
signed main() {
//fast();
in(n),in(m);
rep(i,1,m)in(a[i]),in(b[i]);
rep(i,1,m)c[i]=a[i];
sort(c+1,c+m+1);
int tot=unique(c+1,c+m+1)-c;//离散化
tot--;
rep(i,1,m)sum[lower_bound(c+1,c+tot+1,a[i])-c].push_back(b[i]);//处理数据
s.insert(n);//初始化
rep(i,1,tot) {//枚举每行的黑子
top1=top2=0;//清空统计数组
for(auto v:sum[i]) {//记录同列的黑子和左上或左下的黑子
if(s.find(v)!=s.end())a[++top1]=v;//不浪费一点空间!
if(s.find(v+1)!=s.end()||s.find(v-1)!=s.end())b[++top2]=v;
}
rep(j,1,top1)s.erase(a[j]);//删添
rep(j,1,top2)s.insert(b[j]);
}
cout<<s.size();
return 0;
}
中间的题跳过了……
H 题:[ABC153F] Silver Fox vs Monster
不难想到一个贪心思路,对每只怪兽按
TLE……
一个简单的 hack:
8 4 1
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
每一次只能消灭一只怪兽,因此时间复杂度为
优化思路
双指针优化。很明显,炸弹对怪兽的伤害是一个区间修改,这里就已经可以用树状数组直接实现,但其实用差分就足够了。那如何确定右边界?二分答案可以实现,但其实可以用双指针。这里的区间类似一个滑动窗口,因此可以用双指针确定右边界,然后通过差分实现区间修改,将时间复杂度优化到线性的
总复杂度为
代码实现
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 2e5+5;
const int inf = INT_MAX;
inline long long read() {
long long x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n,d,a;
long long c[N];
pair<long long,long long>x[N];
signed main() {
//fast();
in(n),in(d),in(a);
rep(i,1,n)in(x[i].first),in(x[i].second);
sort(x+1,x+n+1);
long long j=1,t=0,ans=0;
rep(i,1,n){
t+=c[i];
while(j<=n&&x[j].first-x[i].first<=2*d+1)j++;
if(x[i].second-t>0){
int p=(x[i].second-t*a)/a+((x[i].second-t*a)%a);
t+=p*a;
c[j]-=p*a;
ans+=p;
}
}
cout<<ans;
return 0;
}
I 题:[ABC246D] 2-variable Function
这道题有着明显的单调性,可以直接用双指针解决,
代码如下
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(long long i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e1;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
long long solve(long long i,long long j){
return i*i*i+i*i*j+i*j*j+j*j*j;
}
signed main() {
//fast();
long long n;
cin>>n;
long long ans=0x3f3f3f3f3f3f3f3f;
long long j=1e6;
rep(i,0,1e6){
while(solve(i,j)>=n&&j>=0)j--;
j++;
if(solve(i,j)>=n)
ans=min(ans,solve(i,j));
}
cout<<ans;
return 0;
}
中间的题又跳了……
K 题:P3834 【模板】可持久化线段树 2
这东东老师要求用整体二分,但整体二分那树状数组维护的那段我每听懂……所以主席树秒了……
代码如下
还是喜欢链式风格的主席树算法啊。
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 2e5 + 5;
const int inf = INT_MAX;
inline int read() {
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') {
x=x*10+ch-48;
ch=getchar();
}
return x*f;
}
void fast() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
struct Tree{
int l,r,v;
};
vector<Tree>t;
int root[N];
int n,m,a[N],b[N];
int change(int idx,int l,int r,int q,int val){
Tree tmp=t[idx];
if(l==r){
tmp.v+=val;
t.push_back(tmp);
return t.size()-1;
}
int mid=(l+r)>>1;
if(q<=mid){
tmp.l=change(tmp.l,l,mid,q,val);
}else{
tmp.r=change(tmp.r,mid+1,r,q,val);
}
tmp.v=t[tmp.l].v+t[tmp.r].v;
t.push_back(tmp);
return t.size()-1;
}
int query(int idxl,int idxr,int l,int r,int q){
if(l==r){
return l;
}
int mid=(l+r)>>1,tmp=t[t[idxr].l].v-t[t[idxl].l].v;
if(q<=tmp)return query(t[idxl].l,t[idxr].l,l,mid,q);
else return query(t[idxl].r,t[idxr].r,mid+1,r,q-tmp);
}
signed main() {
//fast();
in(n),in(m);
rep(i,1,n)a[i]=b[i]=read();
sort(b+1,b+n+1);
int tot=unique(b+1,b+n+1)-b-1;
Tree tmp;
tmp.l=tmp.r=tmp.v=0;
t.push_back(tmp);
rep(i,1,n){
a[i]=lower_bound(b+1,b+tot+1,a[i])-b;
root[i]=change(root[i-1],1,tot,a[i],1);
}
rep(i,1,m){
int l,r,k;
in(l),in(r),in(k);
printf("%d\n",b[query(root[l-1],root[r],1,tot,k)]);
}
return 0;
}