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
#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;
}
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;
}
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
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5 100 400 300 100 500 101 400
Sample Output
500
Hint
Source
#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;
}