https://codeforces.com/contest/1328
原来队内刷的就是套题…
Divisibility Problem
贪心
int main() {
IOS;
// freopen("P1908_6.in","r",stdin);//读入数据
// freopen("P1908.out","w",stdout); //输出数据
int t;
cin >> t;
while(t--){
ll a, b;
cin >> a >> b;
if(a % b == 0)
cout << 0 << endl;
else {
ll temp = (a / b + 1) * b;
cout << temp - a << endl;
}
}
return 0;
}
K-th Beautiful String
找规律后找位置
规律:b的长度与排列的方式
abb 1
bab 2
bba 3
baab 4(这里我瞎写的,理是这么个理)
直接遍历找第一个b去减就可以得到第二个b位了,前缀和
我理解的是第几组第几个
我的想法和官方反过来的
我:9876543210
官方:0123456789
学到的新方式:
string s(n, ‘a’);
int main() {
IOS;
int t;
cin >> t;
while(t--){
ll n, k;
cin >> n >> k;
ll temp1 = 1ll * (sqrt(1 + 4 * 2 * k) - 1) / 2;//第一个b所在的位置
ll temp = temp1 * (temp1 + 1) / 2;
ll temp2 = k - temp1 * (temp1 + 1) / 2;//第二个b所在的位置
if(temp < k)
++temp1, --temp2;
else if(temp == k)
temp2 = temp1 - 1;
//cout << temp1 << " " << temp2 << endl;
for (int i = n - 1; i >= 0; --i){
if(i != temp1 && i != temp2){
cout << 'a';
}
else
cout << 'b';
}
cout << endl;
}
return 0;
}
官方
int main() {
int t;
cin >> t;
forn(tt, t) {
int n, k;
cin >> n >> k;
string s(n, 'a');
for (int i = n - 2; i >= 0; i--) {
if (k <= (n - i - 1)) {
s[i] = 'b';//最左 第几组
s[n - k] = 'b';//最右 第几个
cout << s << endl;
break;
}
k -= (n - i - 1);//前缀和
}
}
}
Ternary XOR
贪心,如代码
int main() {
IOS;
int t;
cin >> t;
while(t--){//因为xor是不进位加法,使a,b尽量平均
int n;
cin >> n;
string s;
cin >> s;
string a, b;
//把不均分的地方记下来,大的后面统统放小的
bool flag = true;
for (int i = 0; i < n; ++i){
int t = s[i] - '0';
if(t == 0) {
a += "0";
b += "0";
}
else if(t == 2) {
if(flag){
a += "1";
b += "1";
}
else {
a += "0";
b += "2";
}
}
else {
if(flag){
a += "1";
b += "0";
flag = false;
}
else {
a += "0";
b += "1";
}
}
}
cout << a << endl;
cout << b << endl;
}
return 0;
}
Carousel
四色问题的内容是“任何一张地图只用四种颜色就能使具有共同边界的国家着上不同的颜色。”也就是说在不引起混淆的情况下一张地图只需四种颜色来标记就行。
https://www.cnblogs.com/Osea/p/12603390.html
vector<int> v;
int main() {
IOS;
// freopen("P1908_6.in","r",stdin);//读入数据
// freopen("P1908.out","w",stdout); //输出数据
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
v.clear();
bool flag = true;
bool cnt = false;
int pre = -1;
for (int i = 0; i < n; ++i) {
int t;
cin >> t;
if (pre != -1 && t != pre)
flag = false;
else if (t == pre)
cnt = true;
v.push_back(t);
pre = t;
}
if (v[0] == v[n - 1])
cnt = true;
if (flag) {
cout << 1 << endl;
while (n--) {
cout << 1 << " ";
}
} else if (n % 2 == 0) {
cout << 2 << endl;
for (int i = 0; i < n; ++i) {
if (i % 2)
cout << 2 << " ";
else
cout << 1 << " ";
}
} else {
if (cnt) {
flag = true;
int pre = 1;
cout << 2 << endl;
for (int i = 0; i < n; ++i) {
if (flag && i < n - 1 && v[i] == v[i + 1]) {
cout << pre << " " << pre << " ";
++i;
flag = false;
} else {
if (i % 2)
cout << pre << " ";
else
cout << pre << " ";
}
pre = pre % 2 + 1;
}
} else {
cout << 3 << endl;
for (int i = 0; i < n - 1; ++i) {
if (i % 2 == 0)
cout << 1 << " ";
else
cout << 2 << " ";
}
cout << 3;
}
}
cout << endl;
}
return 0;
}