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

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

@[toc]

补题链接:https://codeforces.com/gym/102992
https://ac.nowcoder.com/acm/contest/10272

K. Co-prime Permutation

K. K Co-prime Permutation
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Kotori is very good at math (really?) and she loves playing with permutations and primes.

One day, she thinks of a special kind of permutation named k co-prime permutation. A permutation p1,p2,⋯,pn of n is called a k co-prime permutation of n if there exists exactly k integers i such that 1≤i≤n and gcd(pi,i)=1, where gcd(x,y) indicates the greatest common divisor of x and y.

Given n and k, please help Kotori construct a k co-prime permutation of n or just report that there is no such permutation.

Recall that a permutation of n is a sequence of length n containing all integers from 1 to n.

Input
There is only one test case in each test file.

The first and only line contains two integers n and k (1≤n≤106, 0≤k≤n).

Output
Output one line containing n integers p1,p2,⋯,pn separated by one space, indicating the permutation satisfying the given constraints. If no such permutation exists output "-1" (without quotes) instead. If there are multiple valid answers you can print any of them.

Please, DO NOT output extra spaces at the end of each line, otherwise your answer may be considered incorrect!

Examples
inputCopy
5 3
outputCopy
1 4 5 2 3
inputCopy
1 0
outputCopy
-1

题意:

  • 给出n和k,求n的一个排列P,满足其中有k个gcd(i,Pi)==1.

思路:

  • 已知gcd(x,x+1)=1,gcd(1,x)=1, gcd(x,x)=x,,所以直接初始排列移动一下前面k个即可。
#include<bits/stdc++.h>
using namespace std;
vector<int>a(1000005);
int main(){
   
    int n, k;  cin>>n>>k;
    if(k==0)cout<<"-1";
    else{
   
        for(int i = 1; i <= n; i++)a[i]=i;
        a.erase(a.begin()+k);
        a.insert(a.begin()+1,k);
        for(int i = 1; i <= n; i++)
            cout<<a[i]<<" ";
    }
    return 0;
}

L. Let‘s Play Curling

L. Let's Play Curling
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Curling is a sport in which players slide stones on a sheet of ice toward a target area. The team with the nearest stone to the center of the target area wins the game.

Two teams, Red and Blue, are competing on the number axis. After the game there are (n+m) stones remaining on the axis, n of them for the Red team and the other m of them for the Blue. The i-th stone of the Red team is positioned at ai and the i-th stone of the Blue team is positioned at bi.

Let c be the position of the center of the target area. From the description above we know that if there exists some i such that 1≤i≤n and for all 1≤j≤m we have |c−ai|<|c−bj| then Red wins the game. What's more, Red is declared to win p points if the number of i satisfying the constraint is exactly p.

Given the positions of the stones for team Red and Blue, your task is to determine the position c of the center of the target area so that Red wins the game and scores as much as possible. Note that c can be any real number, not necessarily an integer.

Input
There are multiple test cases. The first line of the input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers n and m (1≤n,m≤105) indicating the number of stones for Red and the number of stones for Blue.

The second line contains n integers a1,a2,⋯,an (1≤ai≤109) indicating the positions of the stones for Red.

The third line contains m integers b1,b2,⋯,bm (1≤bi≤109) indicating the positions of the stones for Blue.

It's guaranteed that neither the sum of n nor the sum of m will exceed 5×105.

Output
For each test case output one line. If there exists some c so that Red wins and scores as much as possible, output one integer indicating the maximum possible score of Red (NOT c). Otherwise output "Impossible" (without quotes) instead.

Example
inputCopy
3
2 2
2 3
1 4
6 5
2 5 3 7 1 7
3 4 3 1 10
1 1
7
7
outputCopy
2
3
Impossible
Note
For the first sample test case we can assign c=2.5 so that the stones at position 2 and 3 for Red will score.

For the second sample test case we can assign c=7 so that the stones at position 5 and 7 for Red will score.

题意:

  • 给出n个红石头ai,m个蓝石头bi的所在位置,求确定一个位置C使得存在ai对于所有的bi都满足|c-ai|<|c-bi|,并且让ai数量尽可能多,输出最多的ai数量。

