zoukankan      html  css  js  c++  java
  • 枪战(maf)

    枪战(maf)

    settle the dispute. Negotiations were very tense, and at one point the trigger-happy participants drew their

    guns. Each participant aims at another with a pistol. Should they go on a killing spree, the shooting will go

    in accordance with the following code of honour:

    the participants shoot in a certain order, and at any moment at most one of them is shooting,

    no shooter misses, his target dies instantly, hence he may not shoot afterwards,

    everyone shoots once, provided he had not been shot before he has a chance to shoot,

    no participant may change his first target of choice, even if the target is already dead (then the shot

    causes no further casualties).

    An undertaker watches from afar, as he usually does. After all, the mobsters have never failed to stimulate

    his business. He sees potential profit in the shooting, but he would like to know tight estimations. Precisely

    he would like to know the minimum and maximum possible death rate. The undertaker sees who aims at

    whom, but does not know the order of shooting. You are to write a programme that determines the numbers

    reads from the standard input what target each mobster has chosen,

    determines the minimum and maximum number of casualties,

    输入

    第1行:人数 1<=n<=1,000,000 

    第2行:包含了n 个整数s1,s2,...,sn,si表示第i个人瞄准的目标,注意一下第i个的目标可能为i 。

    输出

    输出2个整数。枪战后可能的最小和最大的死亡人数。


    solution

    考虑贪心。

    首先没有入度的点一定不会死。假设为A

    那么他们瞄准的人B一定会死,而B瞄准的人可能可以不死。

    于是删除B的出边,类似拓扑序,一个点如果没有入边,它就可以不死。

    还剩下若干环。

    环上的点最少死n/2

    最多:

    1.如果这个环有入边(就是它不是一个单独的环,环外可以打到环上)

     那么可以全死。因为让那个被环外打的人开枪

    不然会活一个人

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define maxn 1000006
    using namespace std;
    int n,a[maxn],in[maxn];
    int q[maxn],l,r,ans1,ans2,die[maxn];//ans1 min ans2 max
    int flag[maxn];
    int main()
    {
        cin>>n;
        for(int i=1;i<=n;i++){scanf("%d",&a[i]);in[a[i]]++;}
        for(int i=1;i<=n;i++){
            if(!in[i])q[++r]=i,ans1++;
        }
        for(int i=1;i<=r;i++){
            int u=q[i];int v=a[u];
            if(die[v])continue;
            die[v]=1;
            in[a[v]]--;flag[a[v]]=1;// flag=1 exist a entrance
            if(!in[a[v]])q[++r]=a[v];
        }
        ans2=r;
        for(int i=1;i<=n;i++){
            if(in[i]&&!die[i]){// in the circle
                int len=0,en=0;
                for(int j=i;!die[j];j=a[j]){
                    die[j]=1;len++;
                    en|=flag[j];
                }
                if(!en&&len>1)ans1++;//can't enter
                ans2+=len/2;
            }
        }
        printf("%d %d
    ",n-ans2,n-ans1);
        return 0;
    }
  • 相关阅读:
    mysql 数据操作 单表查询 group by 注意
    mysql 数据操作 单表查询 group by 聚合函数
    mysql 数据操作 单表查询 group by 聚合函数 没有group by情况下
    mysql 数据操作 单表查询 group by 介绍
    【洛谷P3586】LOG
    【YbtOJ#20078】路径之和
    【ABC181】F
    【洛谷P7045】金牌
    【AT2165】Median Pyramid Hard
    【洛谷P3708】koishi的数学题
  • 原文地址:https://www.cnblogs.com/liankewei/p/10358753.html
Copyright © 2011-2022 走看看