codeforces Round #479(Div.3) A-wrong subtraction B two-gram C- less or equal D E F

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≤109, 1k501≤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.


题意:给定一个整数n(2=
解题思路路:签到题。。n能被10整除的话,n=n/10,否则的话,n--;
AC代码:
#include
using namespace std;
int n, k;
int main()
{
	cin >> n >> k;
	while (k--)
	{
		if (n %10==0) n = 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.


题意:输出长度为n的字符串S,输出 出现最多次数的长度为2的子串。
解题思路:直接用遍历就可以,然后记录当前出现次数最多的长度为2的子串

AC代码:


#include
#include
#include
#include
#include
using namespace std;
int n;
char s[105];
int main()
{
	cin >> n;
	cin >> s;
	int ma = 0;
	string ans="";
	for (int i = 0; i < n-1; i++)
	{
		string temp = "";
		temp = temp + s[i];
		temp = temp + s[i + 1];
		int cnt = 0;
		for (int j = 0; j < n - 1; j++)
		{
			if (s[j] == temp[0] && s[j + 1] == temp[1])
			{
				cnt++;
			}
		}
		if (cnt > ma)
		{
			ans = temp;
			ma = cnt;
		}
	}
	cout << ans << endl;
	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⋅105, 0kn0≤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).

题意:给定一数列a[n],求出a[n]中小于等于x的数量正好为K的x值
解题思路:将a[n]进行从小到大的排序,然后a[k]就是个数正好等于k的那个值,如果a[k]与a[k-1]相同的话,说明不存在数量正好等于k,否则的话,a[k]的值就是x的值,这里需要注意的是a[0],因为我们是从a[1]开始,而且a[i]的值>=1,所以,考虑k=0的情况,即所有的a[i]都大于等于x,这时候x可以取的值是1或是不存在,x可以取1的前提是a[1]>1,否则的话,x只能是不存在,所以当K=0是,a[k]即a[0]的值我们设为1。同样的,还要考虑到最大情况,所以a[n+1]=maxn+1;

AC 代码:

#include
#include
using namespace std;
int n, k, x;
const int maxn = 2e5 + 10;
int a[maxn];
int main()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	a[0] = 1;
	a[n + 1] = 1e9 + 1;
	sort(a + 1, a + n + 1);
	if (a[k] == a[k+1])
	{
		cout << "-1" << endl;
		return 0;
	}
	else
	{
		cout << a[k] << endl;
		return 0;
	}
}
D. Divide by three, multiply by two
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n1n−1operations of the two kinds:

  • divide the number xx by 33 (xx must be divisible by 33);
  • multiply the number xx by 22.

After each operation, Polycarp writes down the result on the board and replaces xx by the result. So there will be nn numbers on the board after all.

You are given a sequence of length nn — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.

Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.

It is guaranteed that the answer exists.

Input

The first line of the input contatins an integer number nn (2n1002≤n≤100) — the number of the elements in the sequence. The second line of the input contains nn integer numbers a1,a2,,ana1,a2,…,an (1ai310181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Output

Print nn integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.

It is guaranteed that the answer exists.

题意:给定一个数a,并且将n写在黑板上,然后进行除3或乘2 的操作,并且把每一步的操作结果都写在黑板上,现在黑板上的n个数都是由a进行n-1次操作得到的,而且顺序是混乱的,现在要求你还原出正确的顺序。
解题思路:由题意知,我们从第一个数开始a[0],我们找其余的数,a[x]有以下几种情况,1)被a[0]除3或乘2得到的,这时候,就把a[x]插入到a[0]的后面,2)a[x]除3或乘2等于a[0],这时候,我们将a[x]插入到a[0]前面。我们以此原理进行遍历a[i],并且用一vector来存储正确的位置,最后vector中的元素就是正确的顺序。

AC代码:

#include
using namespace std;
int n;
const int maxn = 105;
typedef long long int LL;
vectorb;
LL a[maxn];
LL fa[maxn];
bool f[maxn];
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	memset(f, false, sizeof(f));
	b.push_back(a[0]);
	f[0] = true;
	int t = 1;
	int k = 0;
	while (t < n)
	{
		for (int j = 0; j < b.size(); j++)
		{
			LL x = b[j];
			k = 0;
			for (int i = 1; i < n; i++)
			{
				if (f[i] == false)
				{
					if ((x % 2 == 0 && a[i] == x / 2) || a[i] == x * 3)
					{
						b.insert(b.begin() + j, a[i]);
						f[i] = true;
						t++;
						k++;
					}
					if (a[i] == x * 2 || (x % 3 == 0 && a[i] == x / 3))
					{
						b.insert(b.begin() + j + k + 1, a[i]);
						f[i] = true;
						t++;
					}
				}
			}
		}
	}
	for (int i = 0; i < b.size(); i++)
	{
		cout << b[i] << " ";
	}
	cout << 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⋅105, 0m21050≤m≤2⋅105) — number of vertices and edges.

