[hihocoder]#1619“共同富裕” && #1620股票价格3

/**
 *	给定一个数组A1, A2, ... AN,每次操作可以从中选定一个元素Ai,把除了Ai之外的所有元素都加1。  

	问最少几次操作可以实现“共同富裕”,即数组中所有元素都相等。

	例如对于[1, 1, 1, 2, 3]经过3步:[1, 1, 1, 2, 3] -> [2, 2, 2, 3, 3] -> [3, 3, 3, 3, 4] -> [4, 4, 4, 4, 4]。
	第一行包含一个整数N。(1 ≤ N ≤ 100000)  
	以下N行包含N个整数A1, A2, ... AN。 (1 ≤ Ai ≤ 100000)
 *
 *
 * 这道题乍一看来很难做,仔细观察后发现其它元素都加一我们是不是可以看做,剩下一个元素减一。(难则化反的思想)
 * 于是这道题就可以转化为每个元素减多少次1,让整个数组的值全部变为最小。
 * 另外注意结果的输出为100000*100000 > int(0x7fffffff  2147483647),故使用longlong
 */

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int N;
	long long RESULT = 0;
	cin >> N;

	int* temp = new int[N];

	for (int i = 0; i < N; ++i)
	{
		cin >> temp[i];
	}

	sort(temp, temp + N);

	for (int i = 0; i < N; ++i)
	{
		RESULT += (temp[i] - temp[0]);
	}

	cout << RESULT <<endl;

	return 0;
}

/**
 *	#include<bits/stdc++.h>这个头文件包含以下等等C++中包含的所有头文件: 
 *
 *
 * 小Hi最近在关注股票,为了计算股票可能的盈利,他获取了一只股票最近N天的价格A1~AN。  

小Hi想知道,对于第i天的股票价格Ai,几天之后股价会第一次超过Ai。  

假设A=[69, 73, 68, 81, 82],则对于A1=69,1天之后的股票价格就超过了A1;对于A2=73,则是2天之后股票价格才超过A2。


对于50%的数据,1 ≤ N ≤ 1000  

对于100%的数据,1 ≤ N ≤ 100000, 1 ≤ Ai ≤ 1000000

如果我们直接遍历来做的话,时间复杂度就是n2,我当时就超时了
所以我们就可以从后往前走,维护一个栈表,只对表尾进行操作,保证表尾为最小的即可。

 */

#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

struct node{
    int val;
    int loc;
    node(int a,int b):val(a),loc(b){};
};


int main()
{
	    int n;
    scanf("%d",&n);
    vector<int> arr(n+10);
    vector<int> res(n+10);
    deque<node> deq;
    for(int i=0;i!=n;++i){
        scanf("%d",&arr[i]);
    }
    if(n==1){
        cout<<-1<<endl;
        return 0;
    }
    deq.push_back(node(arr[n-1],n-1));
    res[n-1]=-1;
    for(int i=n-2;i>=0;--i){
        int temp=arr[i];
        while (!deq.empty()&&deq.back().val<=temp)
            deq.pop_back();
        if(deq.empty())
            res[i]=-1;
        else
            res[i]=deq.back().loc-i;
        deq.push_back(node(temp,i));
    }
    for(int i=0;i!=n;++i){
        printf("%d\n",res[i]);
    }
	return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值