思路:

  • 实际上就是求两个蓝石头之间最多有几个红石头。
  • 先排个序,枚举蓝石头,每次找到第一个比b[i-1]大的和最后一个比b[i]小的红石头的位置,减一下就是中间的红石头个数,开个ans维护答案。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 200005;
int a[maxn], b[maxn];
int main(){
   
    ios::sync_with_stdio(false);
    int T;  cin>>T;
    while(T--){
   
        int n, m;  cin>>n>>m;
        for(int i=1;i<=n;i++)cin>>a[i];
        for(int i=1;i<=m;i++)cin>>b[i];
        sort(a+1,a+n+1);
        sort(b+1,b+m+1);
        b[0]=-1e9; b[m+1] = 2e9;
        LL ans = -1;
        for(int i=1;i<=m+1;i++){
   
            int l=upper_bound(a+1,a+n+1,b[i-1])-a;
            int r=lower_bound(a+1,a+n+1,b[i])-a-1;
            ans = max(ans,LL(r-l+1));
        }
        if(ans<=0)cout<<"Impossible\n";
        else cout<<ans<<"\n";
    }
    return 0;
}

E. Evil Coordinate

E. Evil Coordinate
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A robot is standing on an infinite 2-dimensional plane. Programmed with a string s1s2⋯sn of length n, where si∈{'U','D','L','R'}, the robot will start moving from (0,0) and will follow the instructions represented by the characters in the string.

More formally, let (x,y) be the current coordinate of the robot. Starting from (0,0), the robot repeats the following procedure n times. During the i-th time:

If si='U' the robot moves from (x,y) to (x,y+1);
If si='D' the robot moves from (x,y) to (x,y−1);
If si='L' the robot moves from (x,y) to (x−1,y);
If si='R' the robot moves from (x,y) to (x+1,y).
However, there is a mine buried under the coordinate (mx,my). If the robot steps onto (mx,my) during its movement, it will be blown up into pieces. Poor robot!

Your task is to rearrange the characters in the string in any order, so that the robot will not step onto (mx,my).

Input
There are multiple test cases. The first line of the input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers mx and my (−109≤mx,my≤109) indicating the coordinate of the mine.

The second line contains a string s1s2⋯sn of length n (1≤n≤105, si∈{'U','D','L','R'}) indicating the string programmed into the robot.

It's guaranteed that the sum of n of all test cases will not exceed 106.

Output
For each test case output one line. If a valid answer exists print the rearranged string, otherwise print "Impossible" (without quotes) instead. If there are multiple valid answers you can print any of them.

Example
inputCopy
5
1 1
RURULLD
0 5
UUU
0 3
UUU
0 2
UUU
0 0
UUU
outputCopy
LDLRUUR
UUU
Impossible
Impossible
Impossible

题意:

  • 给出一个字符串,UDLR分别表示向上下左右移动。默认在坐标原点(0,0),求更改字符顺序,让其移动不经过题目地雷点(mx,my)。

思路:

  • 可以证明存在一种答案,使得相同的方向是连续排在一起的,即枚举UDLR的24种排列即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const char a[]="UDLR";//用0123对应ULDR,a是字符,c是数量,q是排列。
const int dx[]={
   0,0,-1,1},dy[]={
   1,-1,0,0};
int main(){
   
    //ios::sync_with_stdio(false); WA
    int T;  cin>>T;
    while(T--){
   
        int mx, my;  cin>>mx>>my;
        string s;  cin>>s;
        int c[4] = {
   };
        for(int i=0;i<s.size();i++){
   
            if(s[i]=='U')c[0]++;
            if(s[i]=='D')c[1]++;
            if(s[i]=='L')c[2]++;
            if(s[i]=='R')c[3]++;
        }
        //for(int i =0; i<4;i++)cout<<c[i]<<"\n";

        //起点或终点重合
        int tx=c[3]-c[2],ty=c[0]-c[1];
        if(tx==mx&&ty==my || mx==0&&my==0){
   
            cout<<"Impossible\n";
            continue;
        }

        int q[4]={
   0,1,2,3}, ook=0;
        do{
   
            int x=0,y=0,ok=1;
            for(int i=0; i<=3; i++){
   //方向
                for(int j=0;j<c[q[i]];j++){
   //移动(方向q[i]有长度c[q[i]])
                    x += dx[q[i]], y+=dy[q[i]];
                    if(x==mx&&y==my){
   
                        ok=0; break;
                    }
                }
                if(ok==0)break;
            }
            if(ok==1){
   //没有雷就全方向输出
                for(int i=0; i<=3; i++)
                    for(int j=0; j<c[q[i]];j++)
                        putchar(a[q[i]]);
                cout<<"\n";
                ook=1;
                break;
            }
        }while(next_permutation(q,q+4));
        if(ook)continue;
        cout<<"Impossible\n";
    }
    return 0;
}

