A. Vlad and the Best of Five
AC code:
void solve() {
string s; cin >> s;
int t = 0;
for (auto x : s) {
if (x == 'A') t ++;
else t --;
}
if (t > 0) cout << 'A' << endl;
else cout << 'B' << endl;
}
B. Vlad and Shapes
题意:找正方形和三角形。
思路:记录行列坐标,看是否同时具有多个最左/最上的坐标。
AC code:
void solve() {
cin >> n;
map<int, int> h, l;
for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j ++) {
char c; cin >> c;
int x = c - '0';
if (x == 1) {
h[i] ++, l[j] ++;
}
}
int flag = 0;
for (auto [x, y] : l) {
if (y >= 2) flag ++;
break;
}
for (auto [x, y] : h) {
if (y >= 2) flag ++;
break;
}
if (flag == 2) cout << "SQUARE" << endl;
else cout << "TRIANGLE" << endl;
}
C. Vlad and a Sum of Sum of Digits
题意:计算1到n每一个数位的和。
思路:题目时间所限,所以要在样例外前缀和预处理出所有1到2e5的数位和。
AC code:
int sum[N];
void solve() {
cin >> n;
cout << sum[n] << endl;
}
signed main() {
fast();
for (int i = 1; i <= 2e5; i ++) {
int x = i, ca = 0;
while (x) {
ca += x % 10;
x /= 10;
}
sum[i] = sum[i - 1];
sum[i] += ca;
}
T = 1;
cin >> T;
while (T --) {
solve();
}
return 0;
}
D. Vlad and Division
题意:给出n个十进制整数,进行分组,一个组中的任意两数的对应31位二进制必须均不相同,最少分多少组。
思路:
最佳可能对半分组,符合条件的二进制一定是一一对应的,即一组最多2个数。
遍历n个整数,遍历过程中存取每一个数的二进制,我这里用的是字符串存取,然后在已存取的字符串中看是否出现过对应可分为一组的二进制。
AC code:
void solve() {
cin >> n;
for (int i = 0; i < n; i ++) cin >> a[i];
map<string, int> mp;
int ans = n;
for (int i = 0; i < n; i ++) {
int x = a[i], y = 0;
string uu = "", vv = "";
while (x) {
y ++;
int t = x % 2;
if (t) uu += '1', vv += '0';
else uu += '0', vv += '1';
x /= 2;
}
int tt = 31 - y;
while (tt --) {
uu += '0';
vv += '1';
}
mp[uu] ++;
//cout << uu << "+++" << endl;
if (mp[vv] >= 1) {
ans --, mp[vv] --, mp[uu] --;
}
}
cout << ans<< endl;
}
E. Vlad and an Odd Ordering
题意:现在有1到n张牌,按从小到大的顺序放下牌中的奇数,然后从小到大放下手中牌为任意奇数2倍的牌,以此类推,直到放下所有的牌,求第k次放下的牌是多少。
思路:
- 除第一次外,之后的轮次放下的牌一定会是第一轮奇数的偶数倍,因为奇数倍第一轮就放下了
- 每轮放下的牌数为(假设当前剩余n张牌):n/2+n%2
- 只需要枚举需要多少轮,得到第k张牌是第几轮对应的第几个奇数即可
AC code:
void solve() {
cin >> n >> k;
int now = 1;
for (int i = 1; i <= 2e5; i ++) {
if (n / 2 + (n % 2) < k) {
k -= n / 2 + (n % 2);
now *= 2;
n /= 2;
} else {
cout << (2 * k - 1) * now << endl;
break;
}
}
}