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:
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.
The first line of the input contains two integer numbers nn and kk (2≤n≤1092≤n≤109, 1≤k≤501≤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.
#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;
}
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.
The first line of the input contains integer number nn (2≤n≤1002≤n≤100) — the length of string ss. The second line of the input contains the string ss consisting of nn capital Latin letters.
#include
#include
#include
#include
#include
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. 1≤x≤1091≤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).
The first line of the input contains integer numbers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 0≤k≤n0≤k≤n). The second line of the input contains nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the sequence itself.
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).
#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;
}
}
Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, and then performs with it n−1n−1operations of the two kinds:
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.
The first line of the input contatins an integer number nn (2≤n≤1002≤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 (1≤ai≤3⋅10181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
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.
#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;
}
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:
A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices.
The first line contains two integer numbers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 0≤m≤2⋅1050≤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 (1≤vi,ui≤n1≤vi,ui≤n, ui≠viui≠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.
Print one integer — the number of connected components which are also cycles.
#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;
}
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+k−1][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.
The first line of the input containing integer number nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the array. The second line of the input containing nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the array itself.
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.
#include
#include
这次的题还挺简单的。。。嗯 接着加油吧