L. Let‘s Play Curling[思维]——2020-2021 ACM-ICPC, Asia Nanjing Regional Contest
Posted 出尘呢
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了L. Let‘s Play Curling[思维]——2020-2021 ACM-ICPC, Asia Nanjing Regional Contest相关的知识,希望对你有一定的参考价值。
L. Let’s Play Curling
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Curling is a sport in which players slide stones on a sheet of ice toward a target area. The team with the nearest stone to the center of the target area wins the game.
Two teams, Red and Blue, are competing on the number axis. After the game there are (n+m) stones remaining on the axis, n of them for the Red team and the other m of them for the Blue. The i-th stone of the Red team is positioned at ai and the i-th stone of the Blue team is positioned at bi.
Let c be the position of the center of the target area. From the description above we know that if there exists some i such that 1≤i≤n and for all 1≤j≤m we have |c−ai|<|c−bj| then Red wins the game. What’s more, Red is declared to win p points if the number of i satisfying the constraint is exactly p.
Given the positions of the stones for team Red and Blue, your task is to determine the position c of the center of the target area so that Red wins the game and scores as much as possible. Note that c can be any real number, not necessarily an integer.
Input
There are multiple test cases. The first line of the input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers n and m (1≤n,m≤105) indicating the number of stones for Red and the number of stones for Blue.
The second line contains n integers a1,a2,⋯,an (1≤ai≤109) indicating the positions of the stones for Red.
The third line contains m integers b1,b2,⋯,bm (1≤bi≤109) indicating the positions of the stones for Blue.
It’s guaranteed that neither the sum of n nor the sum of m will exceed 5×105.
Output
For each test case output one line. If there exists some c so that Red wins and scores as much as possible, output one integer indicating the maximum possible score of Red (NOT c). Otherwise output “Impossible” (without quotes) instead.
Example
inputCopy
3
2 2
2 3
1 4
6 5
2 5 3 7 1 7
3 4 3 1 10
1 1
7
7
outputCopy
2
3
Impossible
Note
For the first sample test case we can assign c=2.5 so that the stones at position 2 and 3 for Red will score.
For the second sample test case we can assign c=7 so that the stones at position 5 and 7 for Red will score.
思路:
不要找C,以各蓝石头之间有几个红石头判断!
回顾几个错误——都是边界问题:
1.开始是lower_bound(b[i+1]-upper_bound(b[i])
——错误是,有的时候没有足够的数目,于是错了(其实特判一下,这样应该也可以
2.后来用指针数数的方法
——错误仍然是没有考虑边界,左边单独看,右边单独看,b[0]不要重叠了
附:
1.区域赛的T是10000左右…
2.关于scanf和cin的速度
scanf:171 ms
加速后的cin:421 ms
——以后多用scanf吧!更帅哦~~
//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include<bitset>
//#include<string>
//#include<sstream>
#include<vector>
#include<map>
//#include<set>
//#include<ctype.h>
//#include<stack>
//#include<queue>
#ifdef LOCAL
FILE*FP=freopen("text.in","r",stdin);
//FILE*fp=freopen("text.out","w",stdout);
#endif
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define pll pair<ll,ll>
#define plll pair<ll,pll>
#define pdd pair<double,double>
#define pdi pair<double,int>
#define pid pair<int,double>
#define vi vector <int>
#define vii vector <vi>
#define vl vector<ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))
#define _mem(a,b,c) memset(a,b,sizeof(a[0])*c)
#define _forplus(i,a,b) for( register int i=(a); i<=(b); i++)
#define forplus(i,a,b) for( register int i=(a); i<(b); i++)
#define _forsub(i,a,b) for( register int i=(a); i>=(b); i--)
#define _forauto(a,b) for(auto &(a):(b))
#define _forautome(a,b,c) for(auto (a) = (b); (a) != (c); (a)++)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
//参考:set<int>::iterator iter = vis.begin();
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define pi (acos(-1))
#define EPS 0.00000001
#define MOD 1000000007
#define fastio std::ios::sync_with_stdio(false);std::cin.tie(0);
//#define int ll
#define N 100005
int t,n,m,r[N],b[N];
int32_t main(){
fastio
cin>>t;//等下试试scanf
while(t--){
cin>>n>>m;//红蓝数
forplus(i,0,n){
cin>>r[i];
}
forplus(i,0,m){
cin>>b[i];
}
sort(r,r+n);
sort(b,b+m);//bug:看这上面写容易复制错误
int* pr=lower_bound(r,r+n,b[0]);//还有靠左值
int ma=(int)(pr-r);
int cnt;
forplus(i,0,m){//要从0开始!现在是lower_bound 的 b[0] ,可能重复,要略过,并且本身多一个鲁棒性更好
//看i和i-1中的
cnt=0;
while(*pr<b[i]&&pr<&r[n]){//到的是r[n-1]
pr++;
cnt++;
}
while(*pr==b[i]&&pr<&r[n])pr++;
ma=max(ma,cnt);
//cout<<"ma="<<ma<<'\\n';
}
//还有靠右值!!多想想
ma=max(ma,(int)(&r[n]-pr));
if(ma==0)cout<<"Impossible\\n";
else cout<<ma<<'\\n';
}
return 0;
}
数数的解法:
421 ms
O(m+n)
附对比:
二分的解法:
436 ms
O(m*log(n))
(但是好像emmm也差不到哪去欸)
//#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include<bitset>
//#include<string>
//#include<sstream>
#include<vector>
#include<map>
//#include<set>
//#include<ctype.h>
//#include<stack>
//#include<queue>
#ifdef LOCAL
FILE*FP=freopen("text.in","r",stdin);
//FILE*fp=freopen("text.out","w",stdout);
#endif
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define pll pair<ll,ll>
#define plll pair<ll,pll>
#define pdd pair<double,double>
#define pdi pair<double,int>
#define pid pair<int,double>
#define vi vector <int>
#define vii vector <vi>
#define vl vector<ll>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))
#define _mem(a,b,c) memset(a,b,sizeof(a[0])*c)
#define _forplus(i,a,b) for( register int i=(a); i<=(b); i++)
#define forplus(i,a,b) for( register int i=(a); i<(b); i++)
#define _forsub(i,a,b) for( register int i=(a); i>=(b); i--)
#define _forauto(a,b) for(auto &(a):(b))
#define _forautome(a,b,c) for(auto (a) = (b); (a) != (c); (a)++)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
//参考:set<int>::iterator iter = vis.begin();
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define pi (acos(-1))
#define EPS 0.00000001
#define MOD 1000000007
#define fastio std::ios::sync_with_stdio(false);std::cin.tie(0);
//#define int ll
#define N 100005
int t,n,m,r[N],b[N],cntr,cntb;
int32_t main(){
fastio
cin>>t;//等下试试scanf
while(t--){
cin>>n>>m;//红蓝数
forplus(i,0,n){
cin>>r[i];
}
forplus(i,0,m){
cin>>b[i];
}
sort(r,r+n);
sort(b,b+m);
int ma=(int)(lower_bound(r,r+n,b[0])-r);
forplus(i,1,m){
//看i和i-1中的
ma=max(ma,(int)(lower_bound(r,r+n,b[i])-upper_bound(r,r+n,b[i-1])));//注意是lower_bound(b[i+1]-upper_bound(b[i])
}
ma=max(ma,(int)(r+n-upper_bound(r,r+n,b[m-1])));
if(ma==0)cout<<"Impossible\\n";
else cout<<ma<<'\\n';
}
return 0;
}
以上是关于L. Let‘s Play Curling[思维]——2020-2021 ACM-ICPC, Asia Nanjing Regional Contest的主要内容,如果未能解决你的问题,请参考以下文章