lines
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1441 Accepted Submission(s): 596
Problem Description
John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A.
Input
The first line contains a single integer
T(1≤T≤100)
(the data for
N>100
less than 11 cases),indicating the number of test cases.
Each test case begins with an integer N(1≤N≤105) ,indicating the number of lines.
Next N lines contains two integers Xi and Yi(1≤Xi≤Yi≤109) ,describing a line.
Each test case begins with an integer N(1≤N≤105) ,indicating the number of lines.
Next N lines contains two integers Xi and Yi(1≤Xi≤Yi≤109) ,describing a line.
Output
For each case, output an integer means how many lines cover A.
Sample Input
2 5 1 2 2 2 2 4 3 4 5 1000 5 1 1 2 2 3 3 4 4 5 5
Sample Output
3 1
Source
Recommend
n个区间,【x.y】哪个点被覆盖最多。
#include <bits/stdc++.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#include <vector>
typedef pair<int,int>p;
vector<p>v;
const int N=1e5+100;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int x,y;
v.clear();
for(int i=0; i<n; i++)
{
scanf("%d%d",&x,&y);
v.push_back(make_pair(x,1));
v.push_back(make_pair(y+1,-1));
}
sort(v.begin(),v.end());
int max1=0;
int ans=0;
for(int i=0; i<v.size(); i++)
{
ans+=v[i].second;
max1=max(max1,ans);
}
printf("%d\n",max1);
}
return 0;
}
北京区域赛A题。和上面这题很像。求前缀和的前缀和。
#include <iostream>
#include <cstring>
#include <cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#include <vector>
const int N=1000005;
ll hash1[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int r;
ll x,y;
ll l,w;
scanf("%d",&r);
int n;
memset(hash1,0,sizeof(hash1));
scanf("%d",&n);
ll sum=0;
for(int i=0; i<n; i++)
{
scanf("%lld%lld%lld%lld",&x,&y,&l,&w);
//cout<<l*w<<endl;
hash1[x+1]+=w;
hash1[x+l+1]-=w;
sum+=l*w;
// cout<<sum<<endl;
}
ll tot=0;
ll res=0;
ll min1=999999999999999;
int op=0;
for(int i=0; i<=r; i++)
{
tot+=hash1[i];
res+=tot;
if(2*res-sum>=0)
{
// cout<<res<<endl;
if(2*res-sum<=min1)
{
min1=2*res-sum;
op=i;
}
}
}
cout<<op<<endl;
}
return 0;
}