The following mm lines contains edges: edge ii is given as a pair of vertices vivi, uiui (1vi,uin1≤vi,ui≤n, uiviui≠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.

题意:给定n个顶点和m条边,求循环图的个数,这里的循环图要求不能存在多余的边和顶点,即只能形成一个环,不存在多余的任何边或点。
解题思路:由题意,循环图的判定方法,就是对于任何一个顶点,他的度都是2,所以我们直接进行遍历,这里我用vector<>来存储临界点,只要vector.size()不是2,就不符合循环群的要求。这里我用的是bfs进行点的遍历,好像dfs更方便一点。都差不多啦。

AC代码:
#include
using namespace std;
const int maxn = 2e5 + 100;
int n, m;
bool flag[maxn];
vectorvec[maxn];
queueQ;
bool judge(int x)
{
	bool ff = true;
	Q.push(x);
	flag[x] = true;
	while (!Q.empty())
	{
		int now = Q.front();
		Q.pop();
		if (vec[now].size() != 2) ff= false;
		for (int i = 0; i < vec[now].size(); i++)
		{
			if (flag[vec[now][i]]) continue;
			if (!flag[vec[now][i]])
			{
				Q.push(vec[now][i]);
				flag[vec[now][i]] = true;
			}
		}
	}
	return ff;
}
int main()
{
	memset(flag, false, sizeof(flag));
	cin >> n >> m;
	int u, v;
	for (int i = 0; i < m; i++)
	{
		cin >> u >> v;
		vec[u].push_back(v);
		vec[v].push_back(u);
	}
	long long ans = 0;
	for (int i = 1; i <= n; i++)
	{
		if (flag[i]) continue;
		if (!flag[i])
		{
			if (judge(i)) ans++;
		}
	}
	cout << ans << endl;
	return 0;
}
F. Consecutive Subsequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer array of length nn.

You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to [x,x+1,,x+k1][x,x+1,…,x+k−1] for some value xx and length kk.

Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array [5,3,1,2,4][5,3,1,2,4] the following arrays are subsequences: [3][3], [5,3,1,2,4][5,3,1,2,4], [5,1,4][5,1,4], but the array [1,3][1,3] is not.

Input

The first line of the input containing integer number nn (1n21051≤n≤2⋅105) — the length of the array. The second line of the input containing nn integer numbers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the array itself.

Output

On the first line print kk — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.

On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.

题意:给定长度为n的数列,从a[n]中求出最长的差值为1的单调增的等差数列。输出他们的下标,且下标也是递增的。即不能从后往前找。
解题思路:这个我也是参考的别人的,因为之前不是很会map的用法,这里用map可以解决下标的问题,第一个int表示的是a[i],第二个值表示的是从a[i]开始的差值为1的递增数列的长度。时刻记录当前最长的满足要求的数列,并且输出他们的下标。核心思路:mapdp. dp[i]=max(dp[i],dp[i-1]+1).

AC代码:

#include
#include
#include
#include
using namespace std;
const int maxn = 2e5 + 10;
mapdp;
vectorans;
int n;
int a[maxn];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	int cnt = 0;
	int maxnumber = 0;
	for (int i = 1; i <= n; i++)
	{
		dp[a[i]] = max(dp[a[i]], dp[a[i] - 1] + 1);
		if (dp[a[i]] > cnt)
		{
			cnt = dp[a[i]];
			maxnumber = a[i];
		}
	}
	for (int i = n; i >= 1; i--)
	{
		if (a[i] == maxnumber)
		{
			ans.push_back(i);
			maxnumber--;
		}
	}
	cout << cnt << endl;
	for (int i = ans.size()-1; i >=0; i--)
	{
		cout << ans[i] << " ";
	}
	cout << endl;
	return 0;
}

这次的题还挺简单的。。。嗯 接着加油吧

你可能感兴趣的:(Codeforces)