【题解】 [ZJOI2008] 泡泡堂(贪心/二分图/动态规划)

懒得复制,戳我戳我

Solution:

  • 就是有一个贪心策略:(以下假设使\(A\)队分数更高)
  • \(First:\)比较两个分值的最小值,如果\(A\)最小分比\(B\)最小分大就直接比较两个最小的,\(A\)队分数加两分。
  • \(Second:\)上一条不满足我们就比较\(A\)\(B\)最大值,如果\(A\)更大,就直接比较\(A\)队加两分
  • \(Thrid:\)上一条不满足我们就比较\(A\)最小值和\(B\)最大值,如果相等就都加一分,否则就\(B\)加两分

  • 以上贪心不会证明,可以去百度百科看看
  • 二分图的话可以把\(A\)点中一点连向\(B\)中比他小的点,然后找最大匹配啥的乱搞一下应该也行

Code:

//It is coded by Ning_Mew on 4.7
#include<bits/stdc++.h>
using namespace std;

const int maxn=1e5+7;

int n;
int sl[maxn],ds[maxn];

bool cmp(const int &x,const int &y){return x<y;}
int work(int num){
  int pl=1,lt=n,pll=1,ltt=n,ans[3]={0,0,0};
  while(pl<=lt){
    if(sl[pl]>ds[pll]){
      ans[1]+=2; pl++;pll++;continue;
    }else{
      if(sl[lt]>ds[ltt]){
	ans[1]+=2; lt--;ltt--;continue;
      }
      else{
	if(sl[pl]==ds[ltt]){
	  ans[1]++;ans[2]++; pl++;ltt--; continue;
	}else{
	  ans[2]+=2;pl++;ltt--;continue;
	}
      }
    }
  }
  return ans[num];
}
int main(){
  scanf("%d",&n);
  for(int i=1;i<=n;i++)scanf("%d",&sl[i]);
  for(int i=1;i<=n;i++)scanf("%d",&ds[i]);
  sort(sl+1,sl+n+1,cmp);
  sort(ds+1,ds+n+1,cmp);
  printf("%d ",work(1));
  for(int i=1;i<=n;i++)swap(sl[i],ds[i]);
  printf("%d\n",work(2));
  return 0;
}

posted @ 2018-04-08 00:47  Ning_Mew  阅读(215)  评论(0编辑  收藏  举报