A. Reachable Numbers
一开始就被题意唬着了,仔细看一下就是根据题意模拟就完事了。
#include <bits/stdc++.h>
using namespace std;
map<int,int>flag;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,ans = 0;
cin >> n;
int q = n;
while(1)
{
n++;
while(n % 10 == 0)
n /= 10;
if(flag[n] == 0)
{
flag[n] = 1;
ans++;
}
else
break;
}
if(flag[q] == 0)
cout << ans + 1 << endl;
else
cout << ans << endl;
return 0;
}
B. Long Number
也是根据题意模拟,没什么说的了
#include <bits/stdc++.h>
using namespace std;
int n, arr[10];
string str;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> str;
for (int i = 1; i <= 9; i++)
cin >> arr[i];
int dis = 0;
while (arr[str[dis] - '0'] <= str[dis] - '0')
dis++;
for (int i = dis; i < str.size(); i++)
{
int now = str[i] - '0';
if (arr[now] >= str[i] - '0')
str[i] = arr[now] + '0';
else
break;
}
cout << str << endl;
return 0;
}
C1. Increasing Subsequence (easy version)
先排序,然后从小往大遍历,然后看原序列的头尾哪个元素和当前的元素相同,相同则移指针的位置,否则就继续往下走。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int x[maxn],vis[maxn],y[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
#endif // ONLINE_JUDGE
int n,k = 0;
cin >> n;
for(int i = 1;i <= n; i++)
{
cin >> x[i];
y[i] = x[i];
}
sort(y+1,y+1+n);
int l = 1,r = n;
for(int i = 1;i <= n; i++)
{
if(x[l] == y[i])
{
vis[k++] = 1;
l++;
}
else if(x[r] == y[i])
{
vis[k++] = 2;
r--;
}
}
cout << k << endl;
for(int i = 0;i < k; i++)
{
if(vis[i] == 1)
cout << "L";
else
cout << "R";
}
return 0;
}
C2. Increasing Subsequence (hard version)
这个题和C1题目描述一样,但是输入的时候会有相同的元素,这个时候就要贪心一下了。原来的时候头尾指针指的元素不会相同,优先取小的就好了。如果头尾指针指的元素值相等的话,就while往里走,取走的远的,因为要求取出的元素升序,所以取了一端的话,就肯定不会再从另一端取了。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int x[maxn], vis[maxn], y[maxn], z[maxn];
set<int> s;
int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
#endif // ONLINE_JUDGE
int n, k = 0, ll = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x[i];
s.insert(x[i]);
}
set<int>::iterator it;
for (it = s.begin(); it != s.end(); it++)
y[ll++] = *it;
int l = 1, r = n;
for (int i = 0; i < ll; i++) {
// cout << l << " " << r << endl;
if (x[l] == x[r] && x[l] == y[i]) {
int ll = l + 1, rr = r - 1, lll = 0, rrr = 0;
while (x[ll] > x[ll - 1]) {
ll++;
lll++;
}
while (x[rr] > x[rr + 1]) {
rr--;
rrr++;
}
if (rrr > lll) {
rr = r - 1;
vis[k++] = 2;
rrr = 0;
while (x[rr] > x[rr + 1]) {
rr--;
rrr++;
vis[k++] = 2;
}
} else {
ll = l + 1;
vis[k++] = 1;
lll = 0;
while (x[ll] > x[ll - 1]) {
ll++;
lll++;
vis[k++] = 1;
}
}
} else if (x[l] == y[i]) {
z[k] = x[l];
vis[k++] = 1;
l++;
} else if (x[r] == y[i]) {
z[k] = x[r];
vis[k++] = 2;
r--;
}
}
if (s.size() == 1) {
cout << "1" << endl << "L" << endl;
return 0;
}
cout << k << endl;
for (int i = 0; i < k; i++) {
if (vis[i] == 1)
cout << "L";
else
cout << "R";
}
return 0;
}