实现技巧之分段

POJ3273

按每个月的权值进行分段

 1 #include
 2 #include
 3 #include
 4 #include
 5 #include
 6 #include
 7 #include<set>
 8 #include 
 9 #define pii pair 
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 
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<endl;
56     return 0;
57  } 
View Code

最长递增子段

 1 #include
 2 #include
 3 #include
 4 #include
 5 #include
 6 #include
 7 #include<set>
 8 #include
 9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
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
41 #define pii pair
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<endl;
84     return 0;
85 }
View Code

 

转载于:https://www.cnblogs.com/klaycf/p/9644602.html

你可能感兴趣的:(实现技巧之分段)