Licking dog

Links: https://ac.nowcoder.com/acm/contest/903/J
Source: Cattle-off network

Licking dog
Time limit: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 262144K, other languages 524288K
64bit the IO the Format: LLD%

Title Description

> "Licking dog licking dog,
> finally licked,
> nothing."

There are only n dog licking, licking each dog has their own dreaming one.

While everyone has been rejected ten thousand times, also decided ten thousandth zero once courage.

As an otherworldly algorithm designer, you already disillusioned. However, this world of suffering still make you miss. See beings struggling to stick in unrequited love, you decide all living beings, for everyone to find the best outcome, let nothing try to lick the little dog, so that everyone can try and they like or dislike themselves Xiu immortal.

In other words, you need to give this dog licking paired n only for dogs lick i, he and his people dreaming aia_ {i} A i pairing. In addition, i like to lick other dogs can also be paired with him. You need to let the dog lick not been paired as little as possible.

Enter a description:

A first row of n, the number of licking dogs. 
The second row of n numbers, the i denotes an i-th digit licking dogs only dreaming ID aia_} i { A i . 2≤n≤1062 \ n-Le \ Le ^. 6 10 2 n- . 1 0 . 6

Output Description:

The first line of a number that represents the minimum number of dogs licking nothing.
Example 1

Entry

copy
10
3 1 8 6 10 1 4 1 6 1

Export

copy
0
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1e6+10;
struct node{
    int pos,val;
    bool operator<(const node &cur)const {
        return val>cur.val;
    }
};
priority_queue<node>q;
int n;
int vis[maxn];
int w[maxn],e[maxn];
int main()
{
    scanf("%d",&n);
    for(register int i=1;i<=n;++i){
        scanf("%d",w+i);
        e[w[i]]++;
    }
    for(register int i=1;i<=n;++i){
        q.push(node{i,e[i]});
    }
    while (!q.empty()){
        node cur=q.top();
        q.pop();
        int now=cur.pos;
        int nx=w[now];
        if(vis[now]||vis[nx])continue;
        vis[now]=vis[nx]=1;
        n-=2;
        e[w[nx]]--;
        q.push(node{w[nx],e[w[nx]]});
    }
    printf("%d\n",n);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/czy-power/p/11266384.html
Dog