返回顶部

实现技巧之分段

POJ3273

按每个月的权值进行分段

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<vector>
 7 #include<set>
 8 #include<queue> 
 9 #define pii pair<int,int> 
10 #define fi first
11 #define se second
12 #define mp make_pair
13 #define pb push_back
14 #define rep(i, a, b) for(int i=(a); i<(b); i++)
15 #define sz(a) (int)a.size()
16 #define de(a) cout << #a << " = " << a << endl
17 #define dd(a) cout << #a << " = " << a << " "
18 #define all(a) a.begin(), a.end()
19 #define endl "\n"
20 #define INF 0x3f3f3f3f
21 #include <iostream>
22 using namespace std;
23 #define M 200000+
24 const int maxn=1e5+7;
25 #define eps 1e-5
26 typedef long long ll;
27 ll n,m,sum;
28 double a[maxn];
29 bool check(ll mid)
30 {
31     int ret=0,cnt=1;
32     rep(i,0,n){
33         ret+=a[i];
34         if(ret>mid){
35             cnt++;
36             ret=a[i];
37         }
38     }
39     return cnt<=m;
40 }
41 int main()
42 {
43     cin>>n>>m;
44     ll max=0;
45     rep(i,0,n){
46         cin>>a[i],sum+=a[i];if(a[i]>max)max=a[i];
47     }
48     ll l=max,r=sum,ans=0,mid;
49     while(r>=l)
50     {
51         mid=(r+l)/2;
52         if(check(mid))r=mid-1;
53         else l=mid+1;
54     }
55     cout<<mid<<endl;
56     return 0;
57  } 
View Code

最长递增子段

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<cstring>
 6 #include<map>
 7 #include<set>
 8 #include<cmath>
 9 #include<queue>
10 #include<bitset>
11 #include<utility>
12 #include<functional>
13 #include<iomanip>
14 #include<sstream>
15 #include<ctime>
16 #include<cassert>
17 #define A first
18 #define B second
19 #define mp make_pair
20 #define pb push_back
21 #define pw(x) (1ll << (x))
22 #define sz(x) ((int)(x).size())
23 #define all(x) (x).begin(),(x).end()
24 #define rep(i,l,r) for(int i=(l);i<(r);i++)
25 #define per(i,r,l) for(int i=(r);i>=(l);i--)
26 #define FOR(i,l,r) for(int i=(l);i<=(r);i++)
27 #define eps 1e-9
28 #define PIE acos(-1)
29 #define cl(a,b) memset(a,b,sizeof(a))
30 #define fastio ios::sync_with_stdio(false);cin.tie(0);
31 #define lson l , mid , ls
32 #define rson mid + 1 , r , rs
33 #define ls (rt<<1)
34 #define rs (ls|1)
35 #define INF 0x3f3f3f3f
36 #define lowbit(x) (x&(-x))
37 #define sqr(a) a*a
38 #define ll long long
39 #define ull unsigned long long
40 #define vi vector<int>
41 #define pii pair<int, int>
42 #define dd(x) cout << #x << " = " << (x) << ", "
43 #define de(x) cout << #x << " = " << (x) << "\n"
44 #define endl "\n"
45 using namespace std;
46 //**********************************
47 const int maxn=135+300;
48 int a[maxn],front[maxn],rear[maxn];
49 int n;
50 //**********************************
51 
52 //**********************************
53 int main()
54 {
55     cin>>n;
56     int cnt=1;
57     FOR(i,1,n){
58         scanf("%d",&a[i]);
59         if(a[i]>a[i-1]&&i!=1)cnt++;
60         else cnt=1;
61         rear[i]=cnt;
62         int tmp=cnt;
63         do{
64             front[i-tmp+1]=tmp;
65         }while(tmp--);
66     }
67 //    FOR(i,1,n){
68 //        dd(i);dd(front[i]);de(rear[i]);
69 //    }
70 //    per(i,n-1,1){
71 //        front[i-rear[i]]=front[i];
72 //    }
73     int ans=1;
74     FOR(i,1,n){
75 //        ans=max(ans,front[i]);
76 //        dd(i);dd(front[i]);de(rear[i]);
77         if(a[i+1]>=a[i-1]+2)ans=max(ans,front[i+1]+rear[i-1]+1);
78         else {
79             ans=max(ans,1+rear[i-1]);
80             ans=max(ans,1+front[i+1]);
81         }
82     }
83     cout<<ans<<endl;
84     return 0;
85 }
View Code

 

posted @ 2018-09-14 09:20  牛奶加咖啡~  阅读(129)  评论(0编辑  收藏  举报