codeforces 1185D

博客围绕Codeforces 1185D题目展开,题目要求判断给定数列删除一个元素后,剩余元素能否重排成等差数列,若能则输出删除元素序号,不能则输出 -1。解题思路是先排序,统计相邻元素差的出现次数,再遍历数列计算删除元素后的差及公差,判断是否为等差数列。

题目:http://codeforces.com/problemset/problem/1185/D

D. Extra Element

A sequence a1,a2,…,aka1,a2,…,ak is called an arithmetic progression if for each ii from 11 to kk elements satisfy the condition ai=a1+c⋅(i−1)ai=a1+c⋅(i−1) for some fixed cc.

For example, these five sequences are arithmetic progressions: [5,7,9,11][5,7,9,11], [101][101], [101,100,99][101,100,99], [13,97][13,97] and [5,5,5,5,5][5,5,5,5,5]. And these four sequences aren't arithmetic progressions: [3,1,2][3,1,2], [1,2,4,8][1,2,4,8], [1,−1,1,−1][1,−1,1,−1] and [1,2,3,3,3][1,2,3,3,3].

You are given a sequence of integers b1,b2,…,bnb1,b2,…,bn. Find any index jj (1≤j≤n1≤j≤n), such that if you delete bjbj from the sequence, you can reorder the remaining n−1n−1 elements, so that you will get an arithmetic progression. If there is no such index, output the number -1.

Input

The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — length of the sequence bb. The second line contains nn integers b1,b2,…,bnb1,b2,…,bn (−109≤bi≤109−109≤bi≤109) — elements of the sequence bb.

Output

Print such index jj (1≤j≤n1≤j≤n), so that if you delete the jj-th element from the sequence, you can reorder the remaining elements, so that you will get an arithmetic progression. If there are multiple solutions, you are allowed to print any of them. If there is no such index, print -1.

Examples

input

5
2 6 8 7 4

output

4

input

8
1 2 3 4 5 6 7 8

output

1

input

4
1 2 4 8

output

-1

Note

Note to the first example. If you delete the 44-th element, you can get the arithmetic progression [2,4,6,8][2,4,6,8].

Note to the second example. The original sequence is already arithmetic progression, so you can delete 11-st or last element and you will get an arithmetical progression again.

题意:给一个n个元素的数列,删除一个元素剩下的可不可以随意排列一个等差数列,如果可以,输出删除元素的序号,如果不能,输出-1.

解题思路:先把数组排序,然后算出相邻元素的差bi,统计差出现的次数,然后遍历数列,计算把当前元素删除数列会新增的差和删除的差,计算首项和尾项的差,求得公差,如果公差出现n-2次,则数组是等差数列。

#include<iostream>
#include<queue>
#include<cstring> 
#include<cmath>
#include<map>
#include<algorithm>
#define up(i,x,y) for(i=x;i<=y;i++)  
#define down(i,x,y) for(i=x;i>=y;i--)  
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a<b?a:b
#define MAX(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))
#define MIN(a,b,c) (a<b?(a<c?a:c):(b<c?b:c))
using namespace std;

typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;
const int MAXN = 2750131;
ll T,n,m,i,j;
ll gcd(ll p,ll q)
{return q==0?p:gcd(q,p%q);}


struct point{
	ll data,index;
}a[200005];
bool cmp(point x,point y){
	return x.data<y.data;
}
int main()
{
	map<ll,int>mp;
	cin>>n;
	ll sum=0;
	ll b[200005];
	for(i=0;i<n;i++){
		cin>>a[i].data;
	
		a[i].index=i;
	}
	if(n<4){
		cout<<"1"<<endl;return 0;
	}
	sort(a,a+n,cmp);
	for(i=1;i<n;i++){
		ll s=a[i].data-a[i-1].data;
		b[i]=s;
		mp[s]++;
	}
	ll d,a1,an;
	for(i=0;i<n;i++){
		a1=a[0].data;
		an=a[n-1].data;
		if(i==0){
			a1=a[1].data;
			mp[b[1]]--;
		}
		else if(i==(n-1)){
			an=a[n-2].data;
			mp[b[n-1]]--;
		}
		else{
			ll s1=b[i]+b[i+1];
			mp[s1]++;
			mp[b[i]]--;
			mp[b[i+1]]--;
		}
		if((an-a1)%(n-2)){
			
		}
		else{
			d=(an-a1)/(n-2);
			if(mp[d]==(n-2)){
				cout<<a[i].index+1<<endl;return 0;
			}
		}
		if(i==0){
			mp[b[1]]++;
		}
		else if(i==(n-1)){
			mp[b[n-1]]++;
		}
		else{
			ll s1=b[i]+b[i+1];
			mp[s1]--;
			mp[b[i]]++;
			mp[b[i+1]]++;
		}
	}
	cout<<"-1"<<endl;return 0;
}

 

### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值