Educational Codeforces Round 33

简介: A. Chess For Three time limit per test1 second memory limit per test256 megabytes inputstanda...

A. Chess For Three
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it’s a bit difficult for them because chess is a game for two players, not three.

So they play with each other according to following rules:

  • Alex and Bob play the first game, and Carl is spectating;
  • When the game ends, the one who lost the game becomes the spectator
    in the next game, and the one who was spectating plays against the
    winner.

Alex, Bob and Carl play in such a way that there are no draws.

Today they have played n games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it!

Input
The first line contains one integer n (1 ≤ n ≤ 100) — the number of games Alex, Bob and Carl played.

Then n lines follow, describing the game log. i-th line contains one integer ai (1 ≤ ai ≤ 3) which is equal to 1 if Alex won i-th game, to 2 if Bob won i-th game and 3 if Carl won i-th game.

Output
Print YES if the situation described in the log was possible. Otherwise print NO.

Examples
input
3
1
1
2
output
YES
input
2
1
2
output
NO
Note
In the first example the possible situation is:

  1. Alex wins, Carl starts playing instead of Bob;
  2. Alex wins, Bob replaces Carl;
  3. Bob wins.

The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one.

用一个flag数组来记录是哪两个在比赛,如果胜利的人在flag数组标记的正在比赛的两人输出NO跳出,否则将失败和旁观的人flag数组的状态改变,直到最后都没跳出的输出YES。

#include <cstdio>  
#include <algorithm>  
#include <cmath>  
#include <iostream>  
#include <map>   
#include <queue>  
#include <cstdlib>  
#include <cstring>  
#include <string>  
#include <ctime>  
#include <vector>  

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;

const ll MOD = 1000000009;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-8;

int main(){
    int n;
    cin>>n;
    int s[105];
    int flag[]={0,1,1,0};
    for(int i=0;i<n;i++){
        cin>>s[i];
    }
    for(int i=0;i<n;i++){
        if(!flag[s[i]]){
            cout<<"NO"<<endl;
            return 0;
        }
        else{
            for(int j=1;j<=3;j++){
                if(j!=s[i]){
                    if(flag[j])
                        flag[j]=0;
                    else
                        flag[j]=1;
                }
            }
        }
    }
    cout<<"YES"<<endl;
    return 0;
}

B. Beautiful Divisors
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently Luba learned about a special kind of numbers that she calls beautiful numbers. The number is called beautiful iff its binary representation consists of k + 1 consecutive ones, and then k consecutive zeroes.

Some examples of beautiful numbers:

  • 12(110);
  • 1102(610);
  • 11110002(12010);
  • 1111100002(49610).

More formally, the number is beautiful iff there exists some positive integer k such that the number is equal to (2k1)(2k1).

Luba has got an integer number n, and she wants to find its greatest beautiful divisor. Help her to find it!

Input
The only line of input contains one number n (1 ≤ n ≤105) — the number Luba has got.

Output
Output one number — the greatest beautiful divisor of Luba’s number. It is obvious that the answer always exists.

Examples
input
3
output
1
input
992
output
496

其实在1105范围内满足(2k1)(2k1)式子的只有八个{1,6,28,120,496,2016,8128,32640}直接打表,找n最大的除数输出即可。

#include <cstdio>  
#include <algorithm>  
#include <cmath>  
#include <iostream>  
#include <map>   
#include <queue>  
#include <cstdlib>  
#include <cstring>  
#include <string>  
#include <ctime>  
#include <vector>  

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;

const ll MOD = 1000000009;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-8;

int ans[]={1,6,28,120,496,2016,8128,32640};

int main(){
    int n;
    cin>>n;
    for(int i=7;i>=0;i--){
        if(n%ans[i]==0){
            cout<<ans[i]<<endl;
            return 0;
        }
    }
}

C. Rumor
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vova promised himself that he would never play computer games… But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.

Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.

Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.

The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?

Take a look at the notes if you think you haven’t understood the problem completely.

Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends.

The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor.

Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.

Output
Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.

Examples
input
5 2
2 5 3 4 8
1 4
4 5
output
10
input
10 0
1 2 3 4 5 6 7 8 9 10
output
55
input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
output
15
Note
In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.

In the second example Vova has to bribe everyone.

In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.

裸的并查集题目,直接向花钱少的合并,最后将所有父亲的钱加起来。

#include <cstdio>  
#include <algorithm>  
#include <cmath>  
#include <iostream>  
#include <map>   
#include <queue>  
#include <cstdlib>  
#include <cstring>  
#include <string>  
#include <ctime>  
#include <vector>  

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;

const ll MOD = 1000000009;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-8;

int Father[100005];
int find(int x){
    if(Father[x]!=x)
        Father[x]=find(Father[x]);
    return Father[x];
}
int main(){
    int n,m;
    cin>>n>>m;
    ll c[100005];
    for(int i=1;i<=n;i++){
        Father[i]=i;
        cin>>c[i];
    }
    int a,b;
    for(int i=0;i<m;i++){
        cin>>a>>b;
        a=find(a);
        b=find(b);
        if(c[a]<c[b])
            Father[b]=a;
        else
            Father[a]=b;
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        if(find(i)==i){
            ans+=c[i];
        }
    }
    cout<<ans<<endl;
    return 0;
}

D. Credit Card
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recenlty Luba got a credit card and started to use it. Let’s consider n consecutive days Luba uses the card.

She starts with 0 money on her account.

