 fhyxz

624 Bebebella Susanna  fhyxz2  RSqwq yxc的小迷妹 Weiqt deep-coding SUN._5  fhyxz
2天前

### 题目描述

A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.

By the military charter the soldiers should stand in the order of non-increasing of their height. But as there’s virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.

For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.

Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.

Input
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, …, an (1 ≤ ai ≤ 100) the values of the soldiers’ heights in the order of soldiers’ heights’ increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, …, an are not necessarily different.

Output
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.

#### 样例

Examples

input
4
33 44 11 22
output
2

input
7
10 10 58 31 63 40 76
output
10

---------------------------------------
Note

In the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).

In the second sample the colonel may swap the soldiers in the following sequence:

(10, 10, 58, 31, 63, 40, 76)
(10, 58, 10, 31, 63, 40, 76)
(10, 58, 10, 31, 63, 76, 40)
(10, 58, 10, 31, 76, 63, 40)
(10, 58, 31, 10, 76, 63, 40)
(10, 58, 31, 76, 10, 63, 40)
(10, 58, 31, 76, 63, 10, 40)
(10, 58, 76, 31, 63, 10, 40)
(10, 76, 58, 31, 63, 10, 40)
(76, 10, 58, 31, 63, 10, 40)
(76, 10, 58, 31, 63, 40, 10)


### 算法1

#### C++ 代码

#include <bits/stdc++.h>

using namespace std;

int a;
int mins=110,maxs=0,imin,imax;

int main()
{
int n;
cin >> n;

for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);

for (int i = 1; i <= n; i ++ )
{
if (a[i] > maxs)
{
maxs = a[i];
imax = i;
}
}

for (int i = n; i >= 1; i -- )
{
if (a[i] < mins)
{
mins = a[i];
imin = i;
}
}

int ans = imax - 1 + n - imin; //ans = 往左移次数+往右移次数

if (imax > imin) cout << ans - 1 << endl; //两者在移动过程中有交换，则答案数-1
else cout << ans << endl; //否则直接输出即可

return 0;
}


fhyxz
5天前

### 题目描述

0≤n≤1000

#### 样例

给定 nums = [2, 3, 5, 4, 3, 2, 6, 7]。



### 算法1

#### C++ 代码

class Solution {
public:
int cnt = {0};
int duplicateInArray(vector<int>& nums) {

int n = nums.size();

for (auto i : nums)
{
if (i < 0 || i >= n)
return -1;
}

for (int i = 0; i < n; i ++ )
{
if (nums[i] >= 0) cnt[nums[i]] ++;
}

for (int i = 0; i < n; i ++ )
{
if (cnt[i] > 1) return i;
}

return -1;
}
};


fhyxz
6天前

### 题目描述

#### 样例

输入样例：
123 456 8

1103


### 算法1

#### C++ 代码

#include <bits/stdc++.h>

using namespace std;

char a[] = {'0','1','2','3','4','5','6','7','8','9','A'};

void d_to(int x, int m)
{
if (x==0) return; //递归出口
d_to(x/m,m);
cout << a[x%m]; //输出余数
}

int main()
{
int a,b,m;
cin >> a >> b >> m;

int x = a + b;

if (x == 0)
{
cout << 0 << endl;
return 0;
}

d_to(x,m);

cout << endl;

return 0;

}


fhyxz
6天前

### 题目描述

“答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于 PAT 的“答案正确”大派送 —— 只要读入的字符串满足下列条件，系统就输出“答案正确”，否则输出“答案错误”。

#### 样例

输入样例：
10
PAT
PAAT
AAPATAA
AAPAATAAAA
xPATx
PT
Whatever
APAAATAA
APT
APATTAA

YES
YES
YES
YES
NO
NO
NO
NO
NO
NO


### 模拟

#### C++ 代码

#include <bits/stdc++.h>

using namespace std;

int main()
{
int n;
cin >> n;

while (n--)
{
string s;
cin >> s;

bool ans = true;

//只能有一个P一个T，中间末尾和开头可以随便插入A。
//但是必须满足开头的A的个数 * 中间的A的个数 = 结尾的A的个数，
//而且P和T之间不能没有A～

for (int i = 0; i < s.size(); i++)
{
//cout << s[i] << endl;

if (s[i] != 'P' && s[i] != 'A' && s[i] != 'T')
{
ans = false;
}

}

int p = count(s.begin(), s.end(), 'P');
int a = count(s.begin(), s.end(), 'A');
int t = count(s.begin(), s.end(), 'T');

int pa = s.find("PA");
int at = s.find("AT");

if (p != 1 || t != 1 || a == 0 || pa == -1 || at == -1)
{
ans = false;
}

if (ans == true)
{
int pp = s.find('P');
int tt = s.find('T');

int zuoa=0, mida=0, youa=0;

for (int i = 0; i < pp; i ++ )
{
if (s[i] == 'A') zuoa ++;
}

for (int i = pp; i != tt; i ++ )
{
if (s[i] == 'A') mida ++;
}

for (int i =tt; i < s.size(); i ++ )
{
if (s[i] == 'A') youa ++;
}

if (zuoa * mida != youa) ans = false;
}

if (ans == true) cout << "YES";
if (ans == false) cout << "NO";

if (n != 0) cout << endl;

}

return 0;
}


