百题计划-2 codeforces1185D Extra Element 暴力
2024-10-21 23:00:28
https://codeforces.com/contest/1185/problem/D
题意:给一个序列,移除一个数然后排序后使得序列成为等差数列,求移除的下标。
解法:
先排序,把所有差值扔到map里,显然,移除一个数最多去掉两个差值,所以map.size-2不能大于1.
然后遍历数组,修改map检查,或遍历map的差值,检查数组均可,这里选择遍历map检查数组更好写。
但有一个坑点,如果数组排序后本身是等差数列,则移除首项,注意!!这里的首项是排序后的,应该输出排序前的id,不能直接输出1.
#include<bits/stdc++.h>
#define sz(v) (int)v.size() using namespace std; typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29); int n;
struct Node{
ll x;
int id;
friend bool operator<(Node A,Node B){
return A.x<B.x;
}
};Node p[maxn];
map<ll,int> mp; int check(ll d){
// 1
int cnt=0;
ll t=p[1].x;
vector<int> v;
for(int i=1;i<=n;i++){
if(p[i].x!=t) cnt++,v.push_back(p[i].id);
else t+=d;
}
if(cnt==0) return p[1].id;
if(cnt==1) return v[0]; // n
cnt=0;t=p[n].x;v.clear();
for(int i=n;i>=1;i--){
if(p[i].x!=t) cnt++,v.push_back(p[i].id);
else t-=d;
}
if(cnt==0) return p[1].id;
if(cnt==1) return v[0]; return -1;
} void solve(){
mp.clear();
for(int i=1;i<=n;i++) cin>>p[i].x,p[i].id=i;
sort(p+1,p+n+1);
for(int i=2;i<=n;i++) mp[p[i].x-p[i-1].x]++;
if(n<=3){
puts("1");return;
}
if(sz(mp)-2>1){
puts("-1");return;
}
for(map<ll,int>::iterator it=mp.begin();it!=mp.end();++it){
int index=check(it->first);
if(index!=-1){
cout<<index<<endl;return;
}
}
puts("-1");
} int main(){
// freopen("in.txt","r",stdin);
while(cin>>n) solve();
return 0;
}
最新文章
- FineUI配置文件
- Python pydoc.py
- CodeForces 743B Chloe and the sequence (递归)
- 关于 微软必应词典客户端(pc) 的案例分析
- C2第四次作业解题报告
- SCAU 07校赛 10317 Fans of Footbal Teams
- IIS 服务器 支持.apk文件的下载
- kvm-GT
- stl源代码剖析:编译器的提前定义位置集设置
- Llinux环境下编译并使用OpenCV
- web之Respone
- charles重复发送一个网络请求&;同时发送N次
- centos 安装sbt
- C#对战小游戏,持续更新(里面暂无内容,标记插眼)
- 每日英语:How to Be a Better Conversationalist
- 回测框架pybacktest简介(二)
- docker 入门 (一)重要概念介绍
- float(浮动)的属性和使用方法
- PostgreSQL的hstore初步学习
- Centos 安装Apache软件
热门文章
- .netcore全局异常处理
- MySQL 中的锁有哪些类型,MySQL 中加锁的原则
- LeetCode-357 统计各位数字都不同的数字个数
- LeetCode-41 缺失的第一个正整数
- switch-声明和类型模式匹配
- Educational Codeforces Round 137 (Rated for Div. 2) - D. Problem with Random Tests
- 文字icon
- HP DC7800 升级CPU出现:Missing or Invalid Processor Microcode Update &mdash;&mdash; 解决方案:更新主板BIOS
- JavaWeb 之 Cookie
- Installing Superset最新版本安装(笔记)