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 nlines 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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
void input()
{
int n, ans = 0, temp = -1, x;
cin >> n;
for (int i = 0; i < n; i++)
{
scanf("%d", &x);
if (x != temp)
{
temp = x;
ans++;
}
}
cout << ans << endl;
}
int main()
{
input();
return 0;
}
给你三个原子,每个原子上带有电子,是否可以将他们相连,电子不能剩余,2个电子可以组成共价键。
思路:
考虑是否可以将第一个和第二个原子链接到第三个,如果不满足,第一个第二个原子相连一条边,在观察,一直进行下去直到满足(如果可以)。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;
void input()
{
long long a, b, c;
long long ans1, ans2, ans3;
cin >> a >> b >> c;
if ((a + b + c) % 2 == 1) //必须是偶数
{
cout << "Impossible" << endl;
}
else
{
if (a + b >= c && c >= abs(a - b) && (a + b) % 2 == c % 2) //能不能连第三个原子?
{
ans1 = (a + b - c) / 2;
ans2 = b - ans1;
ans3 = a - ans1;
cout << ans1 << ' ' << ans2 << ' ' << ans3 << endl;
}
else
{
cout << "Impossible" << endl;
}
}
}
int main()
{
input();
return 0;
}
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阻值的电阻。
思路:
每次只能串联一个1阻值的电阻或则并联1阻值的电阻。逆向思考,如果a/b >= 1; 肯定最后一个电阻是串联上去的,否则并联上去,串连上去的ans+=a/b
#include <iostream>
using namespace std;
long long ans;
long long gcd(long long a, long long b)
{
if (b)
ans += a / b;
return b ? gcd(b, a % b) : a;
}
void input()
{
long long a, b;
cin >> a >> b;
gcd(a, b);
cout << ans << endl;
}
int main()
{
input();
return 0;
}
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 <iostream>
#include <stack>
using namespace std;
stack <char> s;
void input()
{
string str;
cin >> str;
s.push(str[0]);
for (int i = 1; i < str.length(); i++)
{
if (!s.empty())
{
if (str[i] != s.top())
{
s.push(str[i]);
}
else
{
s.pop();
}
}
else
{
s.push(str[i]);
}
}
if (s.empty())
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
}
int main()
{
input();
return 0;
}
Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but ndifferent 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 integershi 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.
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXN 1000000 + 10
#define INF 2000000000
long long H[MAXN];
long long P[MAXN];
int n, m;
long long myabs(long long x)
{
return x > 0 ? x : -x;
}
long long mymax(long long a, long long b)
{
return a > b ? a : b;
}
bool is_true(long long ans)
{
int cur = 1;
long long limits;
for (int i = 1; i <= n; i++)
{
if (myabs(H[i] - P[cur]) > ans)
{
continue;
}
if (H[i] == P[cur])
{
cur++;
}
if (P[cur] < H[i])
{
limits = mymax(H[i] + ans - 2 * myabs(H[i] - P[cur]), H[i] + (ans - (H[i] - P[cur])) / 2);
}
else
{
limits = H[i] + ans;
}
while (P[cur] <= limits && cur <= m)
{
cur++;
}
}
return (cur > m);
}
void input()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
scanf("%I64d", &H[i]);
}
for (int j = 1; j <= m; j++)
{
scanf("%I64d", &P[j]);
}
long long low = -1, high = myabs(H[1] - P[1]) * 2 + myabs(H[1] - P[m]);
long long ans;
while (low + 1 < high)
{
ans = (high + low) / 2;
if (is_true(ans))
{
high = ans;
}
else
{
low = ans;
}
}
cout << high << endl;
}
int main()
{
input();
return 0;
}
Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of threea, b, c the following conditions held:
- a < b < c;
- a divides b, b divides c.
Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has groups of three.
Help Xenia, find the required partition or else say that it doesn't exist.
The first line contains integer n (3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7.
It is guaranteed that n is divisible by 3.
If the required partition exists, print groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them.
If there is no solution, print -1.
6 1 1 1 2 2 2
-1
6 2 2 1 1 4 6
1 2 4 1 2 6
#include <iostream>
#include <cstdio>
using namespace std;
int temp[8];
void input()
{
int x, n;
bool flag = true;
cin >> n;
for (int i = 0; i < n; i++)
{
scanf("%d", &x);
if (x == 1)
{
temp[1]++;
}
else if (x == 2)
{
temp[2]++;
}
else if (x == 3)
{
temp[3]++;
}
else if (x == 4)
{
temp[4]++;
}
else if (x == 6)
{
temp[6]++;
}
else
{
flag = false;
}
}
if (temp[1] != n / 3 || temp[2] + temp[3] != n / 3 || temp[4] + temp[6] != n / 3 || temp[4] > temp[2])
{
flag = false;
}
if (flag)
{
for (int i = 0; i < temp[4]; i++)
{
printf("1 2 4\n");
}
for (int i = 0; i < temp[2] - temp[4]; i++)
{
printf("1 2 6\n");
}
for (int i = 0; i < temp[3]; i++)
{
printf("1 3 6\n");
}
}
else
{
cout << -1 << endl;
}
}
int main()
{
input();
return 0;
}
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during stepti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 ≤ li ≤ ri ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 105; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers ti, li, ri (1 ≤ ti ≤ 109, 1 ≤ li ≤ ri ≤ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3
XXRR
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
#define MAXN 100000 + 10
struct Node
{
int t, l, r;
}temp[MAXN];
void input()
{
int n, m, s, f;
int now = 0, i = 0;
string str = "";
cin >> n >> m >> s >> f;
for (int i = 0; i < m; i++)
{
scanf("%d %d %d", &temp[i].t, &temp[i].l, &temp[i].r);
}
while (s != f)
{
now++;
if (s < f)
{
if (now == temp[i].t)
{
i++;
if ((s + 1 >= temp[i - 1].l && s + 1 <= temp[i - 1].r) || (s >= temp[i - 1].l && s <= temp[i - 1].r))
{
str += 'X';
continue;
}
}
s++;
str += 'R';
}
else
{
if (now == temp[i].t)
{
i++;
if ((s - 1 >= temp[i - 1].l && s - 1 <= temp[i - 1].r) || (s >= temp[i - 1].l && s <= temp[i - 1].r))
{
str += 'X';
continue;
}
}
s--;
str += 'L';
}
}
cout << str << endl;
}
int main()
{
input();
return 0;
}