【codeforces 479DIV.3】A B C E题解

A. Wrong Subtraction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little girl Tanya is learning how to decrease a number by one, but she does it wrong with a number consisting of two or more digits. Tanya subtracts one from a number by the following algorithm:

  • if the last digit of the number is non-zero, she decreases the number by one;
  • if the last digit of the number is zero, she divides the number by 10 (i.e. removes the last digit).

You are given an integer number nn. Tanya will subtract one from it kk times. Your task is to print the result after all kk subtractions.

It is guaranteed that the result will be positive integer number.

Input

The first line of the input contains two integer numbers nn and kk (2n1092≤n≤1091k501≤k≤50) — the number from which Tanya will subtract and the number of subtractions correspondingly.

Output

Print one integer number — the result of the decreasing nn by one kk times.

It is guaranteed that the result will be positive integer number.

Examples
input
Copy
512 4
output
Copy
50
input
Copy
1000000000 9
output
Copy
1
Note

The first example corresponds to the following sequence: 5125115105150512→511→510→51→50

简单的模拟即可,如果n能被10整除,则n除10,否则n-1;

#include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
using namespace std;
int main()
{
	int n,t;
	cin>>n>>t;
	while(t--)
	{
		if(n%10==0)
		{
			n/=10;
		}
		else
		{
			n--;
		}
	}
	cout<<n<<endl;
	return 0;
} 
B. Two-gram
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams.

You are given a string ss consisting of nn capital Latin letters. Your task is to find any two-gram contained in the given string as a substring(i.e. two consecutive characters of the string) maximal number of times. For example, for string ss = "BBAABBBA" the answer is two-gram "BB", which contained in ss three times. In other words, find any most frequent two-gram.

Note that occurrences of the two-gram can overlap with each other.

Input

The first line of the input contains integer number nn (2n1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.

Output

Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string ss as a substring (i.e. two consecutive characters of the string) maximal number of times.

Examples
input
Copy
7
ABACABA
output
Copy
AB
input
Copy
5
ZZZAA
output
Copy
ZZ
Note

In the first example "BA" is also valid answer.

In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times.


直接暴力跑一遍,用map记录两个字符形成的串的个数,找出最大值即可:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std;
char c[105];
map<string,int> p;
int main()
{
		int n;
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>c[i];
		}
		int maxx=0;
		char re[2],ss[2];
		for(int i=0;i<n-1;i++)
		{
			ss[0]=c[i];
			ss[1]=c[i+1];
			p[ss]++;
			if(p[ss]>maxx)
			{
				maxx=p[ss];
				strcpy(re,ss);
			}
		}
		for(int i=0;i<2;i++)
		{
			cout<<re[i];
		}	
	return 0;
 } 

C. Less or Equal
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a sequence of integers of length nn and integer number kk. You should print any integer number xx in the range of [1;109][1;109] (i.e. 1x1091≤x≤109) such that exactly kk elements of given sequence are less than or equal to xx.

Note that the sequence can contain equal elements.

If there is no such xx, print "-1" (without quotes).

Input

The first line of the input contains integer numbers nn and kk (1n21051≤n≤2⋅1050kn0≤k≤n). The second line of the input contains nn integer numbers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the sequence itself.

Output

Print any integer number xx from range [1;109][1;109] such that exactly kk elements of given sequence is less or equal to xx.

If there is no such xx, print "-1" (without quotes).

Examples
input
Copy
7 4
3 7 5 1 10 3 20
output
Copy
6
input
Copy
7 2
3 7 5 1 10 3 20
output
Copy
-1
Note

In the first example 55 is also a valid answer because the elements with indices [1,3,4,6][1,3,4,6] is less than or equal to 55 and obviously less than or equal to 66.

In the second example you cannot choose any number that only 22 elements of the given sequence will be less than or equal to this number because 33 elements of the given sequence will be also less than or equal to this number.


注意几种特殊情况的判断,直接排序即可,判断a[k-1]是否等于a[k],判断k=0和k=n的情况即可

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int a[200005];
int main()
{
	int n,k;
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	if(k==0) 
	{
		if(a[0]<=1) cout<<"-1"<<endl;
		else cout<<"1"<<endl;
	}
	else if(k==n)
	{
		cout<<"1000000000"<<endl;
	}
	else
	{
		if(a[k-1]==a[k]) cout<<"-1"<<endl;
		else 
		{
			cout<<a[k-1]<<endl;
		}
	}
	return 0;
}
E. Cyclic Components
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an undirected graph consisting of nn vertices and mm edges. Your task is to find the number of connected components which are cycles.

Here are some definitions of graph theory.

An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex aa is connected with a vertex bb, a vertex bb is also connected with a vertex aa). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.

Two vertices uu and vv belong to the same connected component if and only if there is at least one path along edges connecting uu and vv.

A connected component is a cycle if and only if its vertices can be reordered in such a way that:

  • the first vertex is connected with the second vertex by an edge,
  • the second vertex is connected with the third vertex by an edge,
  • ...
  • the last vertex is connected with the first vertex by an edge,
  • all the described edges of a cycle are distinct.

A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices.

There are 66 connected components, 22 of them are cycles: [7,10,16][7,10,16] and [5,11,9,15][5,11,9,15].
Input

The first line contains two integer numbers nn and mm (1n21051≤n≤2⋅1050m21050≤m≤2⋅105) — number of vertices and edges.

The following mm lines contains edges: edge ii is given as a pair of vertices viviuiui (1vi,uin1≤vi,ui≤nuiviui≠vi). There is no multiple edges in the given graph, i.e. for each pair (vi,uivi,ui) there no other pairs (vi,uivi,ui) and (ui,viui,vi) in the list of edges.

Output

Print one integer — the number of connected components which are also cycles.

Examples
input
Copy
5 4
1 2
3 4
5 4
3 5
output
Copy
1
input
Copy
17 15
1 8
1 12
5 11
11 9
9 15
15 5
4 13
3 13
4 3
10 16
7 10
16 7
14 3
14 4
17 6
output
Copy
2
Note

In the first example only component [3,4,5][3,4,5] is also a cycle.

The illustration above corresponds to the second example.

利用并查集查找环的个数,不过要注意环是独环,所以每个点的度要为2才行,所以这还是一个坑点。

#include <stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; 
int fa[200005];
int find(int x)
{
	if(fa[x]!=x) fa[x]=find(fa[x]);
	return fa[x];
}
bool comb(int a,int b)
{
	a=find(a);
	b=find(b);
	if(a==b)//如果根相等就是出现了环
	return true;
	else
	{
		fa[a]=b;
		return false;
	}
}
void init(int n)
{
	for(int i=0;i<n;i++)
	fa[i]=i;
}
int du[200005];
int u[200005],v[200005];
int main()
{
	int n,k;
	while(cin>>n>>k)
	{
		memset(du,0,sizeof(du));
		init(n);
		int sum=0;
		for(int i=0;i<k;i++)
		{
			cin>>u[i]>>v[i];
			du[u[i]]++;
			du[v[i]]++;
		}
		for(int i=0;i<k;i++){
			if(du[u[i]]==2&&du[v[i]]==2&&(comb(u[i],v[i])))
			{
				sum++;
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/duanghaha/article/details/80228472