首页 > 其他分享 >abc282 E - Choose Two and Eat One

abc282 E - Choose Two and Eat One

时间:2023-01-04 10:45:52浏览次数:34  
标签:le int Two Choose Eat abc282

题意:

\(n\) 个球,第 \(i\) 个球上有数 \(a_i\),每次操作选两个球,得到 \((a_i^{a_j} + a_j^{a_i}) \% M\) 的收益,丢弃两球之一。重复操作直到只剩一个球,问最大收益

\(n\le 500, M \le 1e9\)

思路:

\(\forall i \neq j\),在 \(i,j\) 之间连一条边权为 \((a_i^{a_j} + a_j^{a_i}) \% M\) 的无向边。图的每棵生成树都与一种方案对应:以随便一个点为根,从下开始选边,每次丢弃儿子

跑此图的最大生成树即可

void sol() {
    cin >> n >> M;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    
    vector<array<int, 3> > edges; // weight, u, v
    for(int i = 1; i <= n; i++)
        for(int j = i + 1; j <= n; j++)
            edges.push_back({(qmi(a[i], a[j]) + qmi(a[j], a[i])) % M, i, j});
    sort(edges.begin(), edges.end());
    reverse(edges.begin(), edges.end());
    
    iota(p, p + N, 0);
    ll ans = 0;
    for(auto [w, u, v] : edges)
        if(get(u) != get(v))
            p[get(u)] = get(v), ans += w;
    cout << ans;
}

标签:le,int,Two,Choose,Eat,abc282
From: https://www.cnblogs.com/wushansinger/p/17024212.html

相关文章

  • 40. Testing Prev Part IV. Spring Boot features
    40. TestingSpringBootprovidesanumberofutilitiesandannotationstohelpwhentestingyourapplication.Testsupportisprovidedbytwomodules; ​​spri......
  • [ABC282F] Union of Two Sets
    ProblemStatementThisisaninteractivetask,whereyourandthejudge'sprogramsinteractviaStandardInputandOutput.Youandthejudgewillfollowthepro......
  • [ABC282E] Choose Two and Eat One
    ProblemStatementAboxcontains$N$balls,eachwithanintegerbetween$1$and$M-1$writtenonit.For$i=1,2,\ldots,N$,theintegerwrittenonthe$i$......
  • 提示:clnt_create: RPC: Program not registered
    提示:clnt_create:RPC:Programnotregistered在NFS服务器端(RHEL6.5)使用命令showmount-e127.0.0.1报错,提示:clnt_create:RPC:Programnotregistered解决办法:......
  • cocos creator教程:框架 - 音频
    【muzzik教程】:框架-音频此处仅谈思想,代码后续会发布在很多代码框架中、其实audio这一块的逻辑一直都没有更新、永远都只有管理音乐、音效的状态控制在我看来这......
  • cocos creator教程:框架 - 多语言
    【muzzik教程】:框架-多语言此处仅谈思想,代码后续会发布多语言需要解决的问题文本、图片不同语言之间的动态切换多状态节点(不同语言间节点属性可能不同、如位......
  • torch.repeat()
    torch.repeat函数函数功能torch.tensor.repeat()函数可以对张量进行重复扩充1)当参数只有两个时:(行的重复倍数,列的重复倍数),1表示不重复。2)当参数有三个时:(通道数的重复......
  • seata 分支事务注册及分支事务回滚时序图梳理
    1.业务时序图集成的seata版本为1.5.2。如图所示,创建三个微服务注册分支事务,整个流程中有四次DB操作,在最后执行异常抛出,用于演示分支事务注册及回滚时序。  2.各服务......
  • Linux搭建ELK+Filebeat+Redis分布式日志管理平台
    ELK介绍需求背景业务发展越来越庞大,服务器越来越多各种访问日志、应用日志、错误日志量越来越多,导致运维人员无法很好的去管理日志开发人员排查问题,需要到服务器上查日志,不......
  • Linux一键部署ELK+Filebeat+Nginx+Redis日志平台自动化脚本
    此脚本是Linux一键部署ELK+Filebeat+Nginx+Redis日志平台自动化脚本,有需要朋友可以参考,脚本内容如下:环境准备操作系统:CentOSLinuxrelease7.8.2003软件版本Elasticsearch:e......