# 马蹄集 oj赛（双周赛第二十二次）

88 篇文章 61 订阅

​编辑门票

## 运动会进行中

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
using namespace std;
const int N=1e5 + 7;
int n,a[N],sum[N],l[N*2],r[N*2],ans;
int main(){
cin >>n;
for(int i=1;i<=n;i++){
cin >>a[i];
if(a[i] == 0)//将女生用-1表示，更容易计算前缀和
a[i] = -1;
sum[i]=sum[i-1]+a[i];//前缀和后，寻找相同数字的最远距离
int tmp=sum[i]+n;//sum[i]的范围是-n到n,矫正下保证为正数
if (!l[tmp])
l[tmp] = i;
else
r[tmp]=i;
}
for (int i=0;i<2 * n;i++)
ans = max(ans,r[i] - l[i]);
cout <<ans <<endl;
return 0;
}

## 矩形

#include <iostream>
#include <vector>
using namespace std;

int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> matrix(n, vector<int>(m));

// 读入矩阵
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
char ch;
cin >> ch;
matrix[i][j] = ch - '0';
}
}

int maxPerimeter = 0;
// 遍历所有可能的矩形
for (int x1 = 0; x1 < n; ++x1) {
for (int y1 = 0; y1 < m; ++y1) {
for (int x2 = x1; x2 < n; ++x2) {
for (int y2 = y1; y2 < m; ++y2) {
bool allZeros = true;
// 检查矩形内是否全部为0
for (int i = x1; i <= x2 && allZeros; ++i) {
for (int j = y1; j <= y2 && allZeros; ++j) {
if (matrix[i][j] != 0) {
allZeros = false;
}
}
}
if (allZeros) {
// 计算周长并更新最大周长
int perimeter = 2 * (x2 - x1 + 1) + 2 * (y2 - y1 + 1);
maxPerimeter = max(maxPerimeter, perimeter);
}
}
}
}
}

cout << maxPerimeter << endl;
return 0;
}


## 三连

#include <iostream>
#include <vector>
using namespace std;

int main() {
int n;
cin >> n;
vector<int> sequence(n);
long long totalSum = 0;

for (int i = 0; i < n; ++i) {
cin >> sequence[i];
totalSum += sequence[i];
}

// 如果总和不能被3整除，无法划分为三个和相等的段
if (totalSum % 3 != 0) {
cout << 0 << endl;
return 0;
}

long long targetSum = totalSum / 3;
long long prefixSum = 0, countTargetSum = 0, ways = 0;

for (int i = 0; i < n - 1; ++i) { // 注意，最后一个元素不参与划分
prefixSum += sequence[i];
if (prefixSum == 2 * targetSum) {
ways += countTargetSum;
}
if (prefixSum == targetSum) {
countTargetSum++;
}
}

cout << ways << endl;
return 0;
}


## 矩阵01变

800
810
000

000
0 1 1

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
using namespace std;
char a[118][110];
int b[110][110],sum[110][110];
int m,n;
int main(){
scanf("%d%d",&m,&n);
for (int i = 1;i <= m;++i)
scanf("%s",a[i] + 1);
for (int i=1;i<=m;i++){
for (int j = 1;j<=n;j++) {
if (a[i][j] == '1')
b[i][j] = 1;
}
}
for (int i = 1;i<=m;i++){
for (int j=1;j<=n;j++) {
sum[i][j] =
sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + b[i][j];
}
}
for (int i =1;i<=m;i++) {
for (int j = 1; j <= n; j++) {
cout << sum[i][j] << " ";
}
cout << endl;
}
return 0;
}

## 最大异或和

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
using namespace std;
int n,a[2010],sum[2010],ans = -0x3f3f3f3f;

int main(){
cin >>n;
for (int i=1;i<=n;i++) {
cin >>a[i];
sum[i] = sum[i - 1] ^ a[i];
}
for (int i=1;i<=n;i++)
for (int j=1;j<=i;j++)
ans = max(ans,sum[i] ^ sum[j -1]);
cout << ans;
return 0;
}

## 切蛋糕

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
using namespace std;
const int N=5e5 + 10;
int m,n,a[N],s[N],q[N],ans = -0x3f3f3f3f;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
s[i] = s[i - 1] + a[i];
}

