SRM548 DIVⅡ-500PT(二分答案OR貪心)
二分答案倒是很快就想到了的,剛開始沒有想好怎么驗證check()。后面就跪在這里了。。。。tree.height>=1啊。。。
毛哥給的辦法是行的,貪心。ORZ,ORZ,我沒寫呀。。。。
爆零。。其實應該果斷提交的吧?
下次TC要雪恥才行!
毛哥給的辦法是行的,貪心。ORZ,ORZ,我沒寫呀。。。。
爆零。。其實應該果斷提交的吧?
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int h[55],n;
int max(int x,int y)
{
return x>y?x:y;
}
bool check(int x)
{
int last,now,i;
last=max(h[0]-x,1);
for (i=1;i<n;i++){
now=h[i];
if (now+x<=last)
return false;
last=max(last+1,now-x);
}
return true;
}
class KingdomAndTrees{
public:
int minLevel(vector <int> heights){
int l,r,mid,i;
n=heights.size();
for (i=0;i<n;i++)
h[i]=heights[i];
l=0;r=0;
for (i=0;i<n;i++)
if (h[i]>r)
r=h[i];
r=r+n;
while (l<r){
mid=(l+r)/2;
if (check(mid))
r=mid;
else
l=mid+1;
}
return r;
}
};
#include<string.h>
#include<vector>
using namespace std;
int h[55],n;
int max(int x,int y)
{
return x>y?x:y;
}
bool check(int x)
{
int last,now,i;
last=max(h[0]-x,1);
for (i=1;i<n;i++){
now=h[i];
if (now+x<=last)
return false;
last=max(last+1,now-x);
}
return true;
}
class KingdomAndTrees{
public:
int minLevel(vector <int> heights){
int l,r,mid,i;
n=heights.size();
for (i=0;i<n;i++)
h[i]=heights[i];
l=0;r=0;
for (i=0;i<n;i++)
if (h[i]>r)
r=h[i];
r=r+n;
while (l<r){
mid=(l+r)/2;
if (check(mid))
r=mid;
else
l=mid+1;
}
return r;
}
};
下次TC要雪恥才行!
posted on 2012-07-03 10:15 wangs 閱讀(200) 評論(0) 編輯 收藏 引用 所屬分類: Topcoder