Codeforces Round #200 (Div. 2) (ABCDE題解)
比賽連結:http://codeforces.com/contest/344
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.
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.
On the single line of the output print the number of groups of magnets.
6
10
10
10
01
10
10
3
4
01
01
10
10
2
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.
題目大意:其實就是求連續的01和10組數的和
題目分析:小模擬
#include <cstdio>
int const MAX = 1e5 + 5;
int a[MAX];
int main()
{
int n, ans = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
ans += (a[i] == a[i - 1] ? 0 : 1);
}
printf("%d\n", ans);
}
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.
The single line of the input contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 106) — the valence numbers of the given atoms.
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).
1 1 2
0 1 1
3 4 5
1 3 2
4 1 1
Impossible
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.
題目大意:三個點,給出每個點的度,要求不能有自環,且三點連通,問1-2,2-3,3-1之間各有幾條邊
題目分析:首先一條邊會對總度數增加2,所以a + b+ c必是偶數,設答案是x,y,z,則有
a = x + z
b = x + y
c = y + z
解得
x = (a + b - c) / 2
y = (b + c - a) / 2
z = (a + c - b) / 2
又x,y,z大於等於0且是整數得到:
x = (a + b - c) / 2
y = b - x
z = a - x
#include <cstdio>
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if((a + b + c) & 1)
{
printf("Impossible\n");
return 0;
}
int t1 = a + b - c;
int t2 = b + c - a;
int t3 = a + c - b;
if((t1 != 0 && t1 % 2) || t1 < 0)
{
printf("Impossible\n");
return 0;
}
if((t2 != 0 && t2 % 2) || t2 < 0)
{
printf("Impossible\n");
return 0;
}
if((t3 != 0 && t3 % 2) || t3 < 0)
{
printf("Impossible\n");
return 0;
}
printf("%d %d %d\n", t1 / 2, b - t1 / 2, a - t1 / 2);
}
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:
- one resistor;
- an element and one resistor plugged in sequence;
- 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.
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.
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 cin, cout streams or the %I64d specifier.
1 1
1
3 2
3
199 200
200
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,如果並聯一個1則變成(a + b) / b,如果串連一個1則變成b / (a + b),可見一個數如果大於1則不斷減,減到小於1,相當於直接除,然後再取倒數一直到分子為0
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
int main()
{
ll a, b, ans = 0;
scanf("%I64d %I64d", &a, &b);
if(a >= b)
ans += a / b;
a = a % b;
while(a)
{
swap(a, b);
ans += a / b;
a = a % b;
}
printf("%I64d\n", ans);
}
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.
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.
Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the quotes) if the wires cannot be untangled.
-++-
Yes
+-
No
++
Yes
-
No
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:
題目分析:棧模擬
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
int const MAX = 1e5 + 5;
char s[MAX];
stack <char> st;
int main()
{
scanf("%s", s);
int len = strlen(s);
st.push('#');
for(int i = 0; i < len; i++)
{
if(s[i] == st.top())
st.pop();
else
st.push(s[i]);
}
if((int)st.size() == 1)
printf("Yes\n");
else
printf("No\n");
}
Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read data in parallel.
When viewed from the side, Mike's hard drive is an endless array of tracks. The tracks of the array are numbered from left to right with integers, starting with 1. In the initial state the i-th reading head is above the track number hi. For each of the reading heads, the hard drive's firmware can move the head exactly one track to the right or to the left, or leave it on the current track. During the operation each head's movement does not affect the movement of the other heads: the heads can change their relative order; there can be multiple reading heads above any of the tracks. A track is considered read if at least one head has visited this track. In particular, all of the tracks numbered h1, h2, ..., hn have been read at the beginning of the operation.
Mike needs to read the data on m distinct tracks with numbers p1, p2, ..., pm. Determine the minimum time the hard drive firmware needs to move the heads and read all the given tracks. Note that an arbitrary number of other tracks can also be read.
The first line of the input contains two space-separated integers n, m (1 ≤ n, m ≤ 105) — the number of disk heads and the number of tracks to read, accordingly. The second line contains n distinct integers hi in ascending order (1 ≤ hi ≤ 1010, hi < hi + 1) — the initial positions of the heads. The third line contains m distinct integers pi in ascending order (1 ≤ pi ≤ 1010, pi < pi + 1) - the numbers of tracks to read.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.
Print a single number — the minimum time required, in seconds, to read all the needed tracks.
3 4
2 5 6
1 3 6 8
2
3 3
1 2 3
1 2 3
0
1 2
165
142 200
81
The first test coincides with the figure. In this case the given tracks can be read in 2 seconds in the following way:
- during the first second move the 1-st head to the left and let it stay there;
- move the second head to the left twice;
- move the third head to the right twice (note that the 6-th track has already been read at the beginning).
One cannot read the tracks in 1 second as the 3-rd head is at distance 2 from the 8-th track.
題目大意:n個磁頭,m個目標位置,每一次操作每個磁頭都可以往左或往右走一格,現在問最少要操作幾次可以訪問到所有的目標位置
題目分析:典型的二分題,二分這個時間,開始上界弄小wa了,來回一趟最多1e11即可,每次判斷最左邊的磁頭記h0和最左邊的目標位置記為p0,如果h0小於p0則直接往右走到最遠,否則要判斷它先向左再向右移的遠還是先向右再向左移的遠,取最大為當前最遠位置,經過的目標點做標記,一旦最後一個標記點被標記則表示當前二分的時間點滿足條件,繼續搜尋取最小時間
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
ll const INF = 1e11 + 5;
ll h[MAX], p[MAX];
bool vis[MAX];
int n, m;
bool ok(ll x)
{
memset(vis, false, sizeof(vis));
int cur = 0;
ll dis = 0;
for(int i = 0; i < n; i++)
{
if(h[i] <= p[cur])
dis = h[i] + x;
else
{
if(x < (h[i] - p[cur]))
return false;
dis = max(p[cur] + x - (h[i] - p[cur]), h[i] + (x - (h[i] - p[cur])) / 2);
}
int idx;
for(int j = cur; j < m; j++)
{
if(p[j] <= dis)
vis[j] = true;
else
{
idx = j;
break;
}
}
if(vis[m - 1])
return true;
cur = idx;
}
return false;
}
int main()
{
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++)
scanf("%I64d", &h[i]);
for(int i = 0; i < m; i++)
scanf("%I64d", &p[i]);
ll l = 0, r = INF, mid, ans;
while(l <= r)
{
mid = (l + r) >> 1;
if(ok(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
printf("%I64d\n", ans);
}
相關文章
- Codeforces Round #186 (Div. 2) (ABCDE題解)
- Codeforces Round #105 (Div. 2) (ABCDE題解)
- Codeforces Round #313 (Div. 2) (ABCDE題解)
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] (ABCDE題解)
- Codeforces Round 976 (Div. 2) 題解
- Codeforces Round 983 (Div. 2) 題解
- Codeforces Round #956 (Div. 2)題解
- Codeforces Round 951 (Div. 2) 題解
- Codeforces Round 965 (Div. 2) 題解
- Codeforces Round #681 (Div. 2)E題解
- Codeforces Round 932 (Div. 2) DE 題解
- Educational Codeforces Round 93 (Rated for Div. 2)題解
- Codeforces Round #665 (Div. 2)A-C題解
- Codeforces Round #673 (Div. 2)(A-D)題解
- Codeforces Round #439 (Div. 2) A-C題解
- Codeforces Round #Pi (Div. 2) (ABCD題解)
- Codeforces Round 976 (Div. 2) 題解(A-E)
- Codeforces Round 893 (Div. 2)題解記錄
- Codeforces Round 892 (Div. 2)題解記錄
- Educational Codeforces Round 171 (Rated for Div. 2) 題解
- Codeforces Round #537 (Div. 2)解題報告
- Codeforces Round #266 (Div. 2)解題報告
- Codeforces Round #323 (Div. 2) (ABCD題解)
- Codeforces Round #331 (Div. 2) (ABC題解)
- Codeforces Round 899 (Div. 2)題解記錄
- Codeforces Round 979 (Div. 2) (ABCD個人題解)
- Educational Codeforces Round 168 (Rated for Div. 2) 題解
- Codeforces Round 986 (Div. 2)題解記錄(A~D)
- Codeforces Round 987 (Div. 2)題解記錄(A~D)
- Codeforces Round 984 (Div. 3) 題解
- Codeforces Round 962 (Div. 3) 題解
- Codeforces Round 946 (Div. 3) 題解
- Codeforces Round #646 (Div. 2)【C. Game On Leaves 題解】GAM
- Codeforces Round #319 (Div. 2) (ABCE題解)
- Codeforces Round #315 (Div. 2) (ABCD題解)
- Codeforces Round 975 (Div. 2)題解記錄(A,B,E)
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)