Educational Codeforces Round 121 (Rated for Div. 2) ABC(区间求并)
A. Equidistant Letters
直接对原字符串进行排序,这样能保证相同的字母相邻,间隔为0.
#include
#include
#include
#define pii pair
#define fi first
#define se second
#define ll long long
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
string s;
cin >> s;
vector v;
for(auto x : s) v.push_back(x);
sort(v.begin(), v.end());
for(auto x : v) cout << x;
cout << endl;
}
return 0;
}
B. Minor Reduction
You are given a decimal representation of an integer ??x without leading zeros.
You have to perform the following reduction on it exactly once: take two neighboring digits in ??x and replace them with their sum without leading zeros (if the sum is 00, it's represented as a single 00).
For example, if ??=10057x=10057, the possible reductions are:
- choose the first and the second digits 11 and 00, replace them with 1+0=11+0=1; the result is 10571057;
- choose the second and the third digits 00 and 00, replace them with 0+0=00+0=0; the result is also 10571057;
- choose the third and the fourth digits 00 and 55, replace them with 0+5=50+5=5; the result is still 10571057;
- choose the fourth and the fifth digits 55 and 77, replace them with 5+7=125+7=12; the result is 1001210012.
What's the largest number that can be obtained?
Input
The first line contains a single integer ??t (1≤??≤1041≤t≤104) — the number of testcases.
Each testcase consists of a single integer ??x (10≤??<1020000010≤x<10200000). ??x doesn't contain leading zeros.
The total length of the decimal representations of ??x over all testcases doesn't exceed 2?1052?105.
Output
For each testcase, print a single integer — the largest number that can be obtained after the reduction is applied exactly once. The number should not contain leading zeros.
Example
input
Copy
2
10057
90
output
Copy
10012
9
整体逻辑是,首先从高位到低位,如果两个位置交换后比原来大,立即换;如果交换后比原来小但是总位数不变,则记录下最小的这样的位置。遍历后如果已经交换过则直接输出,否则如果有记录下的位置,则交换记录的位置的这两个数并输出。剩余的情况就是不论交换哪个位置必然发生字符串整体缩短,那么交换最高位两个位置即可(因为缩短只可能是两个位置加起来不超过10,这样的话加起来的和比任何一个位置都不小。如果发生缩短,选别的位置最终得到的最高位还是原来的最高位,如果选一开始的两个位置,最终得到的最高位不小于原来的最高位,一定不劣)。
#include
#include
#define pii pair
#define fi first
#define se second
#define ll long long
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
string s;
cin >> s;
bool ok = 0;
int pos = -1;
for(int i = 0; i < s.size() - 1; i++) {
int a = s[i] - '0', b = s[i + 1] - '0';
int sum = a + b;
if(a == 0 || b == 0) continue;
if(sum > a * 10 + b) {
a = sum / 10, b = sum % 10;
s[i] = a + '0', s[i + 1] = b + '0';
ok = 1;
break;
} else if(sum >= 10) {
pos = max(pos, i);
}
}
if(ok) cout << s << endl;
else {
if(pos != -1) {
int a = s[pos] - '0', b = s[pos + 1] - '0';
int sum = a + b;
a = sum / 10, b = sum % 10;
s[pos] = a + '0', s[pos + 1] = b + '0';
cout << s << endl;
continue;
}
int n = s.size();
int a = s[0] - '0', b = s[1] - '0';
int sum = a + b;
a = sum / 10, b = sum % 10;
s[0] = a + '0', s[1] = b + '0';
if(!a) s = s.substr(1);
cout << s << endl;
}
}
return 0;
}
C. Monsters And Spells
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters.
The level he's currently on contains ??n monsters. The ??i-th of them appears ????ki seconds after the start of the level and has ???hi health points. As an additional constraint, ???≤????hi≤ki for all 1≤??≤??1≤i≤n. All ????ki are different.
Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: 1,2,3,…1,2,3,… The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is 11. Otherwise, let the damage at the previous second be ??x. Then he can choose the damage to be either ??+1x+1 or 11. A spell uses mana: casting a spell with damage ??x uses ??xmana. Mana doesn't regenerate.
To kill the ??i-th monster, Monocarp has to cast a spell with damage at least ???hi at the exact moment the monster appears, which is ????ki.
Note that Monocarp can cast the spell even when there is no monster at the current second.
The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters.
It can be shown that it's always possible to kill all monsters under the constraints of the problem.
Input
The first line contains a single integer ??t (1≤??≤1041≤t≤104) — the number of testcases.
The first line of the testcase contains a single integer ??n (1≤??≤1001≤n≤100) — the number of monsters in the level.
The second line of the testcase contains ??n integers ??1<??2<?<????k1 The third line of the testcase contains ??n integers ?1,?2,…,???h1,h2,…,hn (1≤???≤????≤1091≤hi≤ki≤109) — the health of the ??i-th monster. The sum of ??n over all testcases doesn't exceed 104104. Output For each testcase, print a single integer — the least amount of mana required for Monocarp to kill all monsters. Example input Copy output Copy 题意很有迷惑性。这个题实际上是给出一些怪物的出现时间(仅在这一秒出现)以及血量,自己可以选择从某个时间蓄力,下一个时间的蓄力值是当前值+1,一开始蓄力值为0。当然也可以选择打断蓄力,再次蓄力的话蓄力值从1开始。同时可以选择进行攻击,攻击伤害即为蓄力值,且攻击并不消耗蓄力值。每一秒需要消耗当前蓄力值大小的mana,问最终击败所有的怪最小需要多少mana。 首先注意,因为怪仅在那一秒出现,所以在那一秒的蓄力值必须大于等于怪的血量。设第i个怪的出现时间为k[i],血量为h[i],则至少需要从k[i] - h[i] + 1开始蓄力。同时,如果有怪的出现时间比k[i]早,最早蓄力时间比k[i] - h[i] + 1晚,那么这个怪对于答案的贡献是可以忽略的。如果两个怪的蓄力时间短有交叉,那么需要把这两个怪合并(即从前一个怪开始蓄力的时间一直蓄力到后一个怪的出现时间)。这样整个问题就转化成了区间求并了,可以用一个栈维护,轻松解决~3
1
6
4
2
4 5
2 2
3
5 7 9
2 1 2
10
6
7
#include