A - 最小的二进制数
题目
String can be called correct if it consists of characters "0" and "1" and there are no redundant leading zeroes. Here are some examples: "0", "10", "1001".
You are given a correct string s.
You can perform two different operations on this string:
- swap any pair of adjacent characters (for example, "101" "110");
- replace "11" with "1" (for example, "110" "10").
Let val(s) be such a number that s is its binary representation.
Correct string a is less than some other correct string b iff val(a) < val(b).
Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
Input
The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s.
The second line contains the string s consisting of characters "0" and "1". It is guaranteed that the string s is correct.
Output
Print one string — the minimum correct string that you can obtain from the given one.
Examples
Input4Output
1001
100Input
1Output
1
1
Note
In the first example you can obtain the answer by the following sequence of operations: "1001" "1010" "1100" "100".
In the second example you can't obtain smaller answer no matter what operations you use.
思路
因为‘11’可替换为‘1’,所以,无论有几个‘1’,替换后都只剩一个,而‘0’数目不可变,所以最小的数应是10的‘0’的数目次方;但若以数字形式输出,数量级非常大,用longlong会溢出,因此只能一位一位的输出;
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 int main() 5 { 6 int n,b=0; 7 int a=0; 8 char s[105]; 9 cin>>n; 10 for(int i=0;i<n;i++) 11 { 12 cin>>s[i]; 13 if(s[i]=='0') 14 a++; 15 else 16 b++; 17 } 18 if(b==0) 19 cout<<"0"; 20 else 21 { 22 cout<<"1"; 23 for(int i=0;i<a;i++) 24 { 25 cout<<"0"; 26 } 27 } 28 }
B - 线段的包含关系
题目
You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj.
Segment [l1, r1] lies within segment [l2, r2] iff l1 ≥ l2 and r1 ≤ r2.
Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1.
Input
The first line contains one integer n (1 ≤ n ≤ 3·105) — the number of segments.
Each of the next n lines contains two integers li and ri (1 ≤ li ≤ ri ≤ 109) — the i-th segment.
Output
Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1.
Examples
Input5Output
1 10
2 9
3 9
2 3
2 9
2 1Input
3Output
1 5
2 6
6 20
-1 -1
Note
In the first example the following pairs are considered correct:
- (2, 1), (3, 1), (4, 1), (5, 1) — not even touching borders;
- (3, 2), (4, 2), (3, 5), (4, 5) — touch one border;
- (5, 2), (2, 5) — match exactly.
思路
先以左端点升序将各线段排序,若左端点相同则以右端点降序排序,因此,后一个线段的左端点定大于等于前面任意线段,若其右端点小于等于前面任意线段,则符合条件,所以,我们只需找出某线段前面的所有线段的最大右端点与其右端点比较,若符合条件,则输出;
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 struct node{ 5 int a,b,c,l; 6 }s[300005]; 7 bool cmp(node x,node y) 8 { 9 return x.a <y.a||x.a==y.a&&x.b>y.b; 10 } 11 int main() 12 { 13 int n,MAXR=0,MAX; 14 cin>>n; 15 for(int i=1;i<=n;i++) 16 { 17 cin>>s[i].a>>s[i].b; 18 s[i].c=i; 19 } 20 sort(s+1,s+n+1,cmp); 21 for(int i=1;i<=n;i++) 22 { 23 if(s[i].b<=MAXR) 24 { 25 cout<<s[i].c<<" "<<MAX; 26 return 0; 27 } 28 else 29 MAXR=s[i].b,MAX=s[i].c; 30 31 } 32 cout<<"-1"<<" "<<"-1"; 33 34 }
C - 地下城还有劳拉
题目
You might have heard about the next game in Lara Croft series coming out this year. You also might have watched its trailer. Though you definitely missed the main idea about its plot, so let me lift the veil of secrecy.
Lara is going to explore yet another dangerous dungeon. Game designers decided to use good old 2D environment. The dungeon can be represented as a rectangle matrix of n rows and m columns. Cell (x, y) is the cell in the x-th row in the y-th column. Lara can move between the neighbouring by side cells in all four directions.
Moreover, she has even chosen the path for herself to avoid all the traps. She enters the dungeon in cell (1, 1), that is top left corner of the matrix. Then she goes down all the way to cell (n, 1) — the bottom left corner. Then she starts moving in the snake fashion — all the way to the right, one cell up, then to the left to the cell in 2-nd column, one cell up. She moves until she runs out of non-visited cells. n and m given are such that she always end up in cell (1, 2).
Lara has already moved to a neighbouring cell k times. Can you determine her current position?
Input
The only line contains three integers n, m and k (2 ≤ n, m ≤ 109, n is always even, 0 ≤ k < n·m). Note that k doesn't fit into 32-bit integer type!
Output
Print the cell (the row and the column where the cell is situated) where Lara ends up after she moves k times.
Examples
Input4 3 0Output
1 1Input
4 3 11Output
1 2Input
4 3 7Output
3 2
思路
感觉就是一道找规律的题目,比赛的时候没做出来,是因为没有读懂题意,没啥好讲的,分类讨论吧
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int n,m,t; 6 long long k; 7 cin>>n>>m; 8 9 cin>>k; 10 if(k<n) 11 cout<<k+1<<" "<<"1"; 12 else if(k<m+n-1) 13 cout<<n<<" "<<k+2-n; 14 else 15 { 16 int w=(k-m-n+2)/(m-1); 17 int t=(k-m-n+2)%(m-1); 18 int y; 19 if(t==0) 20 { 21 w--; 22 if(w%2==0) 23 y=2; 24 else 25 y=m; 26 cout<<n-w-1<<" "<<y; 27 return 0; 28 } 29 else 30 if(w%2==0) 31 { 32 y=m+1-t; 33 } 34 else 35 { 36 y=t+1; 37 } 38 cout<<n-w-1<<" "<<y; 39 } 40 41 42 }
E - 法法在分配工作
题目
Fafa owns a company that works on huge projects. There are n employees in Fafa's company. Whenever the company has a new project to start working on, Fafa has to divide the tasks of this project among all the employees.
Fafa finds doing this every time is very tiring for him. So, he decided to choose the best l employees in his company as team leaders. Whenever there is a new project, Fafa will divide the tasks among only the team leaders and each team leader will be responsible of some positive number of employees to give them the tasks. To make this process fair for the team leaders, each one of them should be responsible for the same number of employees. Moreover, every employee, who is not a team leader, has to be under the responsibility of exactly one team leader, and no team leader is responsible for another team leader.
Given the number of employees n, find in how many ways Fafa could choose the number of team leaders l in such a way that it is possible to divide employees between them evenly.
Input
The input consists of a single line containing a positive integer n (2 ≤ n ≤ 105) — the number of employees in Fafa's company.
Output
Print a single integer representing the answer to the problem.
Examples
Input2Output
1Input
10Output
3
Note
In the second sample Fafa has 3 ways:
- choose only 1 employee as a team leader with 9 employees under his responsibility.
- choose 2 employees as team leaders with 4 employees under the responsibility of each of them.
- choose 5 employees as team leaders with 1 employee under the responsibility of each of them.
- 思路:签到题;
-
1 #include <iostream> 2 using namespace std; 3 int main(){ 4 int n,c=0; 5 cin>>n; 6 for(int i=1;i<n;i++) 7 { 8 if((n-i)%i==0) 9 c++; 10 } 11 cout<<c; 12 }
F - 法法要穿过大门
- 题目
-
Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens to go from one kingdom to another. Each time a citizen passes through a gate, he has to pay one silver coin.
The world can be represented by the first quadrant of a plane and the wall is built along the identity line (i.e. the line with the equation x = y). Any point below the wall belongs to the first kingdom while any point above the wall belongs to the second kingdom. There is a gate at any integer point on the line (i.e. at points (0, 0), (1, 1), (2, 2), ...). The wall and the gates do not belong to any of the kingdoms.
Fafa is at the gate at position (0, 0) and he wants to walk around in the two kingdoms. He knows the sequence S of moves he will do. This sequence is a string where each character represents a move. The two possible moves Fafa will do are 'U' (move one step up, from (x, y) to (x, y + 1)) and 'R' (move one step right, from (x, y) to (x + 1, y)).
Fafa wants to know the number of silver coins he needs to pay to walk around the two kingdoms following the sequence S. Note that if Fafa visits a gate without moving from one kingdom to another, he pays no silver coins. Also assume that he doesn't pay at the gate at point (0, 0), i. e. he is initially on the side he needs.
InputThe first line of the input contains single integer n (1 ≤ n ≤ 105) — the number of moves in the walking sequence.
The second line contains a string S of length n consisting of the characters 'U' and 'R' describing the required moves. Fafa will follow the sequence S in order from left to right.
OutputOn a single line, print one integer representing the number of silver coins Fafa needs to pay at the gates to follow the sequence S.
Examples Input1
Output
U0
Input6
Output
RURUUR1
Input7
Output
URRRUUU2
思路
主要是判断fafa穿越了几次边界,设边界上方区域为‘-1’,下方为‘1’,若两次位置乘积为负,则穿过边界;
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int n; 6 int x=0,y=0,c=0,m[100005]; 7 char s[100005]; 8 cin>>n; 9 for(int i=0;i<n;i++) 10 { 11 cin>>s[i]; 12 if(s[i]=='U') 13 y++; 14 else 15 x++; 16 if(x>y) 17 m[i]=1; 18 else if(x<y) 19 m[i]=-1; 20 else m[i]=0; 21 if(i>=2&&m[i]*m[i-2]==-1) 22 { 23 c++; 24 } 25 } 26 cout<<c; 27 }