标签:txt print freopen == open temp names include getch
输入一串数字,给你M个询问,每次询问就给你两个数字X,Y要求你说出X到Y这段区间内的最大数。
#include<bits/stdc++.h> using namespace std; #define Max(x,y) (x)>(y)?(x):(y) #define Min(x,y) (x)>(y)?(y):(x) #define ll long long #define rg register const int N=1e5+5,M=10000+5,inf=0x3f3f3f3f,P=99999997; int n,m,a[N],lg[N],f[N][30]; template <class t>void rd(t &x){ x=0;int w=0;char ch=0; while(!isdigit(ch)) w|=ch==‘-‘,ch=getchar(); while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); x=w?-x:x; } int main(){ // freopen("in2.txt","r",stdin); rd(n),rd(m); for(int i=1;i<=n;++i) rd(a[i]); lg[0]=-1; for(int i=1;i<=n;++i) f[i][0]=a[i],lg[i]=lg[i>>1]+1; for(int j=1;j<=20;++j) for(int i=1;i+(1<<j)-1<=n;++i) f[i][j]=Max(f[i][j-1],f[i+(1<<(j-1))][j-1]); int x,y; while(m--){ rd(x),rd(y); int s=lg[y-x+1]; printf("%d\n",Max(f[x][s],f[y-(1<<s)+1][s])); } return 0; }
n个数中每连续k个数中最大和最小值
#include<bits/stdc++.h> using namespace std; #define Max(x,y) (x)>(y)?(x):(y) #define Min(x,y) (x)>(y)?(y):(x) #define ll long long #define rg register const int N=1e5+5,M=10000+5,inf=0x3f3f3f3f,P=99999997; int n,k,a[N],lg[N],mx[N][30],mn[N][30]; template <class t>void rd(t &x){ x=0;int w=0;char ch=0; while(!isdigit(ch)) w|=ch==‘-‘,ch=getchar(); while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); x=w?-x:x; } int main(){ // freopen("in2.txt","r",stdin); rd(n),rd(k); for(int i=1;i<=n;++i) rd(a[i]); lg[0]=-1; for(int i=1;i<=n;++i) mn[i][0]=mx[i][0]=a[i],lg[i]=lg[i>>1]+1; for(int j=1;j<=20;++j) for(int i=1;i+(1<<j)-1<=n;++i) mx[i][j]=Max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]), mn[i][j]=Min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]); int x,y,s; for(int i=1;i<=n-k+1;++i){ x=i,y=i+k-1,s=lg[y-x+1]; printf("%d %d\n",Max(mx[x][s],mx[y-(1<<s)+1][s]),Min(mn[x][s],mn[y-(1<<s)+1][s])); } return 0; }
标签:txt print freopen == open temp names include getch
原文地址:https://www.cnblogs.com/lxyyyy/p/11215456.html