连续攻击游戏

题目描述

lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示。当他使用某种装备时,他只能使用该装备的某一个属性。并且每种装备最多只能使用一次。游戏进行到最后,lxhgww遇到了终极boss,这个终极boss很奇怪,攻击他的装备所使用的属性值必须从1开始连续递增地攻击,才能对boss产生伤害。也就是说一开始的时候,lxhgww只能使用某个属性值为1的装备攻击boss,然后只能使用某个属性值为2的装备攻击boss,然后只能使用某个属性值为3的装备攻击boss……以此类推。现在lxhgww想知道他最多能连续攻击boss多少次?

输入输出格式

输入格式:

输入的第一行是一个整数N,表示lxhgww拥有N种装备接下来N行,是对这N种装备的描述,每行2个数字,表示第i种装备的2个属性值

输出格式:

输出一行,包括1个数字,表示lxhgww最多能连续攻击的次数。

输入输出样例

输入样例#1:  复制
3
1 2
3 2
4 5
输出样例#1:  复制
2

说明

Limitation

对于30%的数据,保证N < =1000

对于100%的数据,保证N < =1000000

Solution:

以属性值为左顶点,编号1~n为右顶点连边,二分匹配即可,注意因为要连续,所以第一个无法匹配的就要break。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include<bits/stdc++.h>
#define REP(i, a, b) for(int i = (a); i <= (b); ++ i)
#define REP(j, a, b) for(int j = (a); j <= (b); ++ j)
#define PER(i, a, b) for(int i = (a); i >= (b); -- i)
using namespace std;
typedef long long ll;
template <class T>
inline void rd(T &ret){
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9'){
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}
struct node{int v,nx;}p[2000005];
int n,maxn,ans,tot,vis[10005],mh[1000006],head[10006];
void addedge(int u,int v){p[++tot].v=v,p[tot].nx=head[u],head[u]=tot;}
int match(int cur){
      if(vis[cur])return 0;
      vis[cur]=1;
      for(int i=head[cur];i;i=p[i].nx){
          if(!mh[p[i].v]||match(mh[p[i].v])){
              mh[p[i].v]=cur;
              return 1;
          }
      }
      return 0;
}
int main(){
    rd(n);
    REP(i,1,n){
       int u,v;
       rd(u),rd(v);
       addedge(u,i),addedge(v,i);
       maxn=max(maxn,max(u,v));
    }
    REP(i,1,maxn){
       memset(vis,0,sizeof(vis));
       if(match(i))ans++;
       else break;
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/czy-power/p/10396893.html