zoukankan      html  css  js  c++  java
  • Rikka with Graph hdu 6090

    题解:考虑贪心地一条一条边添加进去。

    当 m leq n-1mn1 时,我们需要最小化距离为 nn 的点对数,所以肯定是连出一个大小为 m+1m+1 的联通块,剩下的点都是孤立点。在这个联通块中,为了最小化内部的距离和,肯定是连成一个菊花的形状,即一个点和剩下所有点直接相邻。

    当 m > n-1m>n1 时,肯定先用最开始 n-1n1 条边连成一个菊花,这时任意两点之间距离的最大值是 22。因此剩下的每一条边唯一的作用就是将一对点的距离缩减为 11。

    这样我们就能知道了最终图的形状了,稍加计算就能得到答案。要注意 mm 有可能大于 frac{n(n-1)}{2}2n(n1)​​

    队友给力,手速快,赛后补代码都补了20多分钟,,, 果然自己代码实现能力还是弱渣。

    AC代码:

    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <iostream>
    using namespace std;
    typedef long long ll;
    ll get(ll n)
    {
        ll temp=n-1ll;
        temp+=(n-1ll)*((2ll)*n-3ll);
        return temp;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            ll n,m;
            scanf("%lld %lld",&n,&m);
            ll ans=0;
            m=min(m,(n-1ll)*n/2ll);
            if(m <= n-1)
            {
                ans+=get(m+1);
                ll temp=n-m-1;
                ans+=temp*(m+1)*n*2;
                ans+=(temp-1ll)*temp*n;
                cout<<ans<<endl;
            }
            else
            {
                ll temp=get(n)-(m-(n-1))*2;
                cout<<temp<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    zoj 3279 线段树 OR 树状数组
    fzu 1962 树状数组 OR 线段树
    hdu 5057 块状链表
    hdu3487 Play with Chain
    bzoj 1588营业额统计(HNOI 2002)
    poj2823 Sliding Window
    poj2828 Buy Tickets
    poj2395 Out of Hay
    poj3667 Hotel
    poj1703 Lost Cows
  • 原文地址:https://www.cnblogs.com/z1141000271/p/7308816.html
Copyright © 2011-2022 走看看