标签:sort etc sizeof utc sans ace else one mem
打个A和D签到>_<,赛后补了F...
先把所有的山峰找出来,然后算一下前缀和$sum[i]$。如果k可以的话肯定满足第i*k段的和$sum[i]-sum[i-k]>=1$,这个k可以暴力枚举>_<,不要枚举1就行>_<
效率$O(∑i|1<=i<=n)$
效率不是很对?注意到,只有$i|n$的才能选,所以最多只有$\sqrt{n}$个选择>_<
1 #include<map>
2 #include<set>
3 #include<list>
4 #include<cmath>
5 #include<queue>
6 #include<cstdio>
7 #include<vector>
8 #include<string>
9 #include<cstring>
10 #include<iostream>
11 #include<algorithm>
12 using namespace std;
13 #define mp make_pair
14 #define ll long long
15 #define reg register
16 #define inf 0x7fffffff
17 #define MOD 1000000007
18 #define clr(a) memset(a,sizeof(a),0);
19 #define rep(i,l,r) for(reg int i=l;i<=r;i++)
20 #define dow(i,l,r) for(reg int i=l;i>=r;i--)
21 inline int read(){int x=0,f=1;char c=getchar();while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}return x*f;}
22 inline ll readl(){ll x=0,f=1;char c=getchar();while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}return x*f;}
23 template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+‘0‘);}
24 template<class T>inline void writeln(T x){if(x<0)putchar(‘-‘);x=abs(x);print(x);putchar(‘\n‘);}
25 template<class T>inline void write(T x){if(x<0)putchar(‘-‘);x=abs(x);print(x);}
26 template<class T>inline T power(T a,T b){T ans=1;while(b){if(b&1){ans=ans*a%MOD;}b>>=1;a=a*a%MOD;}return ans%MOD;}
27 /*================Header Template==============*/
28 int n,a[50005],sum[50005];
29 int main(){
30 n=read(); a[0]=inf; a[n+1]=inf;
31 rep(i,1,n) a[i]=read();
32 rep(i,1,n){
33 if(a[i]>a[i-1]&&(a[i]>a[i+1])) sum[i]=sum[i-1]+1;
34 else sum[i]=sum[i-1];
35 }
36 // rep(i,1,n) cout<<sum[i]<<" ";
37 rep(k,1,n){
38 if(n%k)continue;
39 int flag=1;
40 for(reg int i=k;i<=n;i+=k)
41 if((sum[i]-sum[i-k])); else flag=0;
42 if(flag) return writeln(n/k),0;
43 }
44 writeln(0);
45 }
Trisolaris dalao好强啊>_<,我好弱啊...他给了我以下提示
“这道题,对每次区间修改打个标记,标记$r[i]=y,l[i]=x|x<=i<=y$,就避免了重复的合并>_<”
效率$O(nlogn)$
1 #include<map>
2 #include<set>
3 #include<list>
4 #include<cmath>
5 #include<queue>
6 #include<cstdio>
7 #include<vector>
8 #include<string>
9 #include<cstring>
10 #include<iostream>
11 #include<algorithm>
12 using namespace std;
13 #define mp make_pair
14 #define ll long long
15 #define reg register
16 #define inf 0x7fffffff
17 #define MOD 1000000007
18 #define clr(a) memset(a,sizeof(a),0);
19 #define rep(i,l,r) for(reg int i=l;i<=r;i++)
20 #define dow(i,l,r) for(reg int i=l;i>=r;i--)
21 inline int read(){int x=0,f=1;char c=getchar();while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}return x*f;}
22 inline ll readl(){ll x=0,f=1;char c=getchar();while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}return x*f;}
23 template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+‘0‘);}
24 template<class T>inline void writeln(T x){if(x<0)putchar(‘-‘);x=abs(x);print(x);putchar(‘\n‘);}
25 template<class T>inline void write(T x){if(x<0)putchar(‘-‘);x=abs(x);print(x);}
26 template<class T>inline T power(T a,T b){T ans=1;while(b){if(b&1){ans=ans*a%MOD;}b>>=1;a=a*a%MOD;}return ans%MOD;}
27 /*================Header Template==============*/
28 int num[200005],f[200005];
29 int find( int x ){ return (x == f[x]) ? x : f[x] = find( f[x] ); }
30 void uni(int x,int y){
31 int fx=find(x) , fy=find(y);
32 if(num[fx]>num[fy]){ num[fx]+=num[fy]; f[fy]=fx; }
33 else { num[fy]+=num[fx]; f[fx]=fy; }
34 }
35 int n,m,l[200005],r[200005];
36 int main(){
37 n=read();m=read();
38 rep(i,1,n) f[i]=i , num[i]=1 , l[i]=i , r[i]=i;
39 rep(i,1,m){
40 int st=read();
41 if(st==1){
42 int x=read() , y=read();
43 uni(x,y);
44 }else
45 if(st==2){
46 int x=read() , y=read();
47 rep(j,r[x],l[y]) uni(x,j);
48 rep(j,r[x],l[y]) r[j]=y,l[j]=x;
49 }else{
50 int x=read() , y=read();
51 puts((find(x)==find(y))?"YES":"NO");
52 }
53 }
54 }
这题我怎么可能自己写出来QWQ
看了一下网上的题解...哦
原来,对于每次交换,$a[i]+i$是固定的,因为$i-1$的同时$a[i]+1$了>_<,所以对于每个点,$a[i]+i$肯定是不会变的
所以,定义$b[i]=a[i]+i$,排序之后去重,如果a[i]+i相同的话,那么交换了和没交换是一样的,比如样例2...
顺便还学会了$unique$:是个函数、用法:$t=unique(a+1,a+n+1)-a-1;$
效率:$O(nlogn)$
1 #include<map>
2 #include<set>
3 #include<list>
4 #include<cmath>
5 #include<queue>
6 #include<cstdio>
7 #include<vector>
8 #include<string>
9 #include<cstring>
10 #include<iostream>
11 #include<algorithm>
12 using namespace std;
13 #define mp make_pair
14 #define ll long long
15 #define reg register
16 #define inf 0x7fffffff
17 #define MOD 1000000007
18 #define clr(a) memset(a,sizeof(a),0);
19 #define rep(i,l,r) for(reg int i=l;i<=r;i++)
20 #define dow(i,l,r) for(reg int i=l;i>=r;i--)
21 inline int read(){int x=0,f=1;char c=getchar();while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}return x*f;}
22 inline ll readl(){ll x=0,f=1;char c=getchar();while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}return x*f;}
23 template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+‘0‘);}
24 template<class T>inline void writeln(T x){if(x<0)putchar(‘-‘);x=abs(x);print(x);putchar(‘\n‘);}
25 template<class T>inline void write(T x){if(x<0)putchar(‘-‘);x=abs(x);print(x);}
26 template<class T>inline T power(T a,T b){T ans=1;while(b){if(b&1){ans=ans*a%MOD;}b>>=1;a=a*a%MOD;}return ans%MOD;}
27 /*================Header Template==============*/
28 int main(){
29 int n=read(),a[200005]={0};
30 rep(i,1,n) a[i]=read() , a[i]+=i;
31 sort(a+1,a+n+1);
32 int t=unique(a+1,a+n+1)-a-1;
33 if(t==n) return puts("Happy"),0; else return puts("Sad"),0;
34
35 }
标签:sort etc sizeof utc sans ace else one mem
原文地址:https://www.cnblogs.com/ZincSabian/p/9245759.html