【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)
題干:
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered?1?through infinity from left to right.
Obviously, there is not enough sand on the beach, so you brought?n?packs of sand with you. Let height?hi?of the sand pillar on some spot?i?be the number of sand packs you spent on it.?You can't split a sand pack to multiple pillars, all the sand from it should go to a single one.?There is a fence of height equal to the height of pillar with?H?sand packs to the left of the first spot and you should prevent sand from going over it.
Finally you ended up with the following conditions to building the castle:
- h1?≤?H: no sand from the leftmost spot should go over the fence;
- For any??|hi?-?hi?+?1|?≤?1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen;
- : you want to spend all the sand you brought with you.
As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.
Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.
Input
The only line contains two integer numbers?n?and?H?(1?≤?n,?H?≤?1018) — the number of sand packs you have and the height of the fence, respectively.
Output
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
Examples
Input
5 2Output
3Input
6 8Output
3Note
Here are the heights of some valid castles:
- n?=?5,?H?=?2,?[2,?2,?1,?0,?...],?[2,?1,?1,?1,?0,?...],?[1,?0,?1,?2,?1,?0,?...]
- n?=?6,?H?=?8,?[3,?2,?1,?0,?...],?[2,?2,?1,?1,?0,?...],?[0,?1,?0,?1,?2,?1,?1,?0...]?(this one has?5?spots occupied)
The first list for both cases is the optimal answer,?3?spots are occupied in them.
And here are some invalid ones:
- n?=?5,?H?=?2,?[3,?2,?0,?...],?[2,?3,?0,?...],?[1,?0,?2,?2,?...]
- n?=?6,?H?=?8,?[2,?2,?2,?0,?...],?[6,?0,?...],?[1,?4,?1,?0...],?[2,?2,?1,?0,?...]
題目大意:
? ? ?對給定的n,H,把n劃分為a1,a2,a3,...a1,a2,a3,...,要求首項a1≤Ha1≤H,相鄰兩項之差不大于1,而且最后一項必須是1。總個數要最少,輸出這個最小的總個數。
解題報告:
附一個比較好的題解鏈接
? ?總的來說,就是先把能填的填上,然后剩下的按照正態分布填(這樣可以保證最大)二分驗證這個可以可行即可。注意會爆longlong所以給出兩種處理方式。
AC代碼1:
#include<bits/stdc++.h> #define ll long long using namespace std;ll n,h; bool ok(ll x) {ll ans=0;if(x>=2e9) return 1;if(x<=h) ans=x*(x+1)/2;else {ans=h*(h+1)/2+(x-h)*h; ll high=x-h-1;ll t1=high/2,t2=high-t1;ans+=t1*(t1+1)/2+t2*(t2+1)/2;}if(ans>=n)return 1;else return 0; } int main() {cin>>n>>h;ll l=1,r=1e18+1;ll mid=(l+r)/2;while(l<r) {mid = (l+r)/2;if(ok(mid)) r=mid;else l=mid+1;}printf("%lld\n",l);return 0; }AC代碼2:
#include<bits/stdc++.h> #define ll long long using namespace std;ll n,h; bool ok(ll x) {ll ans=0; // if(x>=2e9) return 1;if(x<=h) ans=x*(x+1)/2;else {ans=h*(h+1)/2+(x-h)*h; ll high=x-h-1;ll t1=high/2,t2=high-t1;ans+=t1*(t1+1)/2+t2*(t2+1)/2;}if(ans>=n) return 1;else return 0; } int main() {cin>>n>>h;ll l=1,r=2*sqrt(n)+1;ll mid=(l+r)/2;while(l<r) {mid = (l+r)/2;if(ok(mid)) r=mid;else l=mid+1;}printf("%lld\n",l);return 0; }總結:難啊!!難嗎??其實還是做題經驗太少了。。。。
總結
以上是生活随笔為你收集整理的【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: searchnavversion.exe
- 下一篇: ACM竞赛、数论内容常用的定理(求解(a