Interviewe

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8797    Accepted Submission(s): 2077


 

Problem Description

YaoYao has a company and he wants to employ m people recently. Since his company is so famous, there are n people coming for the interview. However, YaoYao is so busy that he has no time to interview them by himself. So he decides to select exact m interviewers for this task.

YaoYao decides to make the interview as follows. First he queues the interviewees according to their coming order. Then he cuts the queue into m segments. The length of each segment is

 , which means he ignores the rest interviewees (poor guys because they comes late). Then, each segment is assigned to an interviewer and the interviewer chooses the best one from them as the employee.

YaoYao’s idea seems to be wonderful, but he meets another problem. He values the ability of the ith arrived interviewee as a number from 0 to 1000. Of course, the better one is, the higher ability value one has. He wants his employees good enough, so the sum of the ability values of his employees must exceed his target k (exceed means strictly large than). On the other hand, he wants to employ as less people as possible because of the high salary nowadays. Could you help him to find the smallest m?

 

 

Input

The input consists of multiple cases.
In the first line of each case, there are two numbers n and k, indicating the number of the original people and the sum of the ability values of employees YaoYao wants to hire (n≤200000, k≤1000000000). In the second line, there are n numbers v1, v2, …, vn (each number is between 0 and 1000), indicating the ability value of each arrived interviewee respectively.
The input ends up with two negative numbers, which should not be processed as a case.

 

 

Output

For each test case, print only one number indicating the smallest m you can find. If you can’t find any, output -1 instead.

 

 

Sample Input

11 300 7 100 7 101 100 100 9 100 100 110 110 -1 -1

 

 

Sample Output

3

Hint

We need 3 interviewers to help YaoYao. The first one interviews people from 1 to 3, the second interviews people from 4 to 6, and the third interviews people from 7 to 9. And the people left will be ignored. And the total value you can get is 100+101+100=301>300.

 

 

Source

​2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU​

 

 

Recommend

zhengfeng   |   We have carefully selected several similar problems for you:  ​​2586​​​ ​​2874​​​ ​​2888​​​ ​​3478​​​ ​​3487​​ 

 

题意:给定一个数字串,求最小的分段数m(每段个数按数字输入顺序,余下的不要)
求各个段中的最大值之和超过k(wa了好几发)的最小m是多少

分析:一看即使二分加线段树,线段树走遍天下,结果这题时间被卡了,得加一些优化,才能过,先求出m的上界和下界,二分枚举m

看来还得看看rmq

今天想了想,好像f(mid)的这个函数不满足单调性

所以枚举就行,注意一下上下界

枚举代码

 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define N 200010
#define ll long long
using namespace std;

int a[N];
int n,m;

