CodeForces 344

本文深入探讨了编程领域的核心概念和技术应用,从基础知识到高级实践,涵盖了多种编程语言、开发工具、框架和库,旨在帮助开发者提升技能并解决实际问题。

这次是作为晚训题目做的,代码都挺短,考的是思维,想到了分分钟AC,想不到就依然是想不到……


A - Magnets
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.

Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own.

Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.

Input

The first line of the input contains an integer n (1 ≤ n ≤ 100000) — the number of magnets. Then n lines follow. The i-th line (1 ≤ i ≤ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position.

Output

On the single line of the output print the number of groups of magnets.

Sample Input

Input
6
10
10
10
01
10
10
Output
3
Input
4
01
01
10
10
Output
2

Hint

The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.

The second testcase has two groups, each consisting of two magnets.

这道题纯属水题,意思是说有很多的小磁铁,正负极分别用“+”和“--"表示,磁铁有着”异性相吸,同性相斥“的特点,如果两块小磁铁相互吸引,那么就可以将其看成一块大磁铁,现在给出所有小磁铁的正负极朝向,问最终会形成多少块大磁铁。

只需要在输入的同时判断是否与上一块磁铁的朝向相同,如果相同就不作处理继续输入,如果不同择计数器加1。很水啦,直接上代码把……

/*
Author: ZXPxx
Memory: 0 KB		Time: 62 MS
Language: GNU G++ 4.9.2		Result: Accepted
*/

#include <cstdio>
int main()
{
    int m,ans=1;
    bool f=0;
    char s[2][4];
    scanf("%d",&m);
    m--;
    scanf("%s",s[f]);
    while(m--)
    {
        f=!f;
        scanf("%s",s[f]);
        if(s[f][0]==s[!f][1])
            ans++;
    }
    printf("%d\n",ans);
    return 0;
}



B - Simple Molecules
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into one molecule.

A molecule consists of atoms, with some pairs of atoms connected by atomic bonds. Each atom has a valence number — the number of bonds the atom must form with other atoms. An atom can form one or multiple bonds with any other atom, but it cannot form a bond with itself. The number of bonds of an atom in the molecule must be equal to its valence number.

Mike knows valence numbers of the three atoms. Find a molecule that can be built from these atoms according to the stated rules, or determine that it is impossible.

Input

The single line of the input contains three space-separated integers ab and c (1 ≤ a, b, c ≤ 106) — the valence numbers of the given atoms.

Output

If such a molecule can be built, print three space-separated integers — the number of bonds between the 1-st and the 2-nd, the 2-nd and the 3-rd, the 3-rd and the 1-st atoms, correspondingly. If there are multiple solutions, output any of them. If there is no solution, print "Impossible" (without the quotes).

Sample Input

Input
1 1 2
Output
0 1 1
Input
3 4 5
Output
1 3 2
Input
4 1 1
Output
Impossible

Hint

The first sample corresponds to the first figure. There are no bonds between atoms 1 and 2 in this case.

The second sample corresponds to the second figure. There is one or more bonds between each pair of atoms.

The third sample corresponds to the third figure. There is no solution, because an atom cannot form bonds with itself.

The configuration in the fourth figure is impossible as each atom must have at least one atomic bond.


题目意思是说给出三个原子的化合价,问这三个原子能不能结合成一个分子,如果可以则分别输出每两个原子之间的化学键数量,不能则输出”Impossible“。

这道题考的是化学知识了,要是不知道化学中原子守恒定律的话,几乎是很难完成的。只要根据原子守恒定理,直接手算出各个变量之间的等量关系即可。

代码如下:

/*
Author: ZXPxx
Memory: 0 KB		Time: 30 MS
Language: GNU G++ 4.9.2		Result: Accepted
*/

#include <cstdio>
#define max(x,y) (x)>(y)?(x):(y)

int main()
{
    int a,b,c,x,y,z;
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        x=(a+b-c)/2;y=b-x;z=a-x;        //根据原子守恒定理算出
        if(x<0 || y<0 || z<0 || (b+a-c)%2)
        {printf("Impossible\n");continue;}
        printf("%d %d %d\n",x,y,z);

    }
    return 0;
}

/*
Author: ZXPxx
Memory: 8 KB		Time: 30 MS
Language: GNU G++ 4.9.2		Result: Accepted
*/

#include <cstdio>
#define max(x,y) (x)>(y)?(x):(y)

