对于每个数字二分找到大于等于它的数,再暴力找到第一个小于它的数


 #include<bits/stdc++.h>

 #define inf 0x3f3f3f3f

 const int maxn=;

 using namespace std;

 int b,s;

 int icase;

 int m[maxn+],w[maxn+];

 int flag[maxn+];

 int flagm[maxn+];

 bool cmp(int a,int b){
return a>b;
} int firstmin(int val){
for(int i=;i<=s;i++){
if(w[i]>=val){
return i-;
}
}
return s;
} void solve(){ int x=b,y=;
memset(flag,,sizeof(flag));
memset(flagm,,sizeof(flagm));
sort(m+,m+b+);
sort(w+,w+s+);
for(int i=b;i>=;i--){
int t=x;
int p=lower_bound(w+,w+s+,m[i])-w;
int q=firstmin(m[i]);
// printf("%d %d\n",p,q);
if(p==s+){
if(!flag[q]){
flag[q]=;
x--;
} else{
for(int h=q-;h>=;h--){
if(!flag[h]){
flag[h]=;
x--;
break;
}
}
}
} else if(q==){
if(!flag[p]){
flag[p]=;
x--;
} else {
for(int h=p+;h<=s;h++){
if(!flag[h]){
flag[h]=;
x--;
break;
}
}
}
} else if(p!=s+&&q!=){
int r=inf,v=inf;
if(!flag[p]){
r=abs(m[i]-w[p]);
}
if(!flag[q]){
v=abs(m[i]-w[q]);
}
if(!flag[p]&&!flag[q]){
if(v<=r){
flag[q]=;
x--;
} else {
flag[p]=;
x--;
}
} else if(flag[p]&&!flag[q]){
flag[q]=;
x--;
} else if(!flag[p]&&flag[q]){
flag[p]=;
x--;
} else if(flag[p]&&flag[q]){
int f=;
for(int h=q+;h<p;h++){
if(!flag[h]){
flag[h]=;
x--;
f=;
break;
}
}
if(!f){
int ff=,fff=;
for(int h=p+;h<=s;h++){
if(!flag[h]){
ff=h;
break;
}
}
for(int h=q-;h>=;h--){
if(!flag[h]){
fff=h;
break;
}
}
if(ff&&fff){
if(abs(m[i]-w[ff])<=abs(m[i]-w[fff])){
flag[ff]=;
x--;
} else {
flag[fff]=;
x--;
}
} else if(ff&&!fff){
flag[ff]=;
x--;
} else if(!ff&&fff){
flag[fff]=;
x--;
}
}
} }
if(t==x+) flagm[i]=;
}
for(int i=;i<=b;i++){
if(!flagm[i]) {
y=i;
break;
}
}
printf("%d",x);
if(x) printf(" %d",m[y]);
} void input(){
for(int i=;i<=b;i++){
scanf("%d",&m[i]);
}
for(int i=;i<=s;i++){
scanf("%d",&w[i]);
}
printf("Case %d: ",++icase);
} int main()
{
//freopen("e:\\duipai\\out1.txt","w",stdout);
while(scanf("%d%d",&b,&s)!=EOF&&(b||s)){
input();
solve();
printf("\n");
}
return ;
}


Uva12210-A Match Making Problem的更多相关文章

  1. Python中的正则表达式regular expression

    1 match = re.search(pat,str)  If the search is successful, search() returns a match object or None o ...

  2. Codeforces Gym 100650B Countdown DFS

    Problem B: CountdownTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/conte ...

  3. (zhuan) Notes on Representation Learning

    this blog from: https://opendatascience.com/blog/notes-on-representation-learning-1/   Notes on Repr ...

  4. HGOI 20190708 题解

    Problem A 拿出勇气吧 幸运数字每一位是$4$或者$7$,现在给出一个数字每位数位上数的和为n,求出最小的幸运数n 对于100%的数据,$n\leq 10^6$ Sol : 显然本题要求数的长 ...

  5. Description Resource Path Location Type Java compiler level does not match the version of the installed Java project facet Unknown Faceted Project Problem (Java Version Mismatch)

    project 编译问题,需要三处的jdk版本要保持一致,才能编译通过. 1.在项目上右键properties->project Facets->修改右侧的version  保持一致 2. ...

  6. svn:Repository UUID 'XXX' doesn't match expected UUID 'YYY'

    About a month ago, CodePlex have upgraded their TFS servers to to TFS 2010. While this transition wa ...

  7. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  8. Scala学习文档-样本类与模式匹配(match,case,Option)

    样本类:添加了case的类便是样本类.这种修饰符可以让Scala编译器自动为这个类添加一些语法上的便捷设定. //样本类case class //层级包括一个抽象基类Expr和四个子类,每个代表一种表 ...

  9. [Regular Expressions] Match the Start and End of a Line

    We can use: ^: match the beginning $: match the end Let's say we have the string like the following: ...

随机推荐

  1. 使用 DNSPOD API 实现域名动态解析

    0. 简单概述在家里放一个 NAS 服务器,但是宽带的 IP 地址经常改变,一般路由器自带的花生壳域名解析可以解决,如果路由器没有类似功能或者想使用自己的域名,可以尝试使用 DNSPOD API 来实 ...

  2. 【Codeforces】894E.Ralph and Mushrooms Tarjan缩点+DP

    题意 给定$n$个点$m$条边有向图及边权$w$,第$i$次经过一条边边权为$w-1-2.-..-i$,$w\ge 0$给定起点$s$问从起点出发最多能够得到权和,某条边可重复经过 有向图能够重复经过 ...

  3. Quartz.Net初探

    想必大家在工作中经常会遇到这样类似的需求,在某个时间或者需要不间断的执行某个业务动作去满足任务需求.例如,我们写了一个job,定时去处理一些任务,在没有了解到Quartz.Net之前,我是这样做的,进 ...

  4. poj-1986 Distance Queries(lca+ST+dfs)

    题目链接: Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 11531   Accepted ...

  5. Gym - 101341I:Matrix God(随机算法)

    题意:给出N,以及三个矩阵A,B,C,大小都为N*N.问是否满足A*B=C: N<1000: 思路:由于矩阵乘法的复杂度为O(N^3):而部分验证又不能保证结果正确.我们巧妙地利用矩阵乘法的结合 ...

  6. cm 安装

    为Cloudera Manager建立数据库:/usr/share/cmf/schema/scm_prepare_database.sh mysql -h[mysql数据库的主机名] -P63751 ...

  7. AIM Tech Round 4 (Div. 2)

    A题 分析:暴力 #include "iostream" #include "cstdio" #include "cstring" #inc ...

  8. Poj_1011_Sticks(剪枝)

    一.Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度. ...

  9. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  10. 虚拟机ubuntu和windows共享文件

    设置虚拟机ubuntu和windows共享文件(方便复制文件到本机) (需要安装samba)   登陆ubuntu系统后,点击左边的文件夹.右击新建个文件夹.   设置为共享   ubuntu会提示你 ...