【codeforces 796C】Bank Hacking(用一些技巧来代替multiset)

【题目链接】:http://codeforces.com/contest/796/problem/C

【题意】

给你n个节点,你一开始选择一个节点,然后打掉它,然后与被打掉过的节点相连的节点才能被
打掉,但打掉一个点之后,与它距离小于等于2的节点的权值会增加1(如果中间节点有被打掉的,则不增加,即被切断了);
重复上述步骤,直至所有节点被打掉,能打掉某个节点,你需要满足:你的权值大于等于这个节点的权值.求一开始你的
权值的最小值;

【题解】

思路在这里http://blog.csdn.net/harlow_cheng/article/details/70050833
这篇题解的程序;
使用的是一些技巧;
以期替代缓慢的multiset
具体的实现的时候;
需要维护最大值,和最大值的数目
以及次小值以及次小值的数目;
以及某个点和最大值的点连接了几个;
某个点和次大值的点连接了几个;
除了和当前枚举的起点距离为1的点增加1之外,其他点(除了自身)都增加2;
根据这个以及上面得到的值不难做出讨论;
主要的思路↓↓↓

/*
    if (a[i]==max1)//如果是最大值
    {
        if (cnt1[i]==tot1-1)//如果剩余的最大值都围着这个最大值
        {
            if (tot1==1)//如果只有这一个最大值
            {
                if (cnt2[i]==tot2)//如果第二小的值全都围着它
                    //那么第二小的值只会+1,不可能大过最大值
                    ans = min(ans,max1);
                else//在外圈还有max2则看看max1和max2+2哪一个大
                    ans = min(ans,max(max1,max2+2));
            }
            else
                ans = min(ans,max1+1);//这个时候max1+1肯定>=max2+2所以
                //不用考虑max2了
        }
        else//如果有剩余的最大值在外圈,则用max1+2更新答案
            ans = min(ans,max1+2);
    }
    else
    {
    //不是最大值
        if (cnt1[i]==tot1)//如果最大值都围着它 那么最大值+1更新
            ans = min(ans,max1+1);
        else
            ans = min(ans,max1+2);//有一个在外圈,那么肯定那个外圈会加2
    }
*/


【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 3e5+100;
const int INF = -1e9-100;

int a[N],n,max1=INF,max2=INF,tot1,tot2,ans=1e9+100;
int cnt1[N],cnt2[N];

void gx(int x)
{
    if (x==max1)
        tot1++;
    else
        if (x==max2)
            tot2++;
        else
        {
            if (x>max1)
            {
                swap(max1,max2);swap(tot1,tot2);
                max1 = x,tot1 = 1;
            }
            else
                if (x>max2)
                    tot2=1,max2 = x;
        }
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);
    cin >> n;
    rep1(i,1,n)
        cin >> a[i],gx(a[i]);
    rep1(i,1,n-1)
    {
        int x,y;
        cin >> x >> y;
        if (a[x]==max1) cnt1[y]++;
        if (a[y]==max1) cnt1[x]++;
        if (a[x]==max2) cnt2[y]++;
        if (a[y]==max2) cnt2[x]++;
    }
    rep1(i,1,n)
        if (a[i]==max1)
        {
            if (cnt1[i]==tot1-1)
            {
                if (tot1==1)
                {
                    if (cnt2[i]==tot2)
                        ans = min(ans,max1);
                    else
                        ans = min(ans,max(max1,max2+2));
                }
                else
                    ans = min(ans,max1+1);
            }
            else
                ans = min(ans,max1+2);
        }
        else
        {
            if (cnt1[i]==tot1)
                ans = min(ans,max1+1);
            else
                ans = min(ans,max1+2);
        }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626406.html