int main()
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    int ab=a+b-c,bc=b+c-a,ac=a+c-b;
    if(ab>=0&&bc>=0&&ac>=0&&ab%2==0&&bc%2==0&&ac%2==0)
        printf("%d %d %d\n",ab/2,bc/2,ac/2);
    else
        printf("Impossible\n");
    return 0;
}


C - Rational Resistance
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.

However, all Mike has is lots of identical resistors with unit resistance R0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements:

  1. one resistor;
  2. an element and one resistor plugged in sequence;
  3. an element and one resistor plugged in parallel.

With the consecutive connection the resistance of the new element equals R = Re + R0. With the parallel connection the resistance of the new element equals . In this case Re equals the resistance of the element being connected.

Mike needs to assemble an element with a resistance equal to the fraction . Determine the smallest possible number of resistors he needs to make such an element.

Input

The single input line contains two space-separated integers a and b (1 ≤ a, b ≤ 1018). It is guaranteed that the fraction  is irreducible. It is guaranteed that a solution always exists.

Output

Print a single number — the answer to the problem.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cincout streams or the %I64dspecifier.

Sample Input

Input
1 1
Output
1
Input
3 2
Output
3
Input
199 200
Output
200

Hint

In the first sample, one resistor is enough.

In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance . We cannot make this element using two resistors.


题目意思是说,有很多个电阻,其阻值都是1 Ω,现在给出两个数a和b,要制作一个阻值为a/b的电阻来,可以用单位阻值的电池通过并联或者串联两种方式制作,我们要做的是计算需要的最少的单位阻值的电阻数量。

刚做完化学又来物理……ORZ~,这让我一个理科学得不好的孩子如何生存啊%>_<%。。。废话不多说,回归正题。

学过物理的都知道,串联电路的总阻值就是各支路电阻值的代数和,即串得越多阻值越大;而并联则相反,并的越多,总阻值越小,且总阻值小于任意支路阻值。显然电阻越并越小,a/b的整数部分可以串联若干1Ω电阻解决.此时,有这样一条重要结论:如果最少用K个电阻构成a/bΩ电阻,那么b/a也需K个(只需改变所有的串并联关系即可).所以此时若b>a,只需交换a,b的值,重复上一步骤.

代码如下:

/*
Author: ZXPxx
Memory: 8 KB		Time: 30 MS
Language: GNU G++ 4.9.2		Result: Accepted
*/

#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int maxn=200000+10;
#define max(x,y) (x)>(y)?(x):(y)

char s[maxn];
__int64 a,b;

