比赛地址
A. AcWing 4722. 数列元素
题意:
给定一个 n ,判断 n 是否是 1 + 2 + 3 + 4 + … + k 的和
思路:
简单模拟
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define int long long
#define div() cout << "-----------------------------" << endl
#define deb(n) cout<< #n << " = " << n <<endl
typedef pair<int, int> PII;
typedef pair<string, int> PSI;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 2e5 + 5, INF = 0x3f3f3f3f;
int n;
int a[N];
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
int sum = 0;
bool flag = false;
for (int i = 1; i <= n; i ++)
{
sum += i;
if (sum == n)
{
flag = true;
break;
}
}
if (flag) cout << "YES" << endl;
else cout << "NO" <<endl;
return 0;
}
B. AcWing 4722. 数列元素
题意:
字符串按 abcdeaabbccddeeaaaabbbbccccddddeeeeaaaaaaaabbbbbbbbccccccccddddddddeeeeeeee 排列,求第 n 个字符
思路:
第一段为 5 个字符,第二段为 10 个字符,第三段为 20 个字符,第四段为 40 个字符,以此类推
所以我们首先要确定第 n 个字符属于哪个字段
int k = 5, sum = 0;
while ( sum + k < n)
{
sum += k;
k *= 2;
}
接着我们我们令 n = n - sum 即可得到 n 为当前字段的第几个字符
同时 k 表示当前字段共有多少个字符,k / 5 即可以得到每个字符占多大长度
要求第 n 个字符 = ⌈ n / len ⌉ 即 ⌊ (n + len - 1) / len ⌋
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define int long long
#define div() cout << "-----------------------------" << endl
#define deb(n) cout<< #n << " = " << n <<endl
typedef pair<int, int> PII;
typedef pair<string, int> PSI;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 2e5 + 5, INF = 0x3f3f3f3f;
int n, m;
int a[N];
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
int sum = 0, k = 5;
while (sum + k < n)
{
sum += k;
k *= 2;
}
n -= sum;
int len = k / 5;
int t = (n + len - 1) / len
cout << (char)( t - 1 + 'a') << endl;
return 0;
}
C. AcWing 4724. 靓号
题意:
给定一个长度为 n 且只包含 0 ~ 9 数字的 s ,和一个数 m ,
你可以进行如下操作:
- 将 s 中的一个数字 c 变为 x ,代价为 | x - c |
求最小代价,使得 s 中含有 m 个相同的数字,若代价相同的有多个,求字典序最小的那个
思路:
枚举 0 ~ 9 共 10 种情况,使得 s 中有 m 个字符(0 ~ 9),由于需要代价最小,所以优先考虑代价小的操作,对于当前操作,由于需要字典序最小,所以优先考虑将大数变小的操作
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define int long long
#define div() cout << "-----------------------------" << endl
#define deb(n) cout<< #n << " = " << n <<endl
typedef pair<int, int> PII;
typedef pair<string, int> PSI;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 2e5 + 5, INF = 0x3f3f3f3f;
int n, m;
string str;
string ans;
int minm = 1e9;
void oper(char x)
{
string s = str;
vector<int> p[19]; // -9 ~ 9 ---> 0 ~ 18
for (int i = 0; i < s.size(); i ++) p[s[i] - x + 9].push_back(i);
int cnt = 0;
int cost = 0;
for (int k = 0; cnt < m; k ++)
{
for (int i = 0; i < p[9 + k].size() && cnt < m; i ++) // 大变小
{
cnt ++;
cost += k;
s[p[9 + k][i]] = x;
}
if (k != 0)
{
for (int i = p[9 - k].size() - 1; i >= 0 && cnt < m; i --) // 小变大
{
cnt ++;
cost += k;
s[p[9 - k][i]] = x;
}
}
}
if (cost < minm || cost == minm && s < ans)
{
minm = cost;
ans = s;
}
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
cin >> str;
for (int i = 0; i < 10; i ++)
oper(i + '0');
cout << minm << endl << ans << endl;
return 0;
}