学习笔记 考试题 T1,T2
T1题意描述
给出 m 个数 a[1],a[2],…,a[m] 求 1~n 中有多少数不是 a[1],a[2],…,a[m]的倍数。
数据说明
对于 60%的数据,1<=n<=
对于另外 20%的数据,m=2
对于 100%的数据,1<=n<=
Input
10 2
2 3
Output
3
题意分析
原理: 多步容斥
我们先dfs把内区间全部的ai倍数减去
又因为会存在重复 所以利用容斥公式
进行多步容斥
如果当前我们筛了奇数个数 就减去 否则就加上
CODE:
#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>
#define int long long
#define inf 0x7fffffff
#define N 500008
#define IL inline
#define M 1008611
#define D double
#define ull unsigned long long
#define R register
using namespace std;
template<typename T>void read(T &a)
{
T x=0,f=1;char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')f=0;ch=getchar();
}
while(isdigit(ch))
{
x=(x<<1)+(x<<3)+ch-'0';ch=getchar();
}
a=f?x:-x;
}
/*-------------OI使我快乐-------------*/
int num[35];
int n,m,ans;
IL int gcd(int x,int y)
{
if(x<y) swap(x,y);
int r=x%y;
while(r)
{
x=y;y=r;r=x%y;
}
return y;
}
IL void dfs(int dep,int cnt,int lcm)
{
if(dep==m+1)
{//当前已经筛完了
if(cnt&1)
{//容斥公式
if(cnt) ans+=(n/lcm);
}
else
{
if(cnt) ans-=(n/lcm);
}
return;
}
dfs(dep+1,cnt,lcm);
//我们不选这个数
int tmp=lcm*num[dep]/gcd(lcm,num[dep]);
//我们选的话就要更新
dfs(dep+1,cnt+1,tmp);
}
signed main()
{
freopen("count.in","r",stdin);
freopen("count.out","w",stdout);
read(n);read(m);
for(R int i=1;i<=m;++i) read(num[i]);
dfs(1,0,1);//从1开始搜索
//倍数从最小公倍数开始
printf("%d\n",n-ans);
fclose(stdin);
fclose(stdout);
return 0;
}
T2题意描述
一个区间的价值定义为该区间中的最大值减最小值 给定 n 个数,求所有区间价值中,第 k 大值为多少。
Input
3 2
2 1 3
Output
2
【样例解释】
[l,r]表示第 l 个数到第 r 个数组成的区间的价值
[1,1]=0 [1,2]=1 [1,3]=2
[2,2]=0 [2,3]=2
[3,3]=0
题意分析
一开始的做法是O(n)枚举区间长度
再O(n)单调队列枚举区间最小值最大值 进行更新
现在直接上正解
我们由于是答案单调性 所以进行二分答案
le=0 ri=aimax
关键在于如何进行检验
我们看图就可以知道
对于当前的k与i区间价值 其左边的(k-1)与i区间价值
一定是>=
因为TA不仅有k有的 还有k没有的 至少不会更差
所以 如果当前[k,i]价值满足>=m 则前(k-1)一定也满足
那么我们直接单调队列扫描就可以了
每一次固定右端点 在左区间内进行查找
注意 由于我们要检验[k,i]区间内的合法性
当这样是不合法的 lx,ly分别对应最大值单调队列以及最小值单调队列左指针
所以我们在k右移是 也要把lx以及ly对应右移
每一次找到这个阈值k之后 都加上
最后与k进行比较即可
CODE:
#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>
#define int long long
#define inf 0x7fffffff
#define N 500008
#define IL inline
#define M 1008611
#define D double
#define ull unsigned long long
#define R register
using namespace std;
template<typename T>void read(T &a)
{
T x=0,f=1;char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')f=0;ch=getchar();
}
while(isdigit(ch))
{
x=(x<<1)+(x<<3)+ch-'0';ch=getchar();
}
a=f?x:-x;
}
/*-------------OI使我快乐-------------*/
int n,m,le,ri,ans;
int num[N];
int Max[N],Min[N];
bool check(int now)
{
int lx=1,rx=1,ly=1,ry=1;
int pos=1,poi,poj,rex=(now==1 ? 1:0);
Max[lx]=Min[ly]=1;
//初始化单调队列
for(R int i=2;i<=n;++i)
{
// printf("youngsc\n");
while(lx<=rx && num[Max[rx]]<num[i]) rx--;
Max[++rx]=i;
while(ly<=ry && num[Min[ry]]>num[i]) ry--;
Min[++ry]=i;
//队列单调的维护
while(pos<i)
{
poi=lx;poj=ly;++pos;
//每一次寻找阈值
while(Max[poi]<pos) ++poi;
while(Min[poj]<pos) ++poj;
//指针不合法的话就右移
if(num[Max[poi]]-num[Min[poj]]>=now) {lx=poi;ly=poj;}
else {--pos;break;}
//这里进行一个优化
//无法确定阈值的确切位置
//所以转变为找第一个不合法的位置
//上一个就是阈值
}
if(num[Max[lx]]-num[Min[ly]]>=now) rex+=pos;
//只有是找到了合法阈值 才可以相加
}
// cout<<rex<<endl;
return rex>=m;
}
signed main()
{
freopen("kth.in","r",stdin);
freopen("kth.out","w",stdout);
read(n);read(m);
for(R int i=1;i<=n;++i)
{
read(num[i]);ri=max(ri,num[i]);
}
while(le<=ri)
{//二分答案的板子
int mid=(le+ri)>>1;
if(check(mid)) ans=mid,le=mid+1;
else ri=mid-1;
}
printf("%lld\n",ans);
// fclose(stdin);
// fclose(stdout);
return 0;
}