链接:https://atcoder.jp/contests/abc414
A – Streamer Takahashi
算法:
模拟。
思路:
无。
关键代码:
void miyan()
{
int n, l, r;
cin >> n >> l >> r;
ll ans = 0;
while (n--)
{
int x, y;
cin >> x >> y;
if (x <= l && y >= r)
++ans;
}
cout << ans << endl;
}
B – String Too Long
算法:
模拟。
思路:
无。
关键代码:
void miyan()
{
int n;
cin >> n;
ll cnt = 0;
bool ok = 1;
string ans = "";
for (int i = 0; i < n; ++i)
{
ll l;
char c;
cin >> c >> l;
if (l > 100 || cnt + l > 100)
ok = 0;
if (ok)
{
cnt += l;
ans += string(l, c);
}
}
if (ok)
cout << ans << endl;
else
cout << "Too Long" << endl;
}
C – Palindromic in Both Bases
算法:
大模拟。
思路:
\(n\) 是 \(1e12\),不能直接循环找,考虑到回文数前半部分和后半部分一样,对于每个数字只枚举前半部分,后半部分直接原样复制即可,再考虑在两半部分之间加数字,以统计奇数长度的回文数。
关键代码:
void miyan()
{
ll a, n;
cin >> a >> n;
auto check = [&](string s) -> bool
{
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
if (s[i] != s[j])
return 0;
string res = "";
ll x = stoll(s);
while (x > 0)
{
ll t = x % a;
res.push_back(t + '0');
x /= a;
}
for (int i = 0, j = res.size() - 1; i < j; ++i, --j)
if (res[i] != res[j])
return 0;
return 1;
};
ll ans = 0;
for (int i = 1; i <= min(n, 9ll); ++i)
{
string s = to_string(i);
if (check(s))
ans += i;
}
for (int i = 1; i <= min(1ll * N, n); ++i)
{
string s = to_string(i);
int len = s.size();
{
string ss = s;
ss += '0';
for (int j = len - 1; j >= 0; --j)
ss += s[j];
ll num = stoll(ss);
if (num <= n)
{
if (check(ss))
ans += num;
for (char c = '1'; c <= '9'; ++c)
{
ss[len] = c;
num = stoll(ss);
if (num > n)
break;
if (check(ss))
ans += num;
}
}
}
{
string t = s;
for (int j = len - 1; j >= 0; --j)
t += s[j];
ll num = stoll(t);
if (num > n)
break;
if (check(t))
ans += num;
}
}
cout << ans << endl;
}
D – Transmission Mission
算法:
贪心。
思路:
经典区间问题,分 \(m\) 段就是找 \(m – 1\) 个间隔,直接取前 \(m – 1\) 个最大间隔即可。
关键代码:
void miyan()
{
int n, m;
cin >> n >> m;
vector<ll> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
ranges::sort(a);
vector<ll> cnt;
cnt.reserve(n);
for (int i = 1; i < n; ++i)
cnt.push_back(a[i] - a[i - 1]);
ranges::sort(cnt, greater<>());
ll ans = accumulate(all(cnt), 0ll);
for (int i = 0; i < m - 1; ++i)
ans -= cnt[i];
cout << ans << endl;
}
E – Count A%B=C
整除分块后面再补。