struct node //线段树
{
int l,r;
ll dat;
}tree[N*4];
void PushUp(int x) //线段树维护结点信息
{
tree[x].dat=max(tree[x<<1].dat,tree[x<<1|1].dat);
}
void build(int x,int l,int r) //线段树建树
{
tree[x].l=l;
tree[x].r=r;
if (l==r) //叶子节点
{
tree[x].dat=a[l];
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
PushUp(x);
}

ll query(int x,int l,int r) //线段树区间查询
{
if (l<=tree[x].l&&r>=tree[x].r)
return tree[x].dat; //找到

int mid=(tree[x].l+tree[x].r)>>1;

ll ans=0;
if (l<=mid) ans=max(ans,query(x<<1,l,r));
if (r>mid) ans=max(ans,query(x<<1|1,l,r));
return ans;
}

void update(int x,int y,ll k) //线段树单点修改
{
if (tree[x].l==tree[x].r)
{
tree[x].dat += k;
return ;
}
int mid=(tree[x].l+tree[x].r)>>1;
if (y<=mid) update(x<<1,y,k);
else update(x<<1|1,y,k);
PushUp(x);
}
ll f(int mid)
{
ll sum=0;
int pre=1;
int cnt=n/mid;
for(int i=1;i<=mid;i++)
{
sum+=query(1,pre,i*cnt);
pre=i*cnt+1;
}
return sum;
}
int main()
{
ll s;
while(scanf("%d%lld",&n,&s)!=-1)
{
if(n==-1&&s==-1)break;
int maxx=-1e9,minn=1e9;
ll sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
maxx=max(maxx,a[i]);
minn=min(minn,a[i]);
}

if(maxx>=s) {
printf("1\n");
continue;
}
if(sum<s){
printf("-1\n");
continue;
}
minn=minn?minn:1;
maxx=maxx?maxx:1;
build(1,1,n);
int l=max(int(s/maxx),1),r=min(int(s/minn+1),n)+1;
//cout<<query(1,1,n);
for(int i=l;i<=r;i++)
{
if(f(i)>s)
{
printf("%d\n",i);
break;
}
}


}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define N 200010
#define ll long long
using namespace std;

int a[N];
int n,m;

struct node //线段树
{
int l,r;
ll dat;
}tree[N*4];
void PushUp(int x) //线段树维护结点信息
{
tree[x].dat=max(tree[x<<1].dat,tree[x<<1|1].dat);
}
void build(int x,int l,int r) //线段树建树
{
tree[x].l=l;
tree[x].r=r;
if (l==r) //叶子节点
{
tree[x].dat=a[l];
return;
}
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
PushUp(x);
}

ll query(int x,int l,int r) //线段树区间查询
{
if (l<=tree[x].l&&r>=tree[x].r)
return tree[x].dat; //找到

int mid=(tree[x].l+tree[x].r)>>1;

ll ans=0;
if (l<=mid) ans=max(ans,query(x<<1,l,r));
if (r>mid) ans=max(ans,query(x<<1|1,l,r));
return ans;
}

void update(int x,int y,ll k) //线段树单点修改
{
if (tree[x].l==tree[x].r)
{
tree[x].dat += k;
return ;
}
int mid=(tree[x].l+tree[x].r)>>1;
if (y<=mid) update(x<<1,y,k);
else update(x<<1|1,y,k);
PushUp(x);
}
ll f(int mid)
{
ll sum=0;
int pre=1;
int cnt=n/mid;
for(int i=1;i<=mid;i++)
{
sum+=query(1,pre,i*cnt);
pre=i*cnt+1;
}
return sum;
}
int main()
{
ll s;
while(scanf("%d%lld",&n,&s)!=-1)
{
if(n==-1&&s==-1)break;
int maxx=-1e9,minn=1e9;
ll sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
maxx=max(maxx,a[i]);
minn=min(minn,a[i]);
}

if(maxx>=s) {
printf("1\n");
continue;
}
if(sum<s){
printf("-1\n");
continue;
}
minn=minn?minn:1;
maxx=maxx?maxx:1;
build(1,1,n);
int l=max(int(s/maxx),1),r=min(int(s/minn+1),n)+1;
//cout<<query(1,1,n);
while(l<r)
{
//cout<<l<<" "<<r<<endl;
int mid=(l+r)>>1;
//cout<<mid<<" "<<f(mid)<<endl;
if(f(mid)>s)
r=mid;
else
l=mid+1;
}
//cout<<l<<endl;
printf("%d\n",l);

}
return 0;
}

 

 

一份神奇的线段树代码,我超时它没超

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN=200005;
int N, K;
ll a[maxN], pre, tree[maxN<<2];
void buildTree(int rt, int l, int r)
{
if(l == r)
{
tree[rt] = a[l];
return;
}
int mid = (l + r)>>1;
buildTree(rt<<1, l, mid);
buildTree(rt<<1|1, mid+1, r);
tree[rt] = max(tree[rt<<1], tree[rt<<1|1]);
}
ll query(int rt, int l, int r, int ql, int qr)
{
if(ql<=l && qr>=r) return tree[rt];
if(l == r) return tree[rt];
int mid = (l + r)>>1;
if(ql>mid) return query(rt<<1|1, mid+1, r, ql, qr);
else if(qr<=mid) return query(rt<<1, l, mid, ql, qr);
else
{
ll ans = query(rt<<1|1, mid+1, r, mid+1, qr);
ans = max(ans, query(rt<<1, l, mid, ql, mid));
return ans;
}
}
int main()
{
while(scanf("%d%d", &N, &K)!=EOF)
{
pre = 0;
if(N<0 && K<0) break;
for(int i=1; i<=N; i++)
{
scanf("%lld", &a[i]);
pre += a[i];
}
if(pre <= K) { printf("-1\n"); continue; }
buildTree(1, 1, N);
int L=1, R=N, mid=0, ans=N;
while(L <= R)
{
mid = (L + R)>>1;
ll tmp = 0;
int up = N/mid;
for(int i=1; i<=mid; i++)
{
tmp += query(1, 1, N, (i-1)*up+1, i*up);
}
if(tmp > K) { R=mid-1; ans=mid; }
else L = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}