数列极差问题(the problem of sequence biggest difference)

#include <iostream>
#include <string>
#include <list>  //we use the list  container , because it is easy to sort and get the biggest number and smallest number
using namespace std;

void sequenceBiggestLess(list<int> a, list<int> b)
{
a.sort();
b.sort();
int temp1, temp2;
while(a.size()!=1)
{
//get the biggest number
temp1 = a.front();
a.pop_front();
temp2 = a.front();
a.pop_front();
temp1 = temp1 * temp2 + 1;
a.push_back(temp1);
a.sort();

//get the smallest number
temp1 = b.back();
b.pop_back();
temp2 = b.back();
b.pop_back();
temp1 = temp1 * temp2 + 1;
b.push_back(temp1);
b.sort();
}
temp1 = a.front();
temp2 = b.front();
cout << "biggest value: "<< temp1-temp2;
}

int main()
{
list<int> a, b;
cout << "please input the numbers you wanted to calculate\n enter the number of numbers\n";
int n;
cin >> n;
int temp;
for(int i=0; i<n; i++)
{
cin >> temp;
a.push_back(temp);
b.push_back(temp);
}
sequenceBiggestLess(a, b);
return 0;
}

时间: 2024-10-14 04:58:54

数列极差问题(the problem of sequence biggest difference)的相关文章

UVa 1363 (数论 数列求和) Joseph&#39;s Problem

题意: 给出n, k,求 分析: 假设,则k mod (i+1) = k - (i+1)*p = k - i*p - p = k mod i - p 则对于某个区间,i∈[l, r],k/i的整数部分p相同,则其余数成等差数列,公差为-p 然后我想到了做莫比乌斯反演时候有个分块加速,在区间[i, n / (n / i)],n/i的整数部分相同,于是有了这份代码. 1 #include <cstdio> 2 #include <algorithm> 3 using namespace

贪心算法练习:数列极差问题

在黑板上写n个正整数排成的一个数列,进行如下操作:每次擦掉其中的两个数a和b,然后在数列里面加入一个数a*b+1,如此循环往复直到黑板上只剩下一个数,在所有按这种操作方式最后得到的数中,最大的记为max,最小的记min,则该数列的极差定义为m=max-min.输入一个正整数n,然后输入n个正整数构成一个数列.输出这n个正整数构成的数列的极差. 1 #include<stdio.h> 2 #include<stdlib.h> 3 long maxNumber;//整个数组里面的最大的

贪心算法——数列极差

贪心算法的思想就是用局部的最优解,达到最后全局的最优解.贪心算法使用是有限制的,一个问题能不能使用贪心来做,往往我们要对其进行必要的证明.贪心算法策略具有无后向性,也就是当前阶段的状态确定之后,不受后面阶段状态的影响. 现在我们先将一个能使用贪心算法的问题--数列极差. 问题描述:在黑板上写了N个正整数作成的一个数列,进行如下操作:每一次擦去其中的两个数a和b,然后在数列中加入一个数a*b+1,如此下去直至黑板上剩下一个数,在所有按这种操作方式最后得到的数中,最大的max,最小的为min,则该数

「一本通 1.1 练习 1」数列极差

题目传送门 解题思路 这题也是典型的贪心算法题. 对于这个问题 先通过实例来认识问题所描述的计算过程. 令\(N=3\),取数列\(3,5,7\) 可能有下面三种情况 \((3×5+1)×7+1=113\) \((3×7+1)×5+1=111\) \((5×7+1)×3+1=109\)? 由此可见先运算小数据的到的是最大值,先运算大数据得到的是最小值.? 故针对此问题可采用贪心算法,下面验证其合理性: 不妨假设\(3\)个数\(a<b<c\). 则有以下几种组合计算结果: \(1.(a×b+1

数列极差

题目描述 思路 代码 #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int n, arr[50005], save[50005]; int tmp[50005]; long long maxx, minn; const int inf = 0x7fffffff; inline int read() { int s = 0, f = 1; char ch = g

[LeetCode&amp;Python] Problem 530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

hdu-5496 Beauty of Sequence(递推)

题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 813    Accepted Submission(s): 379 Problem Description Sequence is beautiful and the beauty of an integer sequence is def

[C7] Andrew Ng - Sequence Models

About this Course This course will teach you how to build models for natural language, audio, and other sequence data. Thanks to deep learning, sequence algorithms are working far better than just two years ago, and this is enabling numerous exciting

SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operations with int64 type, all Delphi solutions for the problem 455 (Sequence analysis) run much slower than the same code written in C++ or Java. We do not gua