# bzoj 2151 种树

n个数组成的环中取m个互不相邻的数使这些数和最大

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cmath>
5 #include<algorithm>
6 #include<cstring>
7 #include<vector>
8 #include<stack>
9 #include<queue>
10 #include<map>
11 #define rep(i,s,t) for(register int i=(s),i__end=(t);i<=i__end;++i)
12 #define dwn(i,s,t) for(register int i=(s),i__end=(t);i>=i__end;--i)
13 #define ren for(int i=fst[x];i;i=nxt[i])
14 #define Fill(x,t) memset(x,t,sizeof(x))
15 #define ll long long
16 #define ull unsigned long long
17 #define inf 1LL<<60
18 #define MAXN 200100
19 #define MOD 998244353
20 using namespace std;
22 {
23     int x=0,f=1;char ch=getchar();
24     while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
25     while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
26     return x*f;
27 }
28 int n,m,g[MAXN],ans,r[MAXN],l[MAXN],vis[MAXN];
29 struct data {int id,val;};
30 bool operator < (const data a,const data b) {return a.val<b.val;}
31 bool operator == (const data a,const data b) {return a.val==b.val&&a.id==b.id;}
32 priority_queue <data> q;
33 void work()
34 {
35     while(vis[q.top().id]) q.pop();data x=q.top();ans+=x.val;q.pop();
36     g[x.id]=g[l[x.id]]+g[r[x.id]]-g[x.id],vis[l[x.id]]=vis[r[x.id]]=1;
37     q.push((data){x.id,g[x.id]});r[l[l[x.id]]]=l[r[r[x.id]]]=x.id;
38     l[x.id]=l[l[x.id]],r[x.id]=r[r[x.id]];
39 }
40 int main()
41 {
43     rep(i,1,n) l[i]=i-1,r[i]=i+1;l[1]=n,r[n]=1;rep(i,1,m) work();printf("%d\n",ans);
44 }
View Code

bzoj 1150 数据备份

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cmath>
5 #include<algorithm>
6 #include<cstring>
7 #include<vector>
8 #include<stack>
9 #include<queue>
10 #include<map>
11 #define rep(i,s,t) for(register int i=(s),i__end=(t);i<=i__end;++i)
12 #define dwn(i,s,t) for(register int i=(s),i__end=(t);i>=i__end;--i)
13 #define ren for(int i=fst[x];i;i=nxt[i])
14 #define Fill(x,t) memset(x,t,sizeof(x))
15 #define ll long long
16 #define ull unsigned long long
17 #define inf 1000000007
18 #define MAXN 1001000
19 #define MOD 998244353
20 using namespace std;
22 {
23     int x=0,f=1;char ch=getchar();
24     while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
25     while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
26     return x*f;
27 }
28 int n,m,g[MAXN],r[MAXN],l[MAXN],vis[MAXN];
29 ll ans;
30 struct data {int id;ll val;};
31 bool operator < (const data a,const data b) {return a.val>b.val;}
32 priority_queue <data> q;
33 void work()
34 {
35     while(vis[q.top().id]) q.pop();data x=q.top();ans+=x.val;q.pop();
36     g[x.id]=g[l[x.id]]+g[r[x.id]]-g[x.id],vis[l[x.id]]=vis[r[x.id]]=1;
37     q.push((data){x.id,g[x.id]});r[l[l[x.id]]]=l[r[r[x.id]]]=x.id;
38     l[x.id]=l[l[x.id]],r[x.id]=r[r[x.id]];
39 }
40 int main()
41 {
46 }