In the evening of i-th day a transaction ai occurs. If ai > 0, then ai bourles are deposited to Luba’s account. If ai < 0, then ai bourles are withdrawn. And if ai = 0, then the amount of money on Luba’s account is checked.

In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d.

It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1».

Luba must not exceed this limit, and also she wants that every day her account is checked (the days when ai = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her!

Input
The first line contains two integers n, d (1 ≤ n ≤ 105, 1 ≤ d ≤ 109) —the number of days and the money limitation.

The second line contains n integer numbers a1, a2, … an ( - 104 ≤ ai ≤ 104), where ai represents the transaction in i-th day.

Output
Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money.

Examples
input
5 10
-1 5 0 -5 3
output
0
input
3 4
-10 0 20
output
-1
input
5 10
-5 0 10 -11 0
output
2

目录
相关文章
|
Java
Java实现随机生成某个省某个市的身份证号?如何编码?
【10月更文挑战第18天】Java实现随机生成某个省某个市的身份证号?如何编码?
1148 5
|
安全 大数据 虚拟化
随着云计算和大数据技术的发展,Hyper-V在虚拟化领域的地位日益凸显
随着云计算和大数据技术的发展,Hyper-V在虚拟化领域的地位日益凸显。作为Windows Server的核心组件,Hyper-V具备卓越的技术性能,支持高可用性、动态迁移等功能,确保虚拟机稳定高效运行。它与Windows深度集成,管理便捷,支持远程管理和自动化部署,降低管理成本。内置防火墙、RBAC等安全功能,提供全方位安全保障。作为内置组件,Hyper-V无需额外购买软件,降低成本。其广泛的生态系统支持和持续增长的市场需求,使其成为企业虚拟化解决方案的首选。
|
测试技术 API
在性能测试中,怎样设置合理的迭代次数?
在性能测试中,迭代次数的合理设置至关重要,它直接影响到测试结果的准确性和可靠性。
869 57
|
人工智能
《AI助力生物学:基因编辑与蛋白质结构解析的加速引擎》
在生物学研究中,AI正发挥重要作用,特别是在基因编辑和蛋白质结构解析方面。AI通过设计新型基因编辑工具(如OpenCRISPR™)、提高编辑效率与精准度(如EVOLVEpro),以及优化整个编辑过程,显著加速了基因编辑的研究进展。在蛋白质结构解析领域,AI技术如AlphaFold实现了精准预测蛋白质三维结构,加速了蛋白质设计与改造,并解析蛋白质相互作用网络。这不仅推动了医学和农业领域的发展,也带来了伦理和法律等挑战,需要确保其健康、可持续发展。
593 11
|
中间件 API
Next.js 实战 (八):使用 Lodash 打包构建产生的“坑”?
这篇文章介绍了作者在使用Nextjs15进行项目开发时遇到的部署问题。在部署过程中,作者遇到了打包构建时的一系列报错,报错内容涉及动态代码评估在Edge运行时不被允许等问题。经过一天的尝试和调整,作者最终删除了lodash-es库,并将radash的部分源码复制到本地,解决了打包报错的问题。文章最后提供了项目的线上预览地址,并欢迎读者留言讨论更好的解决方案。
397 0
Next.js 实战 (八):使用 Lodash 打包构建产生的“坑”?
|
JSON 数据可视化 知识图谱
基于百炼 qwen plus 、开源qwen2.5 7B Instruct 建非schema限定的图谱 用于agent tool的图谱形式结构化 文本资料方案
基于百炼 qwen plus 的上市企业ESG图谱构建工作,通过调用阿里云的 OpenAI 服务,从 Excel 文件读取上市公司 ESG 报告数据,逐条处理并生成知识图谱,最终以 YAML 格式输出。该过程包括数据读取、API 调用、结果处理和文件保存等步骤,确保生成的知识图谱全面、动态且结构清晰。此外,还提供了基于 Pyvis 的可视化工具,将生成的图谱以交互式图形展示,便于进一步分析和应用。
1496 3
|
消息中间件 缓存 监控
在PHP中,实现多线程
在PHP中,实现多线程
454 6
|
前端开发 安全 数据库
使用Python开发独立站的全面指南
本文详细介绍了如何使用Python及其Web框架Django和Flask快速搭建功能完善、易于管理的独立站。从Python和Web开发基础讲起,逐步覆盖环境搭建、项目创建、数据库设计、视图与URL路由、模板创建、表单处理、测试调试、部署优化及安全维护等内容,旨在帮助开发者高效构建稳定的Web应用。
576 1
|
编解码 人工智能 算法
国家扶持超高清产业背景下:视频云AIGC的超高清技术实践
本次分享由阿里云视频云高级产品解决方案架构师陈震主讲,聚焦国家扶持超高清产业背景下,视频云AIGC的超高清技术实践。内容涵盖超高清产业发展趋势与挑战、阿里视频云的应对方案及应用案例。通过全链路超高清解决方案,结合AI、云计算等技术,提供从内容生产、传输到播放的完整支持,助力行业应对超高清视频带来的技术与市场挑战。
622 0
|
算法 调度 UED
深入理解操作系统的进程调度机制
本文旨在探讨操作系统中至关重要的组成部分之一——进程调度机制。通过详细解析进程调度的概念、目的、类型以及实现方式,本文为读者提供了一个全面了解操作系统如何高效管理进程资源的视角。此外,文章还简要介绍了几种常见的进程调度算法,并分析了它们的优缺点,旨在帮助读者更好地理解操作系统内部的复杂性及其对系统性能的影响。

热门文章

最新文章