hihoCoder 1095 HIHO Drinking Game 微软苏州校招笔试 12月27日
2015-03-26 20:39
399 查看
由game规则可以看出,T越大超出d的可能性越大,对小ho越有利,其实我是通过打表才看出来这个单调性的==。
对T进行二分搜索,[0,K+1],因为如果N=1,那么应该有T=K+1,小ho才可以获胜。
因为N是奇数,两者得分相等的情况不会存在,二分的时候可以忽略这种case。
注意二分的mid值如果越过了右边界,需要返回一个很大的值才能使其向左搜索。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<ctype.h>
#include<map>
#include<time.h>
#include<bitset>
#include<set>
#include<list>
using namespace std;
//hihocder 1095
const int maxn=1e5+10;
int N;
int K;
int d[maxn];
int ans;
void test()//打表找规律
{
int n=5;
int k=6;
int r=0;
int dd[]={3,6,6,2,1};
int s1=0;
int s2=0;
for(int t=1;t<=k;t++)
{
s1=0;
s2=0;
r=0;
for(int i=0;i<n;i++)
{
r=r+t;
if(r<=dd[i])
{
s1++;
r=0;
}
else
{
s2++;
r-=dd[i];
}
}
cout<<t<<" "<<s1<<" "<<s2<<endl;
}
}
pair<int,int> cal(int t)
{
if(t>(K+1))
{
return make_pair(0x3f3f3f3f,0x3f3f3f3f);//溢出右边界时,应该向左搜索,有点像罚函数
}
int remain=0;
int hi=0;
int ho=0;
for(int i=0;i<N;i++)
{
remain+=t;
if(remain<=d[i])
{
hi++;
remain=0;
}
else
{
ho++;
remain-=d[i];
}
}
return make_pair(hi,ho);
}
void binary()
{
int l=0;
int r=K+1;//对于N=1的case,T=K+1,所以搜索上界是K+1
int mid=0;
while(l<=r)
{
mid=(l+r)/2;
pair<int,int>tmp=cal(mid);
pair<int,int>tmp2=cal(mid+1);
//cout<<mid<<endl;
//cout<<tmp.first<<" "<<tmp.second<<" "<<tmp2.first<<" "<<tmp2.second<<endl;
if(tmp.first>tmp.second&&tmp2.first<tmp2.second)//== doesn't exist
{
ans=mid+1;
break;
}
else if(tmp.first<tmp.second&&tmp2.first<tmp2.second)
{
r=mid;
}
else if(tmp.first>tmp.second&&tmp2.first>tmp2.second)
{
l=mid;
}
}
}
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//test();
scanf("%d %d",&N,&K);
for(int i=0;i<N;i++)
{
scanf("%d",&d[i]);
}
binary();
printf("%d\n",ans);
return 0;
}
对T进行二分搜索,[0,K+1],因为如果N=1,那么应该有T=K+1,小ho才可以获胜。
因为N是奇数,两者得分相等的情况不会存在,二分的时候可以忽略这种case。
注意二分的mid值如果越过了右边界,需要返回一个很大的值才能使其向左搜索。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<ctype.h>
#include<map>
#include<time.h>
#include<bitset>
#include<set>
#include<list>
using namespace std;
//hihocder 1095
const int maxn=1e5+10;
int N;
int K;
int d[maxn];
int ans;
void test()//打表找规律
{
int n=5;
int k=6;
int r=0;
int dd[]={3,6,6,2,1};
int s1=0;
int s2=0;
for(int t=1;t<=k;t++)
{
s1=0;
s2=0;
r=0;
for(int i=0;i<n;i++)
{
r=r+t;
if(r<=dd[i])
{
s1++;
r=0;
}
else
{
s2++;
r-=dd[i];
}
}
cout<<t<<" "<<s1<<" "<<s2<<endl;
}
}
pair<int,int> cal(int t)
{
if(t>(K+1))
{
return make_pair(0x3f3f3f3f,0x3f3f3f3f);//溢出右边界时,应该向左搜索,有点像罚函数
}
int remain=0;
int hi=0;
int ho=0;
for(int i=0;i<N;i++)
{
remain+=t;
if(remain<=d[i])
{
hi++;
remain=0;
}
else
{
ho++;
remain-=d[i];
}
}
return make_pair(hi,ho);
}
void binary()
{
int l=0;
int r=K+1;//对于N=1的case,T=K+1,所以搜索上界是K+1
int mid=0;
while(l<=r)
{
mid=(l+r)/2;
pair<int,int>tmp=cal(mid);
pair<int,int>tmp2=cal(mid+1);
//cout<<mid<<endl;
//cout<<tmp.first<<" "<<tmp.second<<" "<<tmp2.first<<" "<<tmp2.second<<endl;
if(tmp.first>tmp.second&&tmp2.first<tmp2.second)//== doesn't exist
{
ans=mid+1;
break;
}
else if(tmp.first<tmp.second&&tmp2.first<tmp2.second)
{
r=mid;
}
else if(tmp.first>tmp.second&&tmp2.first>tmp2.second)
{
l=mid;
}
}
}
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//test();
scanf("%d %d",&N,&K);
for(int i=0;i<N;i++)
{
scanf("%d",&d[i]);
}
binary();
printf("%d\n",ans);
return 0;
}
相关文章推荐
- Hihocoder #1095 : HIHO Drinking Game (微软苏州校招笔试)( *【二分搜索最优解】)
- hihoCoder 1096 Divided Product 微软苏州校招笔试 12月27日
- hihoCoder 1106 Koch Snowflake 微软苏州校招笔试 1月17日
- hihoCoder 1107 Shortest Proper Prefix 微软苏州校招笔试 1月17日
- hihoCoder #1094 : Lost in the City(枚举,微软苏州校招笔试 12月27日 )
- 微软苏州校招笔试 12月27日 By Java
- hihoCoder 1103 Colorful Lecture Note 微软苏州校招笔试 1月10日
- 微软苏州校招笔试 12月27日
- hihocoder #1094 : Lost in the City微软苏州校招笔试 12月27日 (建图不大【暴力枚举】 子图的4种形态 1Y )
- 【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题
- 微软苏州校招笔试(2016.12):#1091 : Clicker
- 【hihocoder】Font Size(微软2016.4校招笔试题)
- 水水微软苏州校招笔试 1月17日 题解..
- [微软苏州校招 Hihocoder] Disk Storage
- 【hihocoder】1237 : Farthest Point ->微软2016校招在线笔试题
- 微软苏州校招笔试题目(1月10日)Colorful Lecture Note的解法
- Hihocoder #1095 : HIHO Drinking Game (微软笔试题)
- hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)
- 微软2016校招笔试 第二场部分题目个人思路
- hihocoder 1289 2017年微软实习笔试题