F. Fireworks

Fireworks
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Kotori is practicing making fireworks for the upcoming hanabi taikai1
. It takes her n minutes to make a
single firework, and as she is not really proficient in making fireworks, each firework only has a probability
of p × 10−4
to be perfect.
After she finishes making a firework, she can just start making the next firework, or take m minutes to
light all the remaining fireworks finished before. If there is at least one perfect firework among the lit ones,
she will be happy and go to rest. Otherwise, she will continue practicing. Can you tell her the minimum
expected practicing time before she goes to rest if she takes the optimal strategy?
Notice that no matter how many fireworks remain, it always takes m minutes to light them all.
Input
There are multiple test cases. The first line of the input contains an integer T (1 ≤ T ≤ 104
) indicating
the number of test cases. For each test case:
The first and only line contains three integers n, m and p (1 ≤ n, m ≤ 109
, 1 ≤ p ≤ 104
).
Output
For each test case, output one line containing one number indicating the minimum expected practicing
time.
Your answer will be considered correct if and only if the absolute or relative error does not exceed 10−4
.
Example
standard input standard output
3
1 1 5000
1 1 1
1 2 10000
4.0000000000
10141.5852891136
3.0000000000

题意:

  • 制作一个烟花需要n分钟,成功的概率为p。完成一次制作后可以继续制作或花m分钟点燃之前所有的烟花,如果有一个烟花是成功的,那么就可以开始休息。
  • 求最小的期望开始休息时间(即期望的最早成功制作一个烟花的时间)

思路:

  • 因为每次都是做出来一批集中释放做好的看看有没有成功的,所以不妨设最优策略为每次做k个能最早休息。所以问题转化为每次制作k个释放,期望的最早制作成功的时间。
  • 每轮制作耗费时间kn+m,至少一个烟花制作成功的概率为$(1-(1-p)^k)$,所以根据几何分布公式可以得到期望为$\frac{kn+m}{1-(1-p)^k}$。
  • 对该式子打表或者求二阶导可以发现这是个单峰的凹函数。可以三分答案。
