A. Exercising Walk
题意:给出上下左右移动的步数,及初始位置和可移动范围,判断能否在可移动范围内走完所有步数
分析:
1.计算同坐标轴之差的绝对值,判断初始位置移动后,是否在可行范围内
2.当某坐标轴上的可移动范围为点时,则该坐标轴上不可移动
详见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int inf = 0x3f3f3f3f;
const ll Inf = 1e18;
int main(void)
{
int t;
cin >> t;
while (t--) {
int a, b, c, d, x, y, x1, y1, x2, y2;
cin >> a >> b >> c >> d >> x >> y >> x1 >> y1 >> x2 >> y2;
int xx = b - a;
int yy = d - c;
xx += x;
yy += y;
if ((x1 == x2 && (a || b)) || (y1 == y2 && (c || d))) cout << "no\n";
else if (xx >= x1 && xx <= x2 && yy >= y1 && yy <= y2) cout << "yes\n";
else cout << "no\n";
}
return 0;
}
B. Composite Coloring
题意:给一组数字染色,染成同色的数字不能互质,找到一组可行解即可
分析:
1.染成同色的数需要互质,及有相同的因子
2.有相同因子的数染成同色即可
3.列出11个质数,逐一筛
详见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int inf = 0x3f3f3f3f;
const ll Inf = 1e18;
const int maxn = 1e3 + 5;
const int prime[20] = { 2,3,5,7,11,13,17,19,23,29,31 };
int an[maxn];
int ans[maxn];
int main(void)
{
int t;
cin >> t;
while (t--) {
memset(ans, 0, sizeof(ans));
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> an[i];
int cnt = 1, tot = n;
for (int i = 0; i < 11; i++) {
int flag = 0;
for (int j = 1; j <= n; j++) {
if (ans[j]) continue;
if (an[j] % prime[i] == 0) {
ans[j] = cnt;
flag = 1;
tot--;
}
}
if (flag) cnt++;
if (!tot) break;
}
cout << --cnt << endl;
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
cout << endl;
}
return 0;
}
C. K-Complete Word
题意:将一个字符串变成一个回文串,且形成长度为k的循环
分析:
1.要形成回文,且循环,每个字串都要是回文串
2.以n=6,k=3为例:
1 3 4 6位需相同,2 5位需相同
3.用map存对应位字符的出现次数,将对应位填成出现最多的字符即可,结果加上差值
详见代码注释:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int inf = 0x3f3f3f3f;
const ll Inf = 1e18;
const int maxn = 2e5 + 10;
typedef pair<char, int> pci;
typedef map<char, int>::iterator iter;
char ss[maxn];
map<char, int> mp;
int main(void)
{
int t;
cin >> t;
while (t--) {
int n, k, ans = 0;
cin >> n >> k;
cin >> ss + 1;
for (int i = 1; i <= k / 2; i++) {
mp.clear();
//将每个字段的对应字符存入map
for (int j = 0; j <= (n / k - 1); j++) {
mp[ss[i + j * k]]++;
mp[ss[i + j * k + k - i - i + 1]]++;
}
//找对应位字符出现最多的次数
int maxx = 0;
for (iter it = mp.begin(); it != mp.end(); it++) maxx = max(maxx, it->second);
//加上对应位的数量减去最大出现次数
ans += n / k * 2 - maxx;
}
//当k为奇数时,每个字段的中间位需要单独判断
if (k & 1) {
mp.clear();
int i = k / 2 + 1;
for (int j = 0; j <= (n / k - 1); j++) mp[ss[i + j * k]]++;
int maxx = 0;
for (iter it = mp.begin(); it != mp.end(); it++) maxx = max(maxx, it->second);
ans += n / k - maxx;
}
cout << ans << endl;
}
return 0;
}
D. Walk on Matrix
题意:构造一个矩阵,使正确算法输出与题中算法的输出差k
分析:
1.题中算法过度追求当前最大值,而忽略了当前较小,之后却可以增大的值
2.构造一个题目算法得0,正确算法为k的矩阵
2.构造一个很大的n,n为2的次方数,因为2的次方数仅有最高位为1。(n+k)&k=k,(n+k)&n=n,n&k=0
3.可以利用这个构造出一个2
∗
*
∗ 3的矩阵
详见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int inf = 0x3f3f3f3f;
const ll Inf = 1e18;
int main(void)
{
ll n = (1 << 17);
int k;
cin >> k;
cout << "2 3\n";
cout << n + k << " " << n << " " << n + k << endl;
cout << k << " " << n + k << " " << k << endl;
return 0;
}
EFG咕咕咕。。。