https://codeforces.com/contest/1520
A. Do Not Be Distracted!
题意:给出一个长度为n的字符串,问相同字母之间是否存在其他字母,有的话输出NO,没有输出YES。
思路:无脑暴力模拟啦。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char a[66];
int main()
{
int t, n;
cin >> t;
while(t--)
{
cin >> n >> a + 1;
int flag = 0;
for(int i = 1; i <= n;i++)
{
flag = 0;
for(int j = n;j > i; j--)
{
if(a[j] == a[i])
{
for(int k = i;k < j; k++)
{
if(a[k] != a[i])
{
flag = 1;
break;
}
}
if(flag) break;
}
}
if(flag) break;
}
if(flag) puts("NO");
else puts("YES");
}
return 0;
}
B. Ordinary Numbers
题意:给出n,问[1, n]中每一位都相同的数字有多少个。
思路:很容易发现,假设n有k位,那么ans = (k - 1) * 9 + n / k个1。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[13] = {0, 1, 11, 111, 1111, 11111, 111111, 1111111,
11111111,111111111,1111111111};
int main()
{
int t, n;
cin >> t;
while(t--)
{
cin >> n;
int ans = 0;
int cnt = 0;
int m = n;
while(m)
{
cnt++;
m /= 10;
}
ans = (cnt - 1) * 9 + n / a[cnt];
cout << ans << endl;
}
return 0;
}
C. Not Adjacent Matrix
题意:给出一个n,构造一个n * n的矩阵,使得相邻的两个数相差不为1,且[1, n * n]只在矩阵中出现一次,不存在这样的矩阵输出-1.
思路:将矩阵压成一维,跳着构造就行,只有2 * 2无解。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 111;
int a[maxn * maxn];
int main()
{
int t, n;
cin >> t;
while(t--)
{
cin >> n;
if(n == 2)
{
cout << -1 << endl;
continue;
}
int cnt = 1;
for(int i = 1;i <= n * n; i += 2) a[i] = cnt++;
for(int i = 2;i <= n * n; i += 2) a[i] = cnt++;
for(int i = 1;i <= n * n ;i++)
{
cout << a[i] << ' ';
if(i % n == 0) cout << endl;
}
}
return 0;
}
D. Same Differences
题意:给出一个长度为n的序列a,问有多少对a[j] - a[i] = j - i。
思路:移项得 a[j] - j = a[i] - i, 统计一下再计数就行,用map会很方便,不用处理差为负数的情况。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
int main()
{
int t, n;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
map<int, int> m;
int a;
for(int i = 1;i <= n; i++)
{
scanf("%d", &a);
m[a - i]++;
}
ll ans = 0;
for(auto i:m) ans += (ll)i.second * (i.second - 1) / 2;
printf("%lld\n", ans);
}
return 0;
}