Codeforces Round #703 (Div. 2)个人总结

A. Shifting Stacks
https://codeforces.com/contest/1486/problem/A
题意:
有一个操作对于1<=i<n;
if(a[i]>0)a[i]–,a[i+1]++;
给你一个序列a[],长度为n,是否能够构成一个严格上升数组
即:a[i]<a[i+1] (1<=i<n)
思路:
贪心、
对于每一位至少为i-1;
每次将第i位多的往后添加就行
参考代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#include<unordered_map>
#define LL long long
using namespace std;
const int N = 110;
LL inf = 1e9;
void solve()
{
	int n; scanf("%d", &n);
	bool key = true;
	LL s = 0;
	for (int i = 0; i < n; i++)
	{
		LL a;
		scanf("%lld", &a);
		if (a + s < i)key = false;
		s += (a - i);
	}
	if (key)puts("YES");
	else puts("NO");
}
int main()
{
	int T; scanf("%d", &T);
	while(T--)
	{
		solve();
	}
	return 0;
}

B. Eastern Exhibition
https://codeforces.com/contest/1486/problem/B
题意:
二维平面给定n个点求到这n个点曼哈顿距离相等的点的数量;
思路:
假设点为(x,y) ;
曼哈顿距离
在这里插入图片描述
可以对x轴y轴分开看
对于给定一个序列a[],长度为n;求在这里插入图片描述
直接是结论中点;
1.先给a【】从小到大排序
2当n为奇数x取n的中间项
3.当n为偶数中间两项之间
在这里插入图片描述

在这里插入图片描述
参考代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#include<unordered_map>
#define LL long long
using namespace std;
const int N = 10010;
LL row[N], col[N];
void solve()
{
	int n; scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%lld%lld", &row[i], &col[i]);
	sort(row + 1, row + 1 + n);
	sort(col + 1, col + 1 + n);
	
	if (n & 1)printf("1\n");
	else
	{
		int mid = n >> 1;
		printf("%lld\n", (row[mid + 1] - row[mid] + 1) * (col[mid + 1] - col[mid] + 1));
	}
}
int main()
{
	int T; scanf("%d", &T);
	while(T--)
	{
		solve();
	}
	return 0;
}

C
https://codeforces.com/contest/1486/problem/C2
题意:
交互题 二分 分块 分治,,,
一般和分有关关键在思维具体二分什么这个题具体二分l~r 到第二大数的距离
参考代码;

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#include<unordered_map>
#define LL long long
using namespace std;
const int N = 2e5+10;
int n;
int a[N];
int query(int l, int r)
{
	int tmp;
	cout << "? " << l << " " << r << "\n";
	fflush(stdout);
	cin >> tmp;
	return tmp;
}
void write(int x)
{
	cout << "! " << x << endl;
}
int main()
{
	cin >> n;
	int key = query(1, n);
	int l, r;
	if (key == 1 || key == n)
	{
		if (key == 1)l = 2, r = n;
		else l = 1, r = n - 1;
	}
	else
	{
		if (query(1, key) == key)l = 1, r = key - 1;
		else l = key + 1, r = n;
	}
	if (l>key)
	{
		int res = l;
		while (l <= r)
		{
			int mid = (l + r) / 2;
			if (query(key, mid) == key)r = mid - 1, res = mid;
			else l = mid + 1;
		}
		write(res);
	}
	else
	{
		int res = l;
		while (l <= r)
		{
			int mid=(l + r) >> 1;
			if (query(mid,key) == key)l = mid + 1, res = mid;
			else r = mid - 1;
		}
		write(res);
	}

	return 0;

}

D. Max Median
https://codeforces.com/contest/1486/problem/D
题意:
1,.给你一个长度为n(1≤n≤2e5)的数组

要你求至少长度为k(1<=k<=n)的连续数组的中位数的最大值
2.给定一个序列a[],长度为n;给定一个数k;
定义一个
Amedian in an array of length n, median=n/2取下界;
For example: median([1,2,3,4)= 2, median([3,2,1])= 2 , median([2,1,2,1])= 1.
要找出序列a[]中大于连续k段最大的median;
思路:
感觉是没一点思路
数据小且median 具有二分性质
在这就二分答案+前缀和技巧
首先假设数组只有1和-1的元素

那么怎么判断这个答案为1还是-1

可以利用前缀和与前缀和的前缀的最小值

来计算答案是1还是-1

然后观察发现最终的答案可以二分

cek的时候大于等于这个值当作1,否则当作-1

参考代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#include<unordered_map>
#define LL long long
using namespace std;
const int N = 2e5+10;
int n, k;
int a[N], pre_sum[N];
bool cek(int x)
{
	for (int i = 1; i <= n; i++)
		pre_sum[i] = pre_sum[i - 1] + (a[i] >= x ? 1 : -1);
	int minv = 0;
	for (int i = k,j=1; i <= n; i++,j++)
	{
		if (pre_sum[i] - minv > 0)return true;
		minv = min(pre_sum[j], minv);
	}
	return false;
}
int main()
{
	cin >> n >> k;
	int l = N, r = 1;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		l = min(a[i], l);
		r = max(a[i], r);
	}
	int res = l;
	while (l <= r)
	{
		int mid = (l + r) >> 1;
		if (cek(mid))l = mid + 1, res = mid;
		else r = mid - 1;
	}
	printf("%d", res);
	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值