T1
题意
有一个 的棋盘上有的字母, 一整行、一整列,或是一整条对角线,都是一个字母,那么这个字母就获胜了。如果某一行、一列,或是一条对角线是两个字母,那么这两个字母就获胜。 第一行输出能够获胜的单独的字母的数量。第二行,输出能够获胜的两个字母的数量。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 10;
char a[N][N];
bool one[27], two[27][27];
//one表示能获胜的一个字母,two表示能获胜的一个队
int ans1,ans2;
void check(char x, char y, char z) {//判断是否能赢,x,y,z表示要判断的三个字母
if (x==y && y==z)//判断能不能单独获胜
{
one[x-'A'] = 1;//打标记
return;
}
if (x==y) two[x-'A'][z-'A'] = two[z-'A'][x-'A'] = 1;//打标记
if (y==z) two[y-'A'][x-'A'] = two[x-'A'][y-'A'] = 1;//打标记
if (x==z) two[x-'A'][y-'A'] = two[y-'A'][x-'A'] = 1;//打标记
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
for (int i = 1; i <= 3; i++){//输入表格
for (int j = 1; j <= 3; j++){
cin >> a[i][j];
}
}
//判断每⼀⾏
check(a[1][1], a[1][2], a[1][3]);
check(a[2][1], a[2][2], a[2][3]);
check(a[3][1], a[3][2], a[3][3]);
//判断每⼀列
check(a[1][1], a[2][1], a[3][1]);
check(a[1][2], a[2][2], a[3][2]);
check(a[1][3], a[2][3], a[3][3]);
//判断2条对⻆线
check(a[1][1], a[2][2], a[3][3]);
check(a[1][3], a[2][2], a[3][1]);
for (int i = 0; i <= 25; i++){
if (one[i]) ans1++;//记录单独获胜的数量
}
for (int i = 0; i <= 25; i++){
for (int j = 0; j <= 25; j++){
if (two[i][j]) ans2++;//记录一对获胜的数量
}
}
cout << ans1 << endl << ans2/2;//因为标记时标记了两种情况,所以ans2要除2
return 0;
}T2
题意
有 N 个点,第 i 个点的坐标为 , 求能凑出多少长方形
代码
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 2e5 + 10;
pair<int, int> a[N];
map<pair<int, int>, bool> mp;//记录着是否有这个点
int n, ans;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
mp[{a[i].first, a[i].second}] = 1;//打标记
}
for (int i = 1; i <= n; i++) {//通过两个点确定另外两个点
for (int j = i; j <= n; j++) {
int x1 = a[i].first, y11 = a[i].second, x2 = a[j].first, y2 =a[j].second;
if (x1 < x2 && y11 < y2)//判断是否为长方形
{
if (mp.count({x2, y11}) && mp.count({x1, y2})) ans++;//判断是否有这两个点
}
}
}
cout << ans;
return 0;
}T3
题意
T735761 大鹅要体检
题目描述
有两个⻓度为 N 的数组 a 和 b 。 每次选择⼀个区间 ,将 a 中区间的元素反转。 对于每个 c,统计有多少种不同的区间,使操作后后的 a 与 b 恰好有 c 个位置的元素相等。
代码
#include <bits/stdc++.h>
using namespace std;
int n,a[7505],b[7505],ans[7505],cnt,l,r;
int main(){
cin>>n;
for(int i = 1;i<=n;i++) cin>>a[i];
for(int i = 1;i<=n;i++) cin>>b[i];
//统计开始有几个位置相同
for(int i = 1;i<=n;i++) if(a[i]==b[i]) cnt++;
for(int i = 1;i<=n;i++){
int tmp=cnt;
// 枚举奇数区间
l=i,r=i;
while(l>=1&&r<=n){
if(a[l]==b[l]&&a[l]!=b[r]) tmp--;
if(a[r]==b[r]&&a[r]!=b[l]) tmp--;
if(a[l]!=b[l]&&a[l]==b[r]) tmp++;
if(a[r]!=b[r]&&a[r]==b[l]) tmp++;
ans[tmp]++; // 每扩散⼀次,区间多个
l--,r++;
}
if(i==n) break;
// 枚举偶数区间
l=i,r=i+1;
tmp=cnt;
while(l>=1&&r<=n){
if(a[l]==b[l]&&a[l]!=b[r]) tmp--;
if(a[r]==b[r]&&a[r]!=b[l]) tmp--;
if(a[l]!=b[l]&&a[l]==b[r]) tmp++;
if(a[r]!=b[r]&&a[r]==b[l]) tmp++;
ans[tmp]++;
l--,r++;
}
}
for(int i = 0;i<=n;i++) cout<<ans[i]<<'\n';
}T4
题意
有⻓度为 N 的数组 h 。求每个⻓度的⼦数组的最⼩不对称值。
代码
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 5e3 + 10;
int n, a[N];
int t[N];
int main() {
memset(t, 0x3f, sizeof t);
t[1] = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 2; i < n; i++) {
int l = i, r = i, sum = 0;
while (l>=1 && r<=n) {//求奇数个的最⼩不对称值
sum += abs(a[l] - a[r]);
t[r - l + 1] = min(t[r - l + 1], sum);
l--; r++;
}
}
for (int i = 1; i < n; i++) {
int l = i, r = i + 1, sum =0;
while (l>=1 && r<=n) {//求偶数个的最⼩不对称值
sum += abs(a[l] - a[r]);
t[r - l + 1] = min(t[r - l + 1], sum);
l--; r++;
}
}
t[1] = 0;
for (int i = 1; i <= n; i++) {
cout << t[i] << " ";
}
return 0;
}