5-1889B - Doremy's Connecting Plan

发布时间 2023-11-21 16:45:13作者: xxj112

题意:

思路:
\(i*j*c 越小越有利, 可以让i为1 ,即都与节点1建边, 1节点集合拥有的人越多越有利于于别的集合相连, 对别的点按照,最小要求人数-现有人数=需要人数排序,按顺序连接即可。\)
代码:

点击查看代码
#define int long long 
using namespace std;
typedef pair<int,int> pll;
typedef tuple<int,int,int> TP;
const int N = 1e6 + 10;
int a[N];
int n,c;
//一个集合包括:最小的i ,总人数  
// 考虑贪心,  都和节点1建边 按照 
// j人数+1人数>= 1*j*c;  
// j*c-j人数==1需要的最小人数,按这个从小到达排序;
struct node
{
    int r,qr;
}e[N];
//先按照需要人数,在按照拥有人数
//其实只按照需求人数即可
bool cam(node x,node y)
{
    if(x.qr==y.qr) return x.r>y.r;
    return x.qr<y.qr;
}
void solve()
{
    cin>>n>>c;
    int x;//初始人数
    cin>>x;
    int w;
    for(int i=2;i<=n;i++)
    {
        cin>>w;
        e[i].r=w;
        e[i].qr=c*i-w;
      
    }
    sort(e+2,e+n+1,cam);
    for(int i=2;i<=n;i++)
    {
        if(x<e[i].qr)
        {
            cout<<"NO\n";
            return ;
        }
        x+=e[i].r;
    }
    cout<<"YES\n";
}
signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0 ^ 0;
}