寒假第三周训练——二分题目汇总

1088 - Points in Segments
Time Limit: 2 second(s)Memory Limit: 32 MB

Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segment A B if A ≤ pi ≤ B.

For example if the points are 1, 4, 6, 8, 10. And the segment is 0 to 5. Then there are 2 points that lie in the segment.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers denoting the points in ascending order. All the integers are distinct and each of them range in [0, 108].

Each of the next q lines contains two integers Ak Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting a segment.

Output

For each case, print the case number in a single line. Then for each segment, print the number of points that lie in that segment.

Sample Input

Output for Sample Input

1

5 3

1 4 6 8 10

0 5

6 10

7 100000

Case 1:

2

3

2

Source

LightOJ1088

题意:n个有序数,找出[p,q]之间有多少个数。
#include <cstdio>
#include <iostream>
using namespace std;
int n,q,a,b,l,r,cnt=0;
int s[100005];

int binary_low(int x)
{
	int l=1,r=n,mid=0;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if (s[mid]<x) l=mid+1;
		else r=mid-1;
	}
	return l;
}

int binary_high(int x)
{
	int l=1,r=n,mid=0;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if (s[mid]<=x) l=mid+1;
		else r=mid-1;
	}
	return l;
}

int main()
{
	int t; cin >> t;
	while(t--)
	{
		printf("Case %d:\n",++cnt);
		cin >> n >> q;
		for (int i=1;i<=n;i++)
			scanf("%d",&s[i]);
		for (int i=1;i<=q;i++)
		{
			scanf("%d%d",&a,&b);
			l=binary_low(a);
			r=binary_high(b);
			printf("%d\n",r-l);
		}
	}
	return 0;
}
Drying
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions:20212 Accepted: 5113

Description

It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.

Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.

There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.

Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).

The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).

Output

Output a single integer — the minimal possible number of minutes required to dry all clothes.

Sample Input

sample input #1
3
2 3 9
5

sample input #2
3
2 3 6
5

Sample Output

sample output #1
3

sample output #2
2

Source

题意:每件衣服有ai滴水,每秒蒸发1滴,然后烘干机1秒烘干k滴(就算不蒸发了),问最小多少时间能烘干所以衣服。就二分需要烘干的时间,然后判断能否烘干。

#include <iostream>
#include <cstdio>
typedef long long ll;
using namespace std;
ll a[100005],k,n;
/*
int judge(ll x)
{
	ll t=0;
	for(int i=0;i<n;i++) 
		if (a[i]>x)
		{
			if ((a[i]-x)%k==0) t+=(a[i]-x)/k;
			else t+=(a[i]-x)/k+1;
		}
	if (t>x)
		return 1;
	else return 0;
}
*/  错误做法还没找到错在哪

int judge(ll x)  
{  
    ll i,t=0;  
    if(k==1)
        return 1;  
    for(i=0;i<n;++i)  
    {  
        if(a[i]>x)  
            t+=(a[i]-x+k-2)/(k-1);
    }  
    if(t>x)  
        return 1;  
    return 0;  
}

int main()
{
	while(cin >> n)
	{
		ll max=0;
		for (ll i=0;i<n;i++)
		{
			scanf("%lld",&a[i]);
			if (max<a[i])
				max=a[i];
		}
		scanf("%lld",&k);
		ll l=0,r=max,mid;
		while(l<r)
		{
			mid=(l+r)/2;
			if (judge(mid))
				l=mid+1;
			else r=mid;
		}
		printf("%d\n",r);
	}
	return 0;
}
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions:31673 Accepted: 11954

Description

Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.

FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.

FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.

Input

Line 1: Two space-separated integers:  N and  M 
Lines 2.. N+1: Line  i+1 contains the number of dollars Farmer John spends on the  ith day

Output

Line 1: The smallest possible monthly limit Farmer John can afford to live with.

Sample Input

7 5
100
400
300
100
500
101
400

Sample Output

500

Hint

If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.

Source

题意:有n个数分成m组,求使这m组和的最大值最小的情况。
#include <iostream>
#include <cstdio>
using namespace std;
int n,m,a[100005],sum=0;

int judge(int x)
{
	int cnt=1,t=x;
	for (int i=1;i<=n;i++)
	{
		if (x<a[i]) return 0;
		if (t<a[i]) {t=x; cnt++;}
		t-=a[i];
	}
	if (cnt<=m) return 1;
	else return 0;
}

int main()
{
	cin >> n >> m;
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		sum+=a[i];
	}
	int l=0,r=sum,mid;
	while(l<r)
	{
		mid=(l+r)/2;
		if (judge(mid)) r=mid;
		else l=mid+1;
	}
	printf("%d\n",r);
	return 0;
}

转载于:https://www.cnblogs.com/Radium1209/p/10415390.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值