第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南),签到题2题

简介: 第 46 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南),签到题2题

@[toc]

补题链接:https://pintia.cn/market/item/1459833348620926976

K.Search For Mafuyu

K
Search For Mafuyu
(300分)
Mafuyu has hidden in Sekai, and Kanade is searching for her.

In Sekai, there is nothing but a lot of rooms. There are n rooms in Sekai, numbered from 1 to n. Besides, n−1 pairs of rooms are directly connected by corridors, such that it is possible to move from one room to any other one, using one or more corridors. In other words, rooms in Sekai form a tree.

Kanade is at room 1, and she knows that Mafuyu may hide in any room except room 1, with uniform probability. In one second, Kanade can move to a room adjacent to the room she is currently in. Once Kanade is in the same room with Mafuyu, she immediately finds her. What is the minimum expected time for Kanade to find Mafuyu, if Kanade is taking the optimal strategy?

Input
The first line contains an integer t (1≤t≤1000) --- the number of test cases.

The first line in each test case contains an integer n (2≤n≤100) --- the number of rooms.

Each of the following n−1 lines contains two integers a
i
​
, b
i
​
(1≤a
i
​
,b
i
​
≤n) --- the rooms connected by the i-th corridor. It is guaranteed that it is possible to move from one room to any other one, using one or more corridors.

Output
Output t real numbers. For each test case, output the minimum expected time. Your answer is considered correct if the absolute or relative error is less than 10
−9
.

Sample Input
4
2
1 2
5
1 2
2 3
3 4
1 5
7
1 2
1 3
2 4
2 5
3 6
3 7
10
1 2
2 3
3 4
1 5
5 6
6 7
1 8
8 9
9 10
Output
1.0000000000
3.2500000000
5.3333333333
8.0000000000

题意:

  • T组数据(1000),每次给出一个n个点(100)的树,A和B走迷宫,A在1号点,B藏在某个树中某个点。
  • 求A找到B的最短期望时间。

思路:

  • 最短期望时间,即最优查找策略肯定是先走大小小的子树,再依次走大小大的。所以先跑一遍dfs记录每个节点子树的大小。
  • 然后再跑一遍dfs,对于遇到的节点x,按照子树大小从小往大的走。开个变量维护当前走的步数tim,每次遇到一个点就累加到ans上去,最后除掉n-1即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 550;

vector<int>G[maxn];
int siz[maxn];
void dfs(int x, int fa){
   
    siz[x] = 1;
    for(int to : G[x]){
   
        if(to!=fa){
   
            dfs(to,x);
            siz[x] += siz[to];
        }
    }
}

bool cmp(int x, int y){
   return siz[x]<siz[y]; }
int tim = 0, res = 0;
void dfs2(int x, int fa){
   
    res += tim;
    sort(G[x].begin(),G[x].end(),cmp);
    for(int to : G[x]){
   
        if(to!=fa){
   
            tim++;
            dfs2(to, x);
            tim++;
        }
    }
}