#include<bits/stdc++.h>
using namespace std;
typedef long double LD;
LD fun(int k, int n, int m, LD p){
   
    return ((LD)k*n+m)/((LD)1.0-pow(1.0-p,k));
}
int main(){
   
    int T;  cin>>T;
    while(T--){
   
        int n, m;  LD p;
        cin>>n>>m>>p;
        p *= (1e-4);
        int l = 1, r = 1e9+10;
        while(l < r){
   
            int mid1 = l+(r-l)/3, mid2 = r-(r-l)/3;
            if(fun(mid1,n,m,p)<fun(mid2,n,m,p))r = mid2-1;
            else l = mid1+1;
        }
        printf("%.10Lf\n", fun(l,n,m,p));
    }
    return 0;
}
目录
相关文章
|
存储 消息中间件 SQL
Flink 基础学习(五)数据存储
前面两篇笔记已经写了数据来源和转换如何使用,那么这篇当然就到了数据存储,接下来将会从以下角度介绍一下(喜闻乐见的 What / Why / How)~:
2065 0
Flink 基础学习(五)数据存储
|
9月前
|
安全 编译器 C语言
C语言深度解析:严格别名规则——指针强转的隐形陷阱
C语言严格别名规则禁止不同非char指针访问同一内存,是编译器激进优化(如常量折叠)的基石。违规(如强制类型转换解析协议)会导致O2下玄学bug。安全方案:优先用memcpy(零开销)、union类型双关;避免关闭-fno-strict-aliasing。
|
10月前
|
人工智能 机器人 网络安全
2026年阿里云一键秒级部署OpenClaw(原Clawdbot)教程 快速接入QQ、企微、飞书、钉钉等IM软件
OpenClaw(原Clawdbot)作为轻量化企业级AI自动化代理工具,2026年阿里云推出的“秒级一键部署”方案彻底颠覆了传统部署流程——无需手动配置环境、编译代码,仅需3步即可完成云端部署,同时新增对QQ、企业微信、飞书、钉钉等主流IM软件的原生适配,实现“AI指令在IM端下达,阿里云服务器执行自动化任务”的全链路提效。本文将完整拆解阿里云OpenClaw秒级部署全流程,并针对不同IM软件的接入逻辑、权限配置、调试方法给出详细指南,包含实操代码命令与跨平台适配技巧,帮助企业快速打通“AI能力+IM协作”的核心场景。
1739 6
|
人工智能 自然语言处理 搜索推荐
文章“找茬”神器——媒体行业AI智能校对方案
年初DeepSeek大模型火爆以后,各行各业都在加速建设AI相关的场景,媒体行业无疑是大模型场景适配较好的一个行业。大模型凭借强大的内容生成能力,可以深度渗透内容生产的全链路环节,从热点事件的智能抓取、新闻稿件的快速生成,文章智能校对、个性化润色,大模型几乎可以重构传统内容生产流程。
1124 15
|
12月前
|
Arthas 存储 运维
记Arthas实现一次CPU排查与代码热更新
本文介绍使用Arthas排查Java应用CPU占用过高问题的完整流程,涵盖线程分析、阻塞定位、watch命令追踪异常、jad反编译实现热更新及火焰图分析,实现无需重启应用的高效故障排查与代码修复。
378 0
|
12月前
|
消息中间件 Linux API
C 语言并发编程核心原理与实践技巧
C语言虽无内置线程库,但可通过POSIX pthread实现Linux多线程并发,配合互斥锁与信号量解决数据竞争;在嵌入式场景中,FreeRTOS提供任务调度、消息队列等机制,满足实时性需求。本文详解从基础线程创建到同步控制,再到嵌入式实时落地的全流程,助你掌握高效稳定的C并发编程核心技能。
|
人工智能 数据可视化 测试技术
Coze, Dify, N8N:三款主流AI工作流平台在测试中的应用对比
在敏捷开发背景下,Coze、Dify和n8n三大AI工作流平台正革新测试自动化。Coze零代码易上手,适合AI密集型任务;Dify支持私有化部署,适配企业级复杂流程;n8n开源可控,擅长系统集成。三者各有优势,助力测试团队实现高效人机协同,提升测试效能。
|
人工智能 自然语言处理 安全
MCP 打造 AI Agent 的 5 大最佳实践
面对流量激增、系统告急,单一渠道AI Agent难堪重负?问题不在能力,在架构!MCP(多渠道平台)作为智能体“神经中枢”,可统一调度Web、微信、钉钉等多渠道任务,实现高并发、可扩展的自动化。本文详解企业级MCP构建AI Agent的五大最佳实践:模块拆分、工具选型、设计模式、上下文管理与安全可控,助你快速落地高效、稳定的智能系统。
|
Web App开发 Linux 开发工具
告别卡顿,畅享GitHub:国内开发者必看的五大加速访问与下载技巧
【8月更文挑战第4天】告别卡顿,畅享GitHub:国内开发者必看的五大加速访问与下载技巧
10966 4
告别卡顿,畅享GitHub:国内开发者必看的五大加速访问与下载技巧
|
存储 Ubuntu Shell
shell 用法入门
本文档详细介绍了Shell脚本的基础知识,包括基本写法、变量定义与使用、命令置换、环境变量、数组操作、算术运算、输入输出处理、控制语句及循环结构等内容。文档还提供了丰富的示例代码,帮助读者更好地理解和掌握Shell编程技巧。此外,还介绍了如何使用`if`语句进行条件判断、`case`语句进行模式匹配以及`while`、`for`循环等控制结构。最后,文档还涵盖了函数定义与调用的方法。适合初学者和有一定基础的开发者参考学习。