凉心的比赛(一)题解

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
Input
4
1001
Output
100
Input
1
1
Output
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.
理解:

一个只有“0”,“1”组成,且没有多余的前导零组成的字符串可以交换任何一对相邻的字符,也可以将“11”替换为“1”,任意多次操作后,输出能转换为最小的二进制的字符串。
做法:将出现的“1”转换成一个"1",且放在最前面。即遍历时将遇到的第一个为“1”的字符记录下来,其他的字符“1”皆不记录,同时将出现的“0”记录在后面。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a[101],b[101];
int main()
{
	int n;
	cin>>n;
	cin>>a;
	int flag=0,k=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]=='1'&&flag==0)//如果有‘1’,只存一个‘1’
		{
			b[k++]='1';
			flag=1;
		}
		if(a[i]=='0')
		{
			b[k++]='0';
		}
	}
	cout<<b;
	return 0;
} 

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
Input
5
1 10
2 9
3 9
2 3
2 9
Output
2 1
Input
3
1 5
2 6
6 20
Output
-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 -1”。
将线段按左端的数从小到大进行排序,如果左端的数一样大,就按照右端的数从大到小进行排序,保证越靠前的包含的范围足够大,且左端够小右端够大。然后寻找上一个线段是否包含这一个线段。有的人用两重循环,即看前一个线段是否包含后面的线段,如果不包含这一个线段就继续往下找,其实不用,如果下一个不被包含于这一个线段的话,那么下一个线段右端肯定比上一个大,就可以直接比较两个相邻的线段是否有包含关系。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct com
{
	int a,b,c;
}Com[300010];//设置三个变量,即记录左端的数,右端的数,下标

bool cmp(com a,com b)
{
	if(a.a!=b.a)
		return a.a<b.a;
	return a.b>b.b;
}

int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int t;
		cin>>Com[i].a>>Com[i].b;
		Com[i].c=i;
	}
	sort(Com+1,Com+n+1,cmp);
	int k=0;
	for(int i=2;i<=n;i++)
	{
		if(Com[i].b<=Com[i-1].b)
		{
			printf("%d %d",Com[i].c,Com[i-1].c);
			k=1;
			break;
		}
	}
	if(k==0)
	printf("-1 -1");
	return 0;
} 

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
Input
4 3 0
Output
1 1
Input
4 3 11
Output
1 2
Input
4 3 7
Output
3 2
Note
Here is her path on matrix 4 by 3:
在这里插入图片描述
理解:

找规律,一个n*m的方格,起点在第一个格,坐标即为(1,1),然后步骤即为先往下走走到头,然后往右走走到头,然后往上一个格后再往左走第二列再往上一个格后再往右走…按次步骤走k步,看最后落在哪个格就输出哪个格的坐标。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
 long long n,m,k;
 cin>>n>>m>>k;
 if(k<n)
  printf("%I64d %I64d",k+1,1LL);
 else
 {
  long long a=k-n+1,b=m-1,x=a%b,y=a/b;
  if(x==0)
  {
   if(y%2==0)
    printf("%I64d %I64d",n+1-y,2LL);
   else 
    printf("%I64d %I64d",n+1-y,m);
  }
  if(x!=0)
  {
   if(y%2==0)
    printf("%I64d %I64d",n-y,1+x);
   else 
    printf("%I64d %I64d",n-y,m-x+1);
  }
 }
 return 0;
} 

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
Input
2
Output
1
Input
10
Output
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.
理解:

在n名员工中选出来几个组长让他们的员工一样多,且没有员工没有组长。
即找出除它本身以外的其他因数的个数。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int count=0;
	for(int i=2;i<n;i++)
	{
		if(n%i==0)
			count++;
	}
	cout<<count+1;
	return 0;
} 

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.
Input
The 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.
Output
On 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
Input
1
U
Output
0
Input
6
RURUUR
Output
1
Input
7
URRRUUU
Output
2
Note
The figure below describes the third sample. The red arrows represent the sequence of moves Fafa will follow. The green gates represent the gates at which Fafa have to pay silver coins.在这里插入图片描述
理解:

一堵墙是沿着恒等线(即方程x = y的那条线)建造的,中间有一些门,一个国王通过从(0,0)开始走动,他知道他要做的动作的顺序。这个序列是一个字符串,每个字符表示一个移动。(U即往上走一步,R即往上走一步),要穿过那些门需要支付一些银币,穿过一次必须支付一个银币,记录一共支付多少。
用两个数组记录走过的横坐标和纵坐标,如果横坐标和纵坐标相同,那么就将要穿过那个门,如果下一步在那个门的另一端,就证明要穿过那个门。即查看在穿过门的那个点上的上一步和下一步是否在同一条直线上。

