ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)A+B

本文包含两个算法题目:一是帮助Sherlock通过已知线索追踪连环杀手的潜在受害者;二是为Sherlock的情人节礼物难题提供解决方案,即对一系列珠宝进行着色,确保价格为质数倍数的珠宝不使用相同颜色,同时最小化所需颜色数量。

A. A Serial Killer

time limit per test:2 seconds

memory limit per test:256 megabytes

input:standard input

output:standard output

Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.

The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.

You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.

Input

First line of input contains two names (length of each of them doesn’t exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.

Next n lines contains two names (length of each of them doesn’t exceed 10), first being the person murdered on this day and the second being the one who replaced that person.

The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.

Output

Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.

Examples

Input
ross rachel
4
ross joey
rachel phoebe
phoebe monica
monica chandler

Output
ross rachel
joey rachel
joey phoebe
joey monica
joey chandler

Input
icm codeforces
1
codeforces technex

Output
icm codeforces
icm technex

Note

In first example, the killer starts with ross and rachel.
• After day 1, ross is killed and joey appears.
• After day 2, rachel is killed and phoebe appears.
• After day 3, phoebe is killed and monica appears.
• After day 4, monica is killed and chandler appears.
代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e7+10;
string a,b,c,d;
int n;
int main()
{
    cin>>a>>b;
    cin>>n;
    cout<<a<<" "<<b<<endl;
    while(n--)
    {
        cin>>c>>d;
        if(a==c) a=d;
        else b=d;
        cout<<a<<" "<<b<<endl;
    }
}

B. Sherlock and his girlfriend

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

Sherlock has a new girlfriend (so unlike him!). Valentine’s day is coming and he wants to gift her some jewelry.

He bought n pieces of jewelry. The i-th piece has price equal to i + 1, that is, the prices of the jewelry are 2, 3, 4, … n + 1.

Watson gave Sherlock a challenge to color these jewelry pieces such that two pieces don’t have the same color if the price of one piece is a prime divisor of the price of the other piece. Also, Watson asked him to minimize the number of different colors used.

Help Sherlock complete this trivial task.

Input

The only line contains single integer n (1 ≤ n ≤ 100000) — the number of jewelry pieces.

Output

The first line of output should contain a single integer k, the minimum number of colors that can be used to color the pieces of jewelry with the given constraints.

The next line should consist of n space-separated integers (between 1 and k) that specify the color of each piece in the order of increasing price.

If there are multiple ways to color the pieces using k colors, you can output any of them.

Examples

Input
3

Output
2
1 1 2

Input
4

Output
2
2 1 1 2

Note

In the first input, the colors for first, second and third pieces of jewelry having respective prices 2, 3 and 4 are 1, 1 and 2 respectively.

In this case, as 2 is a prime divisor of 4, colors of jewelry having prices 2 and 4 must be distinct.
题意:将2–n+1的数进行染色,要求每个数与它的质因子不能同色,
问最少需要的颜色和任意一种方案。
题解:素数没有质因子,所以将 所有素数染成1,其他染成2即可。注意n<3的情况。
代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e7+10;
string a,b,c,d;
int n;
int vis[N];
int ans[N];
void solo()
{
    memset(ans,0,sizeof(ans));
    memset(vis,0,sizeof(vis));
    for(int i=2;i<=100000;i++)
    {
        if(!vis[i])
        {
            ans[i]=1;
            for(int j=2;j*i<=100000;j++)
            {
                vis[i*j]=1;
            }
        }
    }

}
int main()
{
    cin>>n;
    if(n<3)
    {
        cout<<1<<endl;
        for(int i=1;i<=n;i++)
            cout<<1<<" ";
        return 0;
    }
    else
    {
        solo();
        cout<<2<<endl;
        for(int i=2;i<=n+1;i++)
        {
            if(ans[i]==1) cout<<1<<" ";
            else cout<<2<<" ";
        }

    }
}
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
### C/C++中包含`icm20608.h`头文件时出现报错的解决方案 在C/C++项目中,当尝试包含`icm20608.h`头文件时遇到报错问题,通常可能涉及以下几个方面的原因及解决方法: #### 1. 头文件路径问题 如果编译器无法找到`icm20608.h`头文件,则会报错。这种情况下需要确保头文件的路径已被正确配置。可以通过以下方式解决: - 确保`icm20608.h`文件位于当前源文件所在目录下,或者位于标准头文件搜索路径中。 - 如果头文件位于非标准路径下,需使用`-I`选项指定额外的头文件搜索路径。例如,假设`icm20608.h`位于`/usr/local/include/icm20608/`目录下,则可以在编译命令中添加`-I/usr/local/include/icm20608/`[^1]。 ```bash gcc -o main main.c -I/usr/local/include/icm20608/ ``` #### 2. 头文件内容错误或不完整 如果`icm20608.h`本身存在语法错误或定义不完整,也可能导致编译失败。此时应检查头文件内容是否符合C/C++规范,并确保所有依赖的宏、类型和函数声明均已正确定义。 #### 3. 编译器版本或标准库兼容性问题 某些头文件可能依赖特定版本的编译器或标准库。如果使用的编译器版本较低或未启用正确的语言标准(如C99、C11或C++11),可能会导致编译失败。可以尝试以下方法: - 指定编译器语言标准。例如,对于GCC编译器,可以使用`-std=c99`或`-std=c++11`选项[^2]。 ```bash gcc -o main main.c -std=c99 ``` - 更新编译器到最新版本,以支持更多现代语言特性。 #### 4. 链接阶段问题 即使头文件包含成功,如果缺少对应的实现文件(如`.c`或`.cpp`文件)或库文件,链接阶段仍可能出现错误。确保以下事项: - 实现文件已正确编译并链接到最终目标文件中。 - 如果`icm20608.h`依赖外部库,需确保这些库已正确安装并在链接时指定。例如,假设`icm20608`依赖于`libi2c`库,则需要在链接时添加`-li2c`选项[^3]。 ```bash gcc -o main main.c -I/usr/local/include/icm20608/ -li2c ``` #### 示例代码 以下是一个简单的示例,展示如何正确包含和使用`icm20608.h`头文件: ```c #include "icm20608.h" int main() { // 初始化ICM20608传感器 if (icm20608_init() != 0) { printf("Failed to initialize ICM20608\n"); return -1; } // 读取加速度数据 icm20608_data_t data; icm20608_read_accel(&data); printf("Acceleration: X=%f, Y=%f, Z=%f\n", data.ax, data.ay, data.az); return 0; } ``` ### 注意事项 - 确保`icm20608.h`及其相关实现文件来自可信来源,并与硬件设备匹配。 - 如果使用的是第三方库,参考其官方文档以获取更详细的配置说明。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值