超时代码(但优雅):
超时原因:lambda匿名函数拖累了太多运行时间
#include<bits/stdc++.h>
#define string std::string
#define il inline
#define inf 0x3f3f3f3f
typedef long long LL;
typedef unsigned long long ULL;
typedef std::pair<int, int> PII;
//快速读入
il int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * w;
}
auto individual_digits(int u) {
int res = 0;
while (u) res += u % 10, u /= 10;
return res;
}
int main() {
int n, m;
std::vector<int> q;
n = read(), m = read();
for (int i = 0; i < n; ++i)
q.push_back(i + 1);
std::sort(q.begin(), q.end(), [](auto a, auto b) {
if (individual_digits(a) != individual_digits(b))
return individual_digits(a) < individual_digits(b);
else
return a < b;
});
std::cout << q[m - 1] << std::endl;
return 0;
}
具体时长:
可过代码
#include<bits/stdc++.h>
#define string std::string
#define il inline
#define inf 0x3f3f3f3f
typedef long long LL;
typedef unsigned long long ULL;
typedef std::pair<int, int> PII;
//快速读入
il int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * w;
}
const int N = 1e6 + 10;
int n, m;
std::vector<int> q;
int cnt[N];
bool cmp (int x, int y) {
if (cnt[x] != cnt[y])
return cnt[x] < cnt[y];
else
return x < y;
}
int main(){
n = read(), m = read();
for (int i = 1; i <= n; i ++){
int num = i;
while (num){
cnt[i] += num % 10;
num /= 10;
}
q.push_back(i);
}
std::sort (q.begin(), q.end(), cmp);
std::cout << q[m - 1] << std::endl;
return 0;
}