Welcome, Guest! Sign Up RSS

Clever Space

Friday, 11.22.2024
Main » 2013 » September » 29 » [NOIP2010模拟赛]饲养员
2:25 PM
[NOIP2010模拟赛]饲养员

指针法

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
#define N 100010
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
typedef long long LL;
using namespace std;
int a[N],b[N],c[N];
int main()
{
  int n,m,i,j;
  scanf("%d %d",&n,&m);
  LL sum=0;int len=0,tot=0;
  FOR(i,1,n) scanf("%d",&a[i]);
  FOR(i,1,m) scanf("%d",&b[i]);
  sort(a+1,a+1+n);sort(b+1,b+1+m);
  i=1,j=0;
  FOR(i,1,n)
  {
    while ((j<m)&&(b[j+1]<=a[i])) c[++len]=b[++j];
    if (len>0) {tot++;sum+=(LL)(a[i]-c[len--]);}
  }
  printf("%d\n",tot);
  printf("%lld\n",sum);
}

Views: 340 | Added by: dhy0077 | Rating: 5.0/1
Total comments: 0
Only registered users can add comments.
[ Sign Up | Login ]