2019.08.10 shell programming problem written summary

1, the sum of two numbers next to the smallest difference array

Methods: Violence Act

Note that the title given range of values ​​will overflow an int, long long used here

#include<iostream>
#include<vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<long long>temp;
vector<long long>result;
vector<long long>ret;
for (int i = 0; i < n; ++i){
long long j;
cin >> j;
temp.push_back(j);
}
long long m = (temp[1] - temp[0]) > 0 ? (temp[1] - temp[0]) : -(temp[1] - temp[0]);
ret.push_back(temp[0]);
ret.push_back(temp[1]);
result.push_back(m);
for (int i = 1; i < n - 1; ++i){
long long k = (temp[i + 1] - temp[i]) > 0 ? (temp[i + 1] - temp[i]) : -(temp[i + 1] - temp[i]);
if (k < result[result.size() - 1]){
result.push_back(k);
ret.push_back(temp[i]);
ret.push_back(temp[i + 1]);
}
}
cout << ret[ret.size() - 2] << " " << ret[ret.size() - 1] << endl;
}

 

2, the length of the longest sub-sequence of strictly increasing

Methods: Dynamic Programming

//dp
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int binary(int a[], int number, int low, int high){
int mid;
while (low <= high){
mid = (low + high) / 2;
if (number >= a[mid])
low = mid + 1;
else
high = mid - 1;
}
return low;
}
int dp(int a[], int n){
int len;
int*list = new int[n + 2]();
int count = 1;
list[count] = a[1];
for (int i = 2; i <= n; i++){
if (list[count] > a[i]){
len = binary(list, a[i], 1, count);
list[len] = a[i];
}
else if (list[count] < a[i])
list[++count] = a[i];
}
return count;
}
int main(){
int n;
cin >> n;
int*a = new int[n + 1]();
for (int i = 1; i <= n; i++){
cin >> a[i];
}
cout << dp(a, n) << endl;
return 0;
}

 

3, weightlifting, 90% of the body weight (array number of a representative body weight, light only when a person's weight is equal to more than 90% of the weight of a person's weight in order to compete, seeking a total of how many games)

Methods: Violence Act

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int N;
cin >> N;
vector<int>Wei;
int count = 0;
for (int i = 0; i < N; ++i){
int weight;
cin >> weight;
Wei.push_back(weight);
}
sort(Wei.begin(), Wei.end());
for (int i = 0; i < N-1; ++i){
for (int j = i + 1; j < N; ++j){
if(double(Wei[i]) >= 0.9*Wei[j])
++count;
}
}
cout << count << endl;

return 0;
}

 

4, adjustment of the array (the only increase in a certain position), so that after the first increment decrement array, or a increment or a decrement, and then seek to increase the minimum number of

 method:

Greedy, enumerate the middle of the turning point, and then from left to right and right to left to spend updated;
If the current element is smaller than the previous element, then the current minimum cost is greedy freshman than the previous one element;
The same is true traverse forward from behind, and then finally a minimum value.

#include <iostream>
#include <algorithm>
// # the include <bits / STDC ++ H.> #include <bits / STDC ++ H.> contains all the header files currently contained in c ++
// eg: #include <iostream> # the include <cstdio> #include <the fstream> #include <algorithm> #include <the cmath>
// # the include <the deque> #include <Vector> #include <Queue> #include <String> #include <CString> #include <Map > #include <Stack> #include <the SET>
// written test platform can be used to facilitate, VS does not support general G ++ 4.4 or more to support the header files. But will slow the build
the using namespace STD;
const int MAXN = 5E3 + 233;
const Double EPS = 1E-. 8;
typedef Long Long LL;
int T, n-, m, SUM, ANS;
LL A [MAXN];
LL Lmax of [ MAXN], Rmin of [MAXN];
LL Lsum [MAXN], Rsum of [MAXN];



Lsum [0] = 0;
for (int I =. 1; I <n-; I ++)
{
Lmax of [I] = max (Lmax of [I -. 1] +. 1, A [I]); // get give the larger elements in the array is incremented from left to right where the minimum value required for that, so as to satisfy increments.
Lsum [i] = Lsum [i - 1] + Lmax [i] - a [i]; // increment calculation
}
Rmin of [n--. 1] = A [n--. 1];
Rsum of [n--. 1] = 0 ;

for (int i = n - 2; i >= 0; --i)
{
Rmin[i] = max(Rmin[i + 1] + 1, a[i]);
Rsum[i] = Rsum[i + 1] + Rmin[i] - a[i];
}
LL ans = (1LL)*INT_MAX;
LL tmp;
for (int i = 0; i<n; ++i)
{
if (Lmax[i] > Rmin[i])
{
tmp = Lsum[i] + Rsum[i] - Rmin[i] + a[i];
}
else if (Lmax[i] < Rmin[i])
{
tmp = Lsum[i] + Rsum[i] - Lmax[i] + a[i];
}
else
{
tmp = Lsum[i] + Rsum[i] - Lmax[i] + a[i];
}
ans = min(ans, tmp);
}
return ans;
}
void input()
{
//freopen("in.txt","r",stdin);
cin >> n;
for (int i = 0; i<n; ++i)
{
cin >> a[i];
}
}
void solve()
{
cout << minCostIncThenDec(n) << endl;
}
int main()
{
input();
solve();
return 0;
}

Guess you like

Origin www.cnblogs.com/giraffeqiu/p/11334113.html