A. Incorrect Subtraction
Simulate the process of subtracting 1 from the last digit of a number for k times. If the last digit is 0, remove it instead.
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e3 + 10, mod = 1e9 +7;
typedef pair<int,int> PII;
int n,m,t,k;
vector<int> a, b;
void solve()
{
string s;
cin >> s >> m;
while(m--){
if(s.back() != '0'){
s[s.size() - 1] --;
}
else
s.erase(s.size() - 1, 1);
}
cout << s << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int test_cases = 1;
while(test_cases--)
solve();
return 0;
}
B. Two-gram
Use brute force to find the two-character sequence that appears most frequently.
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e3 + 10, mod = 1e9 +7;
typedef pair<int,int> PII;
int n,m,t,k;
vector<int> a, b;
void solve()
{
cin >> n;
string s;
cin >> s;
string ans ;
int maxx = 0;
for(int i = 0;i < s.size() - 1; i++){
string s1 = s.substr(i,2);
int x = 0;
for(int j = 0; j <s.size() - 1; j ++){
if(s.substr(j, 2) == s1)
x ++;
}
if(x > maxx){
maxx = x;
ans = s1;
}
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int test_cases = 1;
while(test_cases--)
solve();
return 0;
}
C. Less or Equal
Handle special cases when m equals n or m is zero. For other cases, sort and check the value at position m.
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e3 + 10, mod = 1e9 +7;
typedef pair<int,int> PII;
int n,m,t,k;
vector<int> a, b;
void solve()
{
cin >> n >> m;
for(int i = 0;i < n; i++){
int x;
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
if(m == n){
cout << a.back() << endl;
return ;
}
if(m == 0){
if(a.front() != 1)
cout << 1 << endl;
else
cout << -1 << endl;
return ;
}
if(a[m] == a[m - 1]){
cout << -1 << endl;
}
else
cout << a[m - 1] << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int test_cases = 1;
while(test_cases--)
solve();
return 0;
}
D. Divide by three, multiply by two
Build a graph where nodes are connected based on division by three or multiplication by two. Use DFS to find a path that covers all nodes.
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 110;
typedef pair<int,int> PII;
bool vis[N];
int arr[N];
vector<int> v[N];
int a[N],ans[N];
void dfs(int val,int x){
ans[x] = a[val];
if(x >= n){
for(int i = 1;i <= n; i++) {
cout << ans[i] << ' ';
}
return ;
}
for(auto i : v[val])
dfs(i, x + 1);
return ;
}
void solve()
{
cin >> n;
for(int i = 0; i < n ;i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
for(int j = 0;j < n;j ++){
if(i == j)
continue;
if(a[i] == a[j] * 3 || a[i] * 2 == a[j]){
v[i].push_back(j);
}
}
}
for(int i = 0;i < n; i++){
dfs(i, 1);
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int test_cases = 1;
while(test_cases--)
{
solve();
}
return 0;
}
E. Cyclic Components
Track the in-degree of each node and use a union-find structure to detect cycles.
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e5 + 10;
typedef pair<int,int> PII;
bool vis[N];
int arr[N];
int ans;
vector<PII> edge;
int sum[N];
int fa[N];
int find(int x){
if(fa[x] == x)
return x;
return fa[x] = find(fa[x]);
}
void add(int x, int y){
int dx = find(x);
int dy = find(y);
if(dx != dy)
fa[dx] = dy;
else
ans ++;
}
void solve()
{
cin >> n >> m;
for(int i = 0; i < m; i++){
int x, y;
cin >> x >> y;
edge.push_back({x, y});
sum[x] ++;
sum[y] ++;
}
for(int i = 1; i <= n; i++)
fa[i] = i;
for(int i = 0;i < m; i++){
if(sum[edge[i].first] == 2 && sum[edge[i].second] == 2)
add(edge[i].first, edge[i].second);
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int test_cases = 1;
while(test_cases--)
{
solve();
}
return 0;
}
F. Consecutive Subsequence
Use a map for dynamic programmming to track longest increasing subsequence and reconstruct it from the end.
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e5 + 10;
typedef pair<int,int> PII;
bool vis[N];
int arr[N];
int ans;
int dp[N];
void solve()
{
cin >> n;
int maxx = 0, maxid;
for(int i = 1; i <= n; i++){
cin >> arr[i];
dp[arr[i]] = max(dp[arr[i]], dp[arr[i] - 1] + 1);
if(dp[arr[i]] > maxx) {
maxx = dp[arr[i]];
maxid = arr[i];
}
}
cout << maxx << endl;
vector<int> res;
for(int i = n; i > 0; i--){
if(arr[i] == maxid){
res.push_back(i);
maxid--;
}
}
for(int i = res.size() - 1; i >= 0; i--)
cout << res[i] << ' ';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int test_cases = 1;
while(test_cases--)
solve();
return 0;
}