CodeForces 344

来源:互联网 发布:mcm大号双肩包 13寸mac 编辑:程序博客网 时间:2024/06/16 18:10

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


A - Magnets
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 344A

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
6101010011010
Output
3
Input
401011010
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: ZXPxxMemory: 0 KBTime: 62 MSLanguage: GNU G++ 4.9.2Result: 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
Submit Status Practice CodeForces 344B

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: ZXPxxMemory: 0 KBTime: 30 MSLanguage: GNU G++ 4.9.2Result: 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: ZXPxxMemory: 8 KBTime: 30 MSLanguage: GNU G++ 4.9.2Result: 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
Submit Status Practice CodeForces 344C

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: ZXPxxMemory: 8 KBTime: 30 MSLanguage: GNU G++ 4.9.2Result: 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
Submit Status Practice CodeForces 344D

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: ZXPxxMemory: 608 KBTime: 30 MSLanguage: GNU G++ 4.9.2Result: 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题还没做出来,待更新……


0 0
原创粉丝点击