fhyxz
7天前

### 题目描述

#### 样例

输入样例：
1234567890987654321123456789

yi san wu


### 算法1

#### C++ 代码

#include <bits/stdc++.h>

using namespace std;

const int N = 10e6+10;

string s;

int main()
{
cin >> s;

int sum = 0;
for (int i = 0; i < s.size(); i ++ )
{
sum += (s[i]-'0');
}

string str = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};

string ans = to_string(sum);
int l = ans.size();

for (int i = 0; i < l; i ++ )
{
cout << str[(ans[i] - '0')];
if (i != l-1) cout << ' ';
}

cout << endl;

return 0;
}


fhyxz
7天前

### 题目描述

#### 样例

输入样例:
6 2
1 2 3 4 5 6

5 6 1 2 3 4


### 算法1

#### C++ 代码

#include <bits/stdc++.h>

using namespace std;

int a;

int main()
{
int n,m;
cin >> n >> m;

for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);

if (m > n) m = m % n;

for (int i = n-m+1; i <= n; i ++ ) cout << a[i] << ' ';
for (int i =1; i <= n-m; i ++ )
{
if (i<n-m) cout << a[i] << ' ';
else cout << a[i] << endl;
}

return 0;
}


fhyxz
7天前

### 题目描述

#### 样例

输入样例 1：
234

BBSSS1234


### 算法1

#### C++ 代码

#include <bits/stdc++.h>

using namespace std;

int main()
{
int n;
cin >> n;

//获取百位并输出
int bai = n/100;
n = n%100;
while (bai--) cout << 'B';

//获取十位并输出
int shi = n/10;
n = n%10;
while (shi--) cout << 'S';

//按格式输出个位
for (int i = 1; i <= n; i ++ ) cout << i;
puts("");

return 0;
}


fhyxz
9天前
//这里填你的代码^^
#include <bits/stdc++.h>

using namespace std;

const int N = 510;
int n;
int w[N][N], f[N][N];

int main()
{
cin >> n;

for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= i; j ++ )
cin >> w[i][j];

for (int i = 1; i <= n; i ++ ) f[n][i] = w[n][i];

for (int i = n - 1; i ; i -- )
for (int j = 1; j <= i; j ++ )
f[i][j] = max(f[i+1][j] + w[i][j], f[i+1][j+1] + w[i][j]);

cout << f << endl;

return 0;
}
//注意代码要放在两组三个点之间，才可以正确显示代码高亮哦~


fhyxz
11天前
//这里填你的代码^^
#include <cstring>
#include <iostream>

using namespace std;

const int N = 200003, null = 0x3f3f3f3f;

int h[N];

int find(int x)
{
int t = (x % N + N) % N;
while (h[t] != null && h[t] != x)
{
t ++ ;
if (t == N) t = 0;
}
return t;
}

int main()
{
memset(h, 0x3f, sizeof h);

int n;
scanf("%d", &n);

while (n -- )
{
char op;
int x;
scanf("%s%d", op, &x);
if (*op == 'I') h[find(x)] = x;
else
{
if (h[find(x)] == null) puts("No");
else puts("Yes");
}
}

return 0;
}

//注意代码要放在两组三个点之间，才可以正确显示代码高亮哦~


fhyxz
11天前
//这里填你的代码^^
#include <iostream>
#include <algorithm>

using namespace std;

typedef unsigned long long ULL;

const int N = 100010, P = 131;

int n, m;
char str[N];
ULL h[N], p[N];

ULL get(int l, int r)
{
return h[r] - h[l - 1] * p[r - l + 1];
}

int main()
{
scanf("%d%d", &n, &m);
scanf("%s", str + 1);

p = 1;
for (int i = 1; i <= n; i ++ )
{
h[i] = h[i - 1] * P + str[i];
p[i] = p[i - 1] * P;
}

while (m -- )
{
int l1, r1, l2, r2;
scanf("%d%d%d%d", &l1, &r1, &l2, &r2);

if (get(l1, r1) == get(l2, r2)) puts("Yes");
else puts("No");
}

return 0;
}

//注意代码要放在两组三个点之间，才可以正确显示代码高亮哦~