CodeForces - 1250J The Parade half

topic

Meaning of the questions:

A total of n height, every soldier has a height. You need to put them into a k-line (soldiers do not need all the arrangements), each row of soldiers height difference is less than equal to 1. You're looking out the maximum number of soldiers can be arranged

 

answer:

This question can easily see that a-half-half line how many soldiers (assuming that half out of the value of x)

Because the instructions on the topic gap height of each row of soldiers can not be greater than 1, so only enter these n adjacent height can only be arranged in a row

 

TLE Code:

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 const int maxn=30005;
 8 const int INF=0x3f3f3f3f;
 9 typedef long long ll;
10 ll v[maxn],n,k,w[maxn];
11 bool panduan(ll x)
12 {
13     for(ll i=1;i<=n;++I)
 14          W [I] = V [I];
 15      LL Start = . 1 , CI = K;
 16      the while (CI)
 . 17      {
 18 is          IF (W [Start]> = X)
 . 19          {
 20 is              W [Start] - = X;   // since this is a primary I Save x, it times out 
21 is              ci-- ;
 22 is          }
 23 is          the else 
24          {
 25              IF (W [Start] + W [Start + . 1 ]> = X + && Start . 1 <= n-)
 26              {
 27                 w[start+1]=(w[start]+w[start+1])-x;
28                 start++;
29                 ci--;
30             }
31             else
32             {
33                 start++;
34             }
35         }
36         if(start>n) break;
37     }
38     if(ci) return 0;
39     else return 1;
40 }
41 int main()
42 {
43     ll t;
44     scanf("%lld",&t);
45     while(t--)
46     {
47         ll sum=0;
48         scanf("%lld%lld",&n,&k);
49         for(ll i=1;i<=n;++i)
50         {
51             scanf("%lld",&v[i]);
52             sum+=V [I];
 53 is          }
 54 is          LL MID, ans = 0 , L = . 1 , R & lt SUM = / K;   // here must give ans initialized to 0 
55          the while (L <= R & lt)
 56 is          {
 57 is              MID = (L R & lt +) >> . 1 ;
 58              IF (panduan (MID))
 59              {
 60                  ANS = MID;
 61 is                  L = MID + . 1 ;
 62 is              }
 63 is              the else R & lt mid- = . 1 ;
 64          }
 65          the printf ("%lld\n",ans*k);
66     }
67     return 0;
68 }

 

 

Correct codes:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 const int maxn=30005;
 8 const int INF=0x3f3f3f3f;
 9 typedef long long ll;
10 ll v[maxn],n,k,w[maxn];
11 bool panduan(ll x)
12 {
13     for(ll i=1; i<=n; ++i)
14         w[i]=v[i];
15     ll start=1,ci=k;
16     while(ci>0)
17     {
18         if(w[start]>=x)
19         {
20             ci=ci-w[start]/x;
21             w[start]%=x;
22         }
23         else
24         {
25             if(start+1<=n && w[start]+w[start+1]>=x)
26             {
27                 ci=ci-(w[start]+w[start+1])/x;
28                 w[start+1]=(w[start]+w[start+1])%x;
29                 start++;
30             }
31             else
32             {
33                 start++;
34             }
35         }
36         if(start>n) break;
37     }
38     if(ci<=0) return 1;
39     else return 0;
40 }
41 int main()
42 {
43     ll t;
44     scanf("%lld",&t);
45     while(t--)
46     {
47         ll sum=0;
48         scanf("%lld%lld",&n,&k);
49         for(ll i=1;i<=n;++i)
50         {
51             scanf("%lld" , & V [I]);
 52 is              SUM = + V [I];
 53 is          }
 54 is          LL MID, ans = 0 , L = . 1 , R & lt SUM = / K;   // must give ans initialized to 0, I was wrong several .. 
55          the while (L <= R & lt)
 56 is          {
 57 is  
58              MID = (L + R & lt) / 2 ;
 59              IF (panduan (MID))
 60              {
 61 is                  ANS = MID;
 62 is                  L = MID + . 1 ;
 63 is              }
 64              the else R & lt mid- =1;
65         }
66         printf("%lld\n",ans*k);
67     }
68     return 0;
69 }

 

 

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11843828.html