int main(){
   
    int T;  cin>>T;
    while(T--){
   
        memset(siz,0,sizeof(siz));
        int n;  cin>>n;
        for(int i = 0; i <= n; i++)G[i].clear();
        for(int i = 1; i < n; i++){
   
            int u, v;  cin>>u>>v;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        dfs(1,0);
        res = tim = 0;
        dfs2(1,0);
        printf("%.11lf\n", res*1.0/(n-1));
    }
    return 0;
}

C.Optimal Strategy

C
Optimal Strategy
(300分)
Ena and Mizuki are playing a game.

There are n items in front of them, numbered from 1 to n. The value of the i-th item is a
i
​
. Ena and Mizuki take turns to move, while Ena moves first. In a move, the player chooses an item that has not been taken and takes it away. The game ends when all items are taken away. The goal of either player is to maximize the sum of values of items they have taken away.

Given that both players move optimally, how many possible game processes are there? Since the number may be too large, you should output it modulo 998244353.

Two processes are considered different if there exists some integer i(1≤i≤n) such that the indices of items taken away in the i-th move are different.

Input
The first line contains an integer n (1≤n≤10
6
).

The second line contains n integers a
1
​
,a
2
​
,…,a
n
​
(1≤a
i
​
≤n).

Output
Output the answer.

Sample Input 1
3
1 2 2
Sample Output 1
4
Sample Input 2
6
1 3 2 2 3 1
Sample Output 2
120
Sample Input 3
12
1 1 4 5 1 4 1 9 1 9 8 10
Sample Output 3
28800
Explanations
In the first example, there are four possible processes:

[1,2,3].
[1,3,2].
[2,3,1].
[3,2,1].
Here [a,b,c] means that in the first move Ena takes away the a-th item, in the second move Mizuki takes away the b-th item, and in the final move Ena takes away the c-th item.

Note that [2,1,3] is not a possible process, since the second move is not optimal for Mizuki.

题意:

  • 给定n个物品,每个物品有一个价值。 Ena和Mizuki轮流取物品,每个人的得分是其所取物品的价值总和。
  • Ena先手,每个人取的都是当前的最优解,问结束时中间有多少种不同的取法。答案模998244353。
  • 如果存在一些整数相等,但是下标不同,算为两种方式。

思路:

  • 要让和最大,自然想到每个数都不能太小。若直接贪心每次双方都取最大第一个样例都解释不通。
    再模拟样例二发现暗示比较明显,每个数都出现偶数次,样例三便是奇偶出现次数都有。

  • 首先考虑当前最大值,如果是偶数个,那么会每次被两个两个的取;如果是奇数个,那么会被先手立刻取走一个,变成偶数的情况。
  • 开个桶数组维护每个数字的出现次数,第 i 个数出现 c[i] 次。
    对于奇数次的数,先手必然取走最大的一个数,后手取走次大的出现的奇数次的数,对结果不产生影响。
    对于偶数次的数,双方轮流从大到小拿,一方拿走一个另一方会立刻拿走相同的数,取当前数时有c[i]/2种方案。
  • 从小到大考虑每个数的出现次数,当前数的可选方案为c[i]/2。比当前数小的数的出现次数和sum可直接作为总和选取(因为是从小到大遍历,现在自己取了能取的最小值,之前的数是都不符合贪心策略的,因为它们都比最小值小,只有后面的数要符合贪心,因此前面的数在前面的过程中可以按照任意顺序取)
    大数一定可在小数之前被选,所以每个数的方案为C(c[i]/2+sum,c[i]/2),每个数内部排列方案数为 c[i]!。
  • 组合数需要预处理,不然会T。
  • 参考https://blog.csdn.net/m0_57050876/article/details/121356251

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL maxn = 1e6+10, mod = 998244353;

//预处理阶乘和逆元求组合数
LL fac[maxn], inv[maxn];
LL mpow(LL a, LL x) {
    if(x==0)return 1; LL t = mpow(a, x>>1); if(x%2==0)return t*t%mod; return t*t%mod*a%mod; }
LL init(LL _n){
   
    fac[0]=inv[0]=1;
    for(int i = 1; i < _n; i++){
   
        fac[i]=fac[i-1]*i%mod; inv[i]=mpow(fac[i],mod-2);
    }return 0;
}
LL C(LL x, LL y) {
   
    if(y<0 || y>x)return 0;
    return fac[x]*inv[y]%mod*inv[x-y]%mod;
}

LL c[maxn], mx, res = 1;

int main(){
   
    init(maxn-1);
    LL n;  cin>>n;  
    for(int i = 1; i <= n; i++){
   
        LL x;  cin>>x;  c[x]++;  mx = max(mx, x);
    }
    LL sum = 0;
    for(int i = 1; i <= mx; i++){
   
        if(c[i]==0)continue;
        LL up = floor(c[i]/2), down = up+sum;
        res = res*fac[c[i]]%mod*C(down, up)%mod;
        sum += c[i];
    }
    cout<<res%mod<<"\n";
    return 0;
}
目录
相关文章
|
JSON 自然语言处理 安全
百度工程师厂外生存指南
百度曾经一度被称为中国互联网的黄埔军校。这句话其实有两方面含义:一是说从百度走出来的工程师活跃在中国各大互联网企业中,对整个中国互联网的繁荣发展做出了贡献。二是说百度如同历史上的黄埔军校一般,为外界培育和输送了大量人才,但是自身却在逐步没落,暗示百度的人才流失严重。然而很多百度厂内高管常以『百度是中国互联网的黄埔军校』而自豪,这只是理解了这句话的第一层含义,却殊不知其第二层。高管们不对厂内人才大量流失的原因做反思,反而因为一句黄埔军校而沾沾自喜。着实让人唏嘘不已。
2248 1
百度工程师厂外生存指南
阿里云轻量应用服务器无固定流量是什么意思?不限流量吗?
阿里云轻量应用服务器“无固定流量”即不限制公网出网流量,套餐价已含无限流量,无超额费用(入网免费)。区别于旧版“每月流量”配额套餐。现2核2G起低至38元/年,200Mbps带宽,性价比高。
|
2月前
|
存储 运维 安全
阿里云无影云电脑服务平台是什么?功能详解、无影灵豆计费、客户端接入全教程
综合来看,阿里云无影云电脑服务平台以云端算力替代实体电脑,覆盖个人办公、家庭共享、企业研发运维、教育等多场景,支持多终端灵活接入、按需升降配。个人用户从9.9元/月入门,按无影灵豆费率计费,低频使用选月套包、高频使用叠加核时包;企业用户按规模选择120/250/360小时时长套餐或不限时长套餐,支持包年优惠,配合统一运维与数据安全管控。建议先通过免费试用验证使用体验,再结合开机时长、规格模式与灵豆消耗规律选择合适的计费方案,及时设置断连定时关机策略避免时长浪费,真正发挥无影云电脑安全、高效、低门槛的优势。
620 0
|
2月前
|
Web App开发 人工智能 Rust
被剪映逼疯的2亿创作者,终于等来了一个“掀桌子”的人
剪映付费墙高筑,创作者深陷“工具焦虑”。开源项目OpenCut应运而生:免费、本地处理、无水印、零上传,操作类剪映,浏览器即开即用。虽功能尚简、稳定性待提升,但已成隐私与自由的可靠备选。8.7万+ Star见证众望所归。(239字)
369 0
|
4月前
|
人工智能 弹性计算 运维
阿里云服务器专属活动参考:轻量云服务器38元起,云服务器99元起,热门云服务器活动信息
本文全面介绍阿里云2026年云服务器专属优惠活动,该活动精准覆盖个人开发者、AI创造者、一人公司、中小型企业、中大型企业及学生六大用户群体,提供从68元轻量应用服务器到GPU实例等差异化产品矩阵。针对不同场景,活动推出个人入门级特惠、AI Agent开发套餐、OPC创业包、企业级算力实例及学生专享抵扣券等多层次优惠,并设置每日限量秒杀与免费试用环节。文章按用户类型详细拆解核心产品组合、价格优势与适用场景,旨在帮助各类用户高效匹配需求,实现高性价比上云。
阿里云服务器专属活动参考:轻量云服务器38元起,云服务器99元起,热门云服务器活动信息
|
4月前
|
存储 人工智能 运维
新版无影云电脑全功能解析:AI 智能体、运维管控与 API 实操教程
随着远程办公、研发安全沙箱、AI智能体运行环境的业务需求持续增长,传统物理PC和老旧云桌面方案逐渐暴露出硬件迭代慢、批量运维繁琐、数据泄露风险高、AI任务算力不足等一系列痛点。新版无影云电脑作为一套完整的云端桌面服务平台,重构底层调度架构,在原有云桌面基础之上新增AI智能体运行环境、批量运维能力、镜像在线制作、精细化安全策略、多终端体验优化以及完备开放API体系,既能够满足企业大规模办公桌面交付,也可以作为Agent程序7×24小时不间断运行的专属算力容器,兼顾普通办公、图形设计、程序开发、AI自动化任务等多元化业务场景。整套平台将计算、存储全部放置云端,本地终端仅负责画面渲染与输入输出,原始
471 0
|
5月前
|
存储 人工智能 自然语言处理
2026最新AI办公助手场景提效全指南
《2026最新AI办公助手场景提效全指南》聚焦职场高频低效痛点,基于作者半年实战经验,系统梳理会议纪要、周报生成、项目同步、知识沉淀、数据分析等六大核心场景的AI提效逻辑与落地方法,涵盖工具选型、岗位适配及避坑建议,助你释放80%重复劳动时间,专注高价值决策与创意工作。(239字)
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?阿里云4核8g服务器1个月和1小时最新价格参考
现在阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享,热门ECS实例及CPU处理器型号说明,2025年最新阿里云4核8G服务器配置价格,热门ECS实例、CPU型号及参考价格整理,云服务器ECS实例规格不同费用价格也不同,经济型e实例4核8G配置159元1个月、一年价格1578元一年、按小时收费0.45元1小时,云服务器ECS计算型c9i实例4核8G配置费用371元1个月、一年7.1折优惠3327元一年,阿里云ECS云服务器4核8G配置价格表,包括1个月、一年和1小时收费:
|
10月前
|
缓存 人工智能 自然语言处理
Prompt 缓存的四种策略:从精确匹配到语义检索
本文详解Prompt缓存四大策略(精确匹配、规范化、语义相似、分层架构),直击LLM应用成本痛点——重复调用导致API费用飙升。代码示例+架构图,助你低成本提升命中率,降本30%–90%,延迟同步优化。
976 11
Prompt 缓存的四种策略:从精确匹配到语义检索
|
10月前
|
监控 安全 Windows
如何在 Active Directory 中查看用户登录历史?
要全面了解 Active Directory 用户的登录活动,首先需要启用登录审计,然后通过事件查看器、PowerShell 或专业审计工具(如 ADAudit Plus)查看安全事件日志中的关键事件 ID,例如成功登录(4624)和失败登录(4625)。
757 8