#include<iostream>
#include<cstdio>
using namespace std;
int c[100010],b[100010];
int main()
{
	int n;
	cin>>n;
	int count=0;
	char a[100010];
	cin>>a;
	for(int i=0;i<n;i++)//记录走过的点
	{
		if(a[i]=='U')
		{
			c[i+1]=c[i]+1;
			b[i+1]=b[i];
		}
		else if(a[i]=='R')
		{
			b[i+1]=b[i]+1;
			c[i+1]=c[i];
		} 
	}
	for(int i=2;i<=n-1;i++)
	{
		if(c[i]==b[i])
		{
			if(c[i+1]==c[i-1]||b[i+1]==b[i-1])//查看在穿过门的那个点上的上一步和下一步是否在同一条直线上。
				count++;
		} 
	}
	printf("%d",count);
	return 0;
}

G - 法法非法是朋友

Fifa and Fafa are sharing a flat. Fifa loves video games and wants to download a new soccer game. Unfortunately, Fafa heavily uses the internet which consumes the quota. Fifa can access the internet through his Wi-Fi access point. This access point can be accessed within a range of r meters (this range can be chosen by Fifa) from its position. Fifa must put the access point inside the flat which has a circular shape of radius R. Fifa wants to minimize the area that is not covered by the access point inside the flat without letting Fafa or anyone outside the flat to get access to the internet.
The world is represented as an infinite 2D plane. The flat is centered at (x1, y1) and has radius R and Fafa’s laptop is located at (x2, y2), not necessarily inside the flat. Find the position and the radius chosen by Fifa for his access point which minimizes the uncovered area.
Input
The single line of the input contains 5 space-separated integers R, x1, y1, x2, y2 (1 ≤ R ≤ 105, |x1|, |y1|, |x2|, |y2| ≤ 105).
Output
Print three space-separated numbers xap, yap, r where (xap, yap) is the position which Fifa chose for the access point and r is the radius of its range.Your answer will be considered correct if the radius does not differ from optimal more than 10 - 6 absolutely or relatively, and also the radius you printed can be changed by no more than 10 - 6 (absolutely or relatively) in such a way that all points outside the flat and Fafa’s laptop position are outside circle of the access point range.
Examples
Input
5 3 3 1 1
Output
3.7677669529663684 3.7677669529663684 3.914213562373095
Input
10 5 5 5 15
Output
5.0 5.0 10.0
注释:这道题还没过,先写下思路。
理解:

以(x1,y1)为圆心,R为半径的一个圆,在该圆内取不包括点 (x2,y2)的最大圆。

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
 int r,x1,y1,x2,y2;
 cin>>r>>x1>>y1>>x2>>y2;
 double l=sqrt((x2-x1)*(x2-x1)*1.0+(y2-y1)*(y2-y1)*1.0),x,y,xx,yy;//l表示圆心和法法之间的距离
 if(l>=r)//如果距离大于半径的话,证明法法在公寓外面。则fifa范围为整个公寓。
 printf("%.1f %.1f %.1f",(double)x1,(double)y1,(double)r);
 else
 {
  double k;
  if(x1!=x2)
  {
   k=(y2-y1)*1.0/(x2-x1);//两点之间直线的斜率
   x=sqrt(((l+r)*1.0/2)*((l+r)*1.0/2)/(1+k*k));//根据勾股定理,求出要求的x与x2的差值
   y=fabs(k*x);//求除要求的y与y2的差值
   if(y2<y1)//根据点在圆心不同的区域,求除要求的坐标。
   {
    if(x2<x1)
    {
     xx=x+x2;
     yy=y+y2; 
    }
    else if(x1>x2)
    {
     xx=x2-x;
     yy=y2+y;
    }
   }
   else 
   {
    if(x2<x1)
    {
     xx=x+x2;
     yy=y2-y;
    }
    else if(x2>x1)
    {
     xx=x2-x;
     yy=y2-y;
    }
   }
   printf("%.16f %.16f %.16f",xx,yy,(l+r)*1.0/2);
  }
  else 
  {
   y=(l+r)*1.0/2;
   if(y2>=y1)
   {
    xx=x1;
    yy=y2-y;
   }
   else
   {
    xx=x1;
    yy=y+y2;
   }
   printf("%.16f %.16f %.16f",xx,yy,y);
  }
 }
 return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值