int main() {
    while(~scanf("%I64d%I64d",&a,&b)) {
        __int64 ans=0;
        while(1) {
            if(a<b) {
                swap(a,b);
            }
            //printf("%d^%d^^%d\n",a,b,ans);
            ans+=(a/b);
            if(a%b==0)
                break;
            a-=b*(a/b);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

D - Alternating Current
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Mad scientist Mike has just finished constructing a new device to search for extraterrestrial intelligence! He was in such a hurry to launch it for the first time that he plugged in the power wires without giving it a proper glance and started experimenting right away. After a while Mike observed that the wires ended up entangled and now have to be untangled again.

The device is powered by two wires "plus" and "minus". The wires run along the floor from the wall (on the left) to the device (on the right). Both the wall and the device have two contacts in them on the same level, into which the wires are plugged in some order. The wires are considered entangled if there are one or more places where one wire runs above the other one. For example, the picture below has four such places (top view):

Mike knows the sequence in which the wires run above each other. Mike also noticed that on the left side, the "plus" wire is always plugged into the top contact (as seen on the picture). He would like to untangle the wires without unplugging them and without moving the device. Determine if it is possible to do that. A wire can be freely moved and stretched on the floor, but cannot be cut.

To understand the problem better please read the notes to the test samples.

Input

The single line of the input contains a sequence of characters "+" and "-" of length n (1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the character "-" otherwise.

Output

Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled.

Sample Input

Input
-++-
Output
Yes
Input
+-
Output
No
Input
++
Output
Yes
Input
-
Output
No

Hint

The first testcase corresponds to the picture in the statement. To untangle the wires, one can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and then draw it under the "minus" wire, eliminating also the remaining two crosses.

In the second testcase the "plus" wire makes one full revolution around the "minus" wire. Thus the wires cannot be untangled:

In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence. The wires can be untangled by lifting "plus" and moving it higher:

In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot be untangled without moving the device itself:


题目意思是说有两根无限长的电线缠在一起,中间有很多交叉部分,图中给出了一部分,而且两端上面都是正极,下面始终都是负极,输入一连串字符串,分别表示他们交叉部分是正极在上面还是负极在上面,然后是从第一个交叉部分开始分开电线。显然如果是连续两个为同极的话,则对于有没有交叉都没有影响,因为思考就能够发现,如果相同的话,可以将这交叉的部分拉开就会形成一个负极与正极交叉的部分,如果还是相同,一样可以将电线拉开,所以用一个栈来维护结果,如果遇到不同的交叉部分则压栈,如果遇到了与栈顶相同的交叉部分,则弹出栈顶元素。


/*
Author: ZXPxx
Memory: 608 KB		Time: 30 MS
Language: GNU G++ 4.9.2		Result: Accepted
*/

#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int maxn=200000+10;
#define max(x,y) (x)>(y)?(x):(y)
stack<int >q;
char s[maxn];

int main() {
    scanf("%s",s);
    int n=strlen(s);
    //printf("%s %d\n",s,n);
    if(n%2) {
        printf("No\n");
        return 0;
    }
    for(int i=0; i<n; i++) {
        if(q.empty()) {
            q.push(s[i]);
        } else {
            if(q.top()==s[i])
                q.pop();
                else
                    q.push(s[i]);
        }
    }
    if(q.empty())
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

E题还没做出来,待更新……


### Codeforces 平台概述 Codeforces 是一个广受认可的在线编程竞赛平台,专注于算法竞技与程序设计能力提升[^1]。该平台由俄罗斯萨拉托夫州立大学的 Mike Mirzayanov 创建,自 2010 年上线以来已成为全球最具影响力的编程竞赛网站之一。 ### 编程竞赛机制 平台上定期举行名为 “Codeforces Round” 的定时竞赛,通常每两周一次,每次持续两小时左右。比赛采用分等级制度(Div. 1 和 Div. 2),根据参赛者当前 Rating 进行划分,确保公平竞争环境[^7]。近年来也推出了更灵活的比赛形式,如 Educational Rounds、Global Rounds 及 Team Contests,进一步丰富了赛事生态[^8]。 竞赛题目一般设置为 5 到 7 道不等,按难度递增标记为 A 至 G 级别。其中 A 题为基础模拟或贪心类问题,适合初学者;B/C 类常考察构造逻辑或多步推理;D/E 题则深入图论、动态规划、数论等领域;F/G 多用于高级技巧如复杂数据结构组合、概率期望推导等[^9]。 ```cpp // 示例:简单实现快速幂取模——常见于处理大指数运算场景 long long mod_pow(long long base, long long exp, long long mod) { long long result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % mod; base = (base * base) % mod; exp >>= 1; } return result; } ``` ### 在线判题系统运作方式 提交代码后,系统会自动运行预设测试用例并返回反馈状态,包括 Accepted (AC)、Wrong Answer (WA)、Time Limit Exceeded (TLE)、Memory Limit Exceeded (MLE) 等结果码[^10]。评测基于 GNU C++ 编译器标准(常用 g++-x.x)、内存限制(通常 256MB)和时间约束(多数单测 ≤2 秒)。选手需注意输入输出效率优化,推荐使用 `scanf/printf` 或关闭同步流 (`ios::sync_with_stdio(false);`) 提升性能[^11]。 ### 算法题目特点与解题策略 Codeforces 的题目强调思维训练而非工程实践,典型考点覆盖: - **基础算法**:二分查找、前缀和、差分数组 - **经典结构**:栈、队列、优先队列、哈希表 - **高阶主题**:树状数组(BIT)、线段树、LCA 查询、网络流 部分难题融合多个知识点,例如 [Problem 1601F - Two Sorts] 要求结合排序理论与 ST 表技术,在离散化后的序列上维护区间最值映射关系,并考虑偏移量加减对位置索引的影响[^3]。 这类综合型挑战推动参与者掌握跨领域建模技能,同时也促进 STL 容器熟练运用与底层原理理解。 ### 社区影响与发展现状 截至近年统计数据显示,Codeforces 注册用户已突破百万级,来自超过 200 个国家和地区。其举办的 ICPC 训练营合作项目、Google Kick Start 前哨赛联动等活动显著提升了国际间青少年计算机教育互动频率[^12]。知名高校如 MIT、Stanford 将其作为选拔 ACM 国际队伍的重要参考依据之一。 活跃度方面,Top Rated 用户长期保持高强度刷题节奏,最高 Rating 曾达 4000+(tourist 保持历史纪录多年),反映出顶尖群体的技术沉淀强度[^13]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值