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

目录
相关文章
|
安全 API 开发者
转账到支付宝账户接口:一次开发,提升打款效率
转账到支付宝账户接口:一次开发,提升打款效率
770 0
|
Ubuntu Linux 数据安全/隐私保护
Win10安装Linux,无需安装虚拟机版
win10,linux,虚拟机,ubuntu
1916 0
Win10安装Linux,无需安装虚拟机版
|
9月前
|
传感器 机器学习/深度学习 人工智能
VR硬件进化史:从“晕3D”到沉浸式未来
VR硬件进化史:从“晕3D”到沉浸式未来
490 4
|
11月前
|
数据采集 JSON 监控
Haskell爬虫:为电商运营抓取京东优惠券的实战经验
Haskell爬虫:为电商运营抓取京东优惠券的实战经验
|
Ubuntu 安全 Linux
Windows——安装Ubuntu 18.04 LTS
Windows——安装Ubuntu 18.04 LTS
460 1
Windows——安装Ubuntu 18.04 LTS
|
机器学习/深度学习
小土堆-pytorch-神经网络-损失函数与反向传播_笔记
在使用损失函数时,关键在于匹配输入和输出形状。例如,在L1Loss中,输入形状中的N代表批量大小。以下是具体示例:对于相同形状的输入和目标张量,L1Loss默认计算差值并求平均;此外,均方误差(MSE)也是常用损失函数。实战中,损失函数用于计算模型输出与真实标签间的差距,并通过反向传播更新模型参数。
325 7
|
存储 前端开发 JavaScript
【Web 前端】JS中的栈和堆是什么?优缺点?
【4月更文挑战第22天】【Web 前端】JS中的栈和堆是什么?优缺点?
|
人工智能 运维 安全
【年终总结系列 2023】成长与收获:回顾过去、展望未来,加油2024!
【1月更文挑战第1天】年关将至,富余的时间也稍显多了些,遂写下此文,好好回顾一下自己这一年的收获,同时也立下2024年的新年flag。
|
缓存 Java Android开发
Android Gradle Plugin 源码解析(上)
一、源码依赖 本文基于: android gradle plugin版本: com.android.tools.build:gradle:2.
2394 0
|
机器学习/深度学习 前端开发 JavaScript
JavaScript在科技领域的应用
在当今科技发展的时代,JavaScript(简称JS)已经成为了一门广泛应用的编程语言。它具有跨平台、灵活性强、易于学习等特点,被广泛应用于网页开发、移动应用、物联网和人工智能等领域。本文将深入探索JavaScript在科技领域的各个应用场景。