当前位置:网站首页>Codeforces Round #707 (Div. 2) C(抽屉原理)
Codeforces Round #707 (Div. 2) C(抽屉原理)
2022-08-08 19:01:00 【Here_SDUT】
题意
给你一个数组,找出四个数,使得其中两个的和等于另外两个的和,输出下标。最多2e5个数,每个数范围为1-2.5e6。
分析
由于数字范围为1-2.5e6,所以两个数字相加最多达到5e6,运用抽屉原理知道,如果有5e6+1个数,那么这些数里一定有数相同。所以我们可以直接暴力合并所有的数,最多合并5e6+1次就会产生至少两个相同的数,直接跳出循环。还有一个坑,本题不能顺序遍历,而是要按间隔遍历,样例8会卡顺序遍历。顺序遍历的缺点是冲突的风险太大了,每次便利的时候,对于内循环的所有结果都不能贡献给答案,因为有一个共同的数。
代码
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// . ' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
//
// .............................................
// 佛祖保佑 一发AC 永无BUG
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 7e6 + 10;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
typedef pair<int, int> PII;
vector<PII> num[maxn];
inline int read() {
int X = 0;
bool flag = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') flag = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
X = (X << 1) + (X << 3) + ch - '0';
ch = getchar();
}
if (flag) return X;
return ~(X - 1);
}
int a[maxn];
int main(int argc, char const *argv[]) {
int n 9;
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j + i <= n; j++) {
int x = a[j] + a[j + i];
num[x].push_back({j, j + i});
for (int k = 0; k < num[x].size(); k++) {
if (num[x][k].first == j || num[x][k].first == j + i ||
num[x][k].second == j || num[x][k].second == j + i)
continue;
puts("YES");
cout << j << ' ' << j + i << ' ' << num[x][k].first << ' '
<< num[x][k].second << endl;
return 0;
}
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = i + 1; j <= n; j++) {
// int x = a[i] + a[j];
// num[x].push_back({i, j});
// for (int k = 0; k < num[x].size(); k++) {
// if (num[x][k].first == i || num[x][k].first == j ||
// num[x][k].second == i || num[x][k].second == j)
// continue;
// puts("YES");
// cout << i << ' ' << j << ' ' << num[x][k].first << ' '
// << num[x][k].second << endl;
// return 0;
// }
// }
// }
puts("NO");
return 0;
}
边栏推荐
猜你喜欢
什么是Shell?从小白到入门你只差一个它
shell九九乘法口诀表
第4讲:SQL语句之DDL类型的数据库定义语言
CAD进阶练习(二)
企业进行知识共享的好处有哪些?
性能优化|从ping延时看CPU电源管理
Performance optimization | CPU power management from the perspective of ping delay
数组!!!
大学生图书馆网页设计模板代码 DIV布局书店网页作业成品 学校书籍网页制作模板 学生简单书籍阅读网站设计成品
Laravel queue consumption instance and timed task add task consumption
随机推荐
ABAP 报表中如何给报表的输入参数增添 F4 Value Help 试读版
文档管理系统对于企业来说有哪些作用?
MogDB学习笔记-从0开始
【761. 特殊的二进制序列】
重读GPDB 和 TiDB 论文引发的 HTAP 数据库再思考
Geometric g6 will carry harmonyos system, a comprehensive upgrade competitiveness of products
商品期货需要多少钱开户。有资金门槛吗?期货开户在哪开安全?
如何在Firewalld中为特定IP地址开放端口
Will ODPS spark on Dataworks process data more efficiently than directly using ODPS SQL?
Ability in general, but it can be large horizontal jump freely?Where is the better?
Is it safe to open an account with Qiniu Business School?Is it reliable to open an account?
run fscript with lua
室外光纤资源管理——可视化管理平台
uniapp父组件使用prop将异步的数据传给子组件
[极客大挑战 2019]BuyFlag&&[HCTF 2018]admin
Architecture Design Fundamentals
软件测试基础笔记
SSM project integration, integrated case
Vue program of web cache problem after packaging
生成验证码工具类