2017.10.28 QB模拟赛 ——上午
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017.10.28 QB模拟赛 ——上午相关的知识,希望对你有一定的参考价值。
T1
1e18 内的立方数有 1e6个
直接枚举可过
二分最优
考场用set 死慢。。
#include <cstdio> int t; long long p; int main(int argc,char *argv[]) { freopen("cubic.in","r",stdin); freopen("cubic.out","w",stdout); scanf("%d",&t); while(t--) { scanf("%I64d",&p); long long l=1,r=1000000; for(long long mid;l<=r;) { mid=(l+r)>>1; if(mid*mid*mid>p) r=mid-1; else if(mid*mid*mid<p) l=mid+1; else {puts("YES");goto flag;} } puts("NO"); flag:; } fclose(stdin); fclose(stdout); return 0; }
T2
p=a^3-b^3
由立方差公式 a^3-b^3=(a-b)*(a^2+ab+b^2)
因为p是素数 所以 a-b=1;
二分即可
#include <cstdio> int t; long long p; int main(int argc,char *argv[]) { freopen("cubicp.in","r",stdin); freopen("cubicp.out","w",stdout); scanf("%d",&t); while(t--) { scanf("%I64d",&p); long long l=1,r=1000000; for(long long mid;l<=r;) { mid=(l+r)>>1; if(mid*mid+mid*(mid+1)+(mid+1)*(mid+1)>p) r=mid-1; else if(mid*mid+mid*(mid+1)+(mid+1)*(mid+1)<p) l=mid+1; else {puts("YES");goto flag;} } puts("NO"); flag:; } fclose(stdin); fclose(stdout); return 0; }
T3
二分答案
线段树维护区间
#include <algorithm> #include <cctype> #include <cstdio> #define N 1000005 using namespace std; struct node { int l,r,x,t; bool operator<(node a)const { return x>a.x; } }a[N],b[N]; inline void read(int &x) { register char ch=getchar(); for(x=0;!isdigit(ch);ch=getchar()); for(;isdigit(ch);x=x*10+ch-‘0‘,ch=getchar()); } int n,t,flag[N<<2|1],val[N<<2|1]; void build(int k,int l,int r) { flag[k]=0; if(l==r) {val[k]=0;return;} int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); val[k]=min(val[k<<1],val[k<<1|1]); } void down(int k) { val[k<<1]=flag[k]; val[k<<1|1]=flag[k]; flag[k<<1]=flag[k]; flag[k<<1|1]=flag[k]; flag[k]=0; } int query(int k,int l,int r,int x,int y) { if(l>=x&&r<=y) return val[k]; int mid=(l+r)>>1,ret=0x7fffffff; if(flag[k]) down(k); if(x<=mid) ret=min(ret,query(k<<1,l,mid,x,y)); if(y>mid) ret=min(ret,query(k<<1|1,mid+1,r,x,y)); val[k]=min(val[k<<1],val[k<<1|1]); return ret; } void modify(int k,int l,int r,int x,int y,int v) { if(l>=x&&r<=y) {flag[k]=val[k]=v;return;} int mid=(l+r)>>1; if(flag[k]) down(k); if(x<=mid) modify(k<<1,l,mid,x,y,v); if(y>mid) modify(k<<1|1,mid+1,r,x,y,v); val[k]=min(val[k<<1],val[k<<1|1]); } bool check(int k) { build(1,1,n); for(int i=1;i<=k;++i) b[i]=a[i]; sort(b+1,b+1+k); int lmax,lmin,rmax,rmin; for(int i=1;i<=k;) { int j,x=b[i].x; lmax=b[i].l,rmax=b[i].r,lmin=b[i].l,rmin=b[i].r; for(j=i+1;b[j].x==x&&j<=k;++j) { lmax=max(lmax,b[j].l); lmin=min(lmin,b[j].l); rmin=min(rmin,b[j].r); rmax=max(rmax,b[j].r); if(lmax>rmin) return true; } int re=query(1,1,n,lmin,rmax); if(re) return true; modify(1,1,n,lmin,rmax,b[i].x); i=j; } return false; } int main(int argc,char *argv[]) { freopen("number.in","r",stdin); freopen("number.out","w",stdout); read(n); read(t); for(int i=1;i<=t;++i) { read(a[i].l); read(a[i].r); read(a[i].x); a[i].t=i; } int ans=t+1,l=1,r=t; for(int mid;l<=r;) { mid=(l+r)>>1; if(check(mid)) ans=mid,r=mid-1; else l=mid+1; } printf("%d",ans); fclose(stdin); fclose(stdout); return 0; }
#include <cstdio> #include <iostream> #include <algorithm> #define N 1000011 #define min(x, y) ((x) < (y) ? (x) : (y)) #define max(x, y) ((x) > (y) ? (x) : (y)) using namespace std; int n, q, ans; int f[N]; struct node { int x, y, z; }p[N], t[N]; inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘; return x * f; } inline bool cmp(node x, node y) { return x.z > y.z; } inline int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } inline bool check(int k) { int i, j, x, y, lmin, lmax, rmin, rmax; for(i = 1; i <= n + 1; i++) f[i] = i; for(i = 1; i <= k; i++) t[i] = p[i]; std::sort(t + 1, t + k + 1, cmp); lmin = lmax = t[1].x; rmin = rmax = t[1].y; for(i = 2; i <= k; i++) { if(t[i].z < t[i - 1].z) { if(find(lmax) > rmin) return 1; for(j = find(lmin); j <= rmax; j++) f[find(j)] = find(rmax + 1); lmin = lmax = t[i].x; rmin = rmax = t[i].y; } else { lmin = min(lmin, t[i].x); lmax = max(lmax, t[i].x); rmin = min(rmin, t[i].y); rmax = max(rmax, t[i].y); if(lmax > rmin) return 1; } } // cout<<find(1)<<endl; if(find(lmax) > rmin) return 1; return 0; } int main() { freopen("number.in","r",stdin); freopen("number.out","w",stdout); int i, x, y, mid; n = read(); q = read(); for(i = 1; i <= q; i++) p[i].x = read(), p[i].y = read(), p[i].z = read(); x = 1, y = q; //cout<<check(2)<<endl; //return 0; ans = q + 1; while(x <= y) { mid = (x + y) >> 1; if(check(mid)) ans = mid, y = mid - 1; else x = mid + 1; } printf("%d\n", ans); return 0; }
以上是关于2017.10.28 QB模拟赛 ——上午的主要内容,如果未能解决你的问题,请参考以下文章
2017-10-28 noip模拟赛by WISCO 信息组