int head = 0, tail = -1;
for (int i = 1; i <= n; i++) {
if (head <= tail && q[head] <= i - m)
while (head <= tail && s[i] <= s[q[tail]])
tail--;
q[++tail] = i;
ans = max(ans, s[i] - s[q[head]]);
}
cout << ans;
return 0;
}

## 信号灯

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
using namespace std;
const int N=1e5 +10;
int n,k,b,a[N],sum [N],ans = 0x3f3f3f3f;
int main() {
cin >> n >> k >> b;
int tmp;
while (b--) {
cin >> tmp;
a[tmp] = 1;
}
for (int i = 1; i <= n; i++)
sum[i] = sum[i - 1] + a[i];
for (int i = 1; i + k - 1 <= n; i++)
ans = min(ans, sum[i + k - 1] - sum[i - 1]);
cout << ans;
return 0;
}

## 背军理

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n,q,num[N],sum [N];
int main(){
cin >>n;
for (int i=1;i<=n;i++){
cin >>num[i];
sum[i] = sum[i - 1] + num[i];
}
cin >>q;
int x,y;
while(q--) {
cin >> x >> y;
cout << sum[y] - sum[x - 1] << endl;
}
return 0;
}

## 最大的平均值

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
using namespace std;
const double eps=1e-6;
const int N = 1e5 + 10;
int n,m;
double a [N],sum [N],l,r;

int check(double mid){
double minn = 0;
for (int i=1;i<=n;i++)
sum[i] = sum[i-1] + a[i] - mid;
for(int i=m;i<=n;i++) {//假设第i个数是尾巴
minn = min(minn, sum[i - m]);
if (sum[i] > minn)
return 1;
}
return 0;
}
int main(){
cin >>n>>m;
for (int i=1;i<=n;i++) {
cin >> a[i];
r = max(r, a[i]);
}
while (r-l>=eps){
double mid = (l+r)/2;
if (check(mid))
l=mid;
else
r=mid;
}
cout <<(int)(r * 1000)<<endl;
return 0;
}

## 门票

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 +7;
const int N = 1e6 + 10;
#define ll long long
ll n,t,a[N],sum[N],ans;
ll q[N];
void merge_sort(int l,int r,ll *a) {
if (l >= r)
return;
int mid = l + r >> 1;
merge_sort(l, mid, a), merge_sort(mid + 1, r, a);

int i = l, j = mid + 1, t = 0;
while (i <= mid && j <= r) {
if (a[i] <= a[j]) {
q[t++] = a[j++];
ans += mid - i + 1;//降序排列，
//求非逆序数
ans %= MOD;
} else
q[t++] = a[i++];
}
while (i <= mid) q[t++] = a[i++];
while (j <= r) q[t++] = a[j++];
for (i = l, j = 0; i <= r; i++, j++)
a[i] = q[j];
}
int main(){
cin >>n >>t;
for(int i=1;i <= n;i++) {
cin >> a[i];
a[i] -= t;
sum[i] = sum[i - 1] + a[i];
}
merge_sort(0,n,sum);
cout <<ans % MOD;
return 0;
}

## 二阶前缀和

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
const int N =1010;
int a[N][N],s[N][N],ans;
using namespace std;
int main() {
int n, r;
cin >> n >> r;
while (n--) {
int x, y, w;
cin >> x >> y >> w;
x++, y++;
a[x][y] += w;
}
for (int i = 1; i <= 1001; i++) {
for (int j = 1; j <= 1001; j++) {
s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
}
for (int i = r; i <= 1001; i++) {
for (int j = r; j <= 1001; j++) {
ans =
max(ans, s[i][j] - s[i - r][j] - s[i][j - r] + s[i - r][j - r]);
}
}
cout << ans;
return 0;
}

## 距离平方和

//
// Created by abner on 2024/3/6.
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll ans,sx,sy,n;

int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
ans += (n - 1) * (x * x + y * y) - ((x * sx + y * sy) * 2);
sx += x, sy += y;
}
cout << ans;
return 0;
}

• 11
点赞
• 30
收藏
觉得还不错? 一键收藏
• 0
评论
10-21 1042
04-14 878
06-12 784
10-25 1054
03-22 1154
06-27 1313
02-26 1036
08-03 361
07-30 422
08-04 175
08-01 538
08-04 490
08-04 139

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。