A. Wrong Subtraction
Topic meaning:
Define an operation that lets you simulate
answer:
simulation
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
inline int max(int a, int b){return a > b ? a : b;}
inline int min(int a, int b){return a < b ? a : b;}
inline int abs(int x){return x < 0 ? -x : x;}
inline void swap(int &x, int &y){int tmp = x;x = y;y = tmp;}
inline void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
int n, k;
int num[20], len;
int main()
{
read(n), read(k);
while(n) num[++ len] = n % 10, n /= 10;
int i = 1;
for(;k;-- k)
{
if(num[i]) -- num[i];
else ++ i;
}
for(int j = len;j >= i;-- j) printf("%d", num[j]);
return 0;
}
B. Two-gram
Topic
Find the substring of the string S with the most occurrences in the string S
answer
brute force enumeration, comparison
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
inline int max(int a, int b){return a > b ? a : b;}
inline int min(int a, int b){return a < b ? a : b;}
inline int abs(int x){return x < 0 ? -x : x;}
inline void swap(int &x, int &y){int tmp = x;x = y;y = tmp;}
inline void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
char s[1000], now;
int n, ans, cnt, p;
int main()
{
read(n);
scanf("%s", s + 1);
for(now = 1;now < n;++ now)
{
cnt = 0;
for(int j = 1;j < n;++ j)
if(s[now] == s[j] && s[now + 1] == s[j + 1]) ++ cnt;
if(cnt > ans) ans = cnt, p = now;
}
printf("%c%c", s[p], s[p + 1]);
return 0;
}
C. Less or Equal
Topic meaning:
Given a sequence of numbers and sequence length n, and a non-negative integer k, ask if there is a number x such that the number of elements in the sequence less than or equal to x is exactly k.
answer:
After sorting, check whether the kth and k+1th are equal.
Note that in the case of k=0, special judgment is required.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
inline int max(int a, int b){return a > b ? a : b;}
inline int min(int a, int b){return a < b ? a : b;}
inline int abs(int x){return x < 0 ? -x : x;}
inline void swap(int &x, int &y){int tmp = x;x = y;y = tmp;}
inline void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
int n, k, num[1000000];
int main()
{
read(n), read(k);
for(int i = 1;i <= n;++ i) read(num[i]);
std::sort(num + 1, num + 1 + n);
if(!k)
{
if(num[1] <= 1) printf("-1");
else printf("1");
return 0;
}
if(num[k] == num[k + 1]) printf("-1");
else printf("%d", num[k]);
return 0;
}
D. Divide by three, multiply by two
Topic meaning:
Give you a bunch of numbers, let you sort them, and require that for two adjacent elements \(a_i,a_{i+1}\) , satisfy \(a_I \times 2 = a_{i+1}\) or \(a_i \div 3 = a_{i+1}\) , the answer is guaranteed to exist
answer:
Since 2 and 3 are relatively prime, for any number \(a\) , \(a \ div 3\) cannot be transformed into \(a through \(\times 2\) and \(\div 3\) operations \times 2\) . This also means that if each number in the sequence is regarded as a point, each number must be a chain with an edge connected to the number in the sequence it can become.
Violently build a chain, just find a head.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
inline long long max(long long a, long long b){return a > b ? a : b;}
inline long long min(long long a, long long b){return a < b ? a : b;}
inline long long abs(long long x){return x < 0 ? -x : x;}
inline void swap(long long &x, long long &y){long long tmp = x;x = y;y = tmp;}
inline void read(long long &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f;
long long num[1000], nxt[1000], tag[1000], n;
int main()
{
read(n);
for(long long i = 1;i <= n;++ i) read(num[i]);
for(long long i = 1;i <= n;++ i)
for(long long j = 1;j <= n;++ j)
if(i == j) continue;
else if(num[i] * 2 == num[j] || num[i] == 3 * num[j]) nxt[i] = j, tag[j] = 1;
for(long long i = 1;i <= n;++ i)
if(!tag[i])
{
while(i) printf("%I64d ", num[i]), i = nxt[i];
return 0;
}
return 0;
}
E. Cyclic Components
Topic meaning:
Given a graph and asking you to have multiple connected components in it is a simple ring.
answer:
According to the process of finding connected components dfs, if the degree of a point is greater than 2, it is impossible to form a simple ring; if the degree of each point is less than 2, and the process of dfs walks back to a point that has been passed, it is a simple ring. .
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
inline int max(int a, int b){return a > b ? a : b;}
inline int min(int a, int b){return a < b ? a : b;}
inline int abs(int x){return x < 0 ? -x : x;}
inline void swap(int &x, int &y){int tmp = x;x = y;y = tmp;}
inline void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
struct Edge
{
int u, v, nxt;
Edge(int _u, int _v, int _nxt){u = _u, v = _v, nxt = _nxt;}
Edge(){}
}edge[2000010];
int head[2000010], cnt, vis[2000010], n, m, tmp1, tmp2, flag1, flag2, ans, fa[2000010];
inline void insert(int a, int b)
{
edge[++ cnt] = Edge(a, b, head[a]), head[a] = cnt;
}
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void dfs(int x, int pre)
{
vis[x] = 1;
int tot = 0;
for(int pos = head[x];pos;pos = edge[pos].nxt)
{
++ tot;
int v = edge[pos].v;
if(v == pre) continue;
int f1 = find(x), f2 = find(v);
if(fa[f1] == fa[f2]) flag2 = 1;
if(vis[v]) continue;
else fa[f1] = f2;
dfs(v, x);
}
if(tot > 2) flag1 = 0;
}
int main()
{
read(n), read(m);
for(int i = 1;i <= n;++ i) fa[i] = i;
for(int i = 1;i <= m;++ i)
read(tmp1), read(tmp2), insert(tmp1, tmp2), insert(tmp2, tmp1);
for(int i = 1;i <= n;++ i)
if(!vis[i])
{
flag1 = 1, flag2 = 0, dfs(i, -1);
ans += flag1 & flag2;
}
printf("%d", ans);
return 0;
}
F. Consecutive Subsequence
Topic meaning:
You are given a sequence and asked to find the longest subsequence with a monotonically increasing tolerance of 1.
answer:
First discretize, \(dp[i]\) represents the length of the longest subsequence ending with \(i\) , \(tong[i]\) represents the number \(i\) in the previously determined \(dp In the \) state, the number is the largest \(dp\) value at the position of \(ti\) . Just transfer, record the plan with a linked list.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
inline int max(int a, int b){return a > b ? a : b;}
inline int min(int a, int b){return a < b ? a : b;}
inline int abs(int x){return x < 0 ? -x : x;}
inline void swap(int &x, int &y){int tmp = x;x = y;y = tmp;}
inline void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
int dp[1000010], num[1000010], pos[1000010], pre[1000010], tong[1000010], n, tot, ans, p;
struct Node
{
int rank, num;
}node[1000010];
bool cmp(Node a, Node b)
{
return a.num < b.num;
}
void dfs(int x)
{
if(!x) return;
dfs(pre[x]);
printf("%d ", x);
}
int main()
{
read(n);
for(int i = 1;i <= n;++ i) read(node[i].num), node[i].rank = i, dp[i] = 1;
std::sort(node + 1, node + 1 + n, cmp);
for(int i = 1;i <= n;++ i)
{
if(node[i].num != node[i - 1].num) ++ tot;
if(node[i].num - node[i - 1].num > 1) ++ tot;
pos[tot] = node[i].num, num[node[i].rank] = tot;
}
for(int i = 1;i <= n;++ i)
{
dp[i] += dp[tong[num[i] - 1]], pre[i] = tong[num[i] - 1];
if(dp[tong[num[i]]] < dp[i])
tong[num[i]] = i;
}
for(int i = 1;i <= n;++ i)
if(dp[i] > ans) ans = dp[i], p = i;
printf("%d\n", ans);
dfs(p);
return 0;
}
Needle water.