A - 369
Problem Statement
You are given two integers A A A and B B B.
How many integers x x x satisfy the following condition?
Condition: It is possible to arrange the three integers A A A, B B B, and x x x in some order to form an arithmetic sequence.
A sequence of three integers p p p, q q q, and r r r in this order is an arithmetic sequence if and only if q − p q-p q−p is equal to r − q r-q r−q.
Constraints
1 ≤ A , B ≤ 100 1 \leq A,B \leq 100 1≤A,B≤100
All input values are integers.
Input
The input is given from Standard Input in the following format:
A A A B B B
Output
Print the number of integers x x x that satisfy the condition in the problem statement.
It can be proved that the answer is finite.
Sample Input 1
5 7
Sample Output 1
3
The integers x = 3 , 6 , 9 x=3,6,9 x=3,6,9 all satisfy the condition as follows:
When x = 3 x=3 x=3, for example, arranging x , A , B x,A,B x,A,B forms the arithmetic sequence 3 , 5 , 7 3,5,7 3,5,7.
When x = 6 x=6 x=6, for example, arranging B , x , A B,x,A B,x,A forms the arithmetic sequence 7 , 6 , 5 7,6,5 7,6,5.
When x = 9 x=9 x=9, for example, arranging A , B , x A,B,x A,B,x forms the arithmetic sequence 5 , 7 , 9 5,7,9 5,7,9.
Conversely, there are no other values of x x x that satisfy the condition.
Therefore, the answer is 3 3 3.
Sample Input 2
6 1
Sample Output 2
2
Only x = − 4 x=-4 x=−4 and 11 11 11 satisfy the condition.
Sample Input 3
3 3
Sample Output 3
1
Only x = 3 x=3 x=3 satisfies the condition.
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
set<int> S;
if ((a + b) % 2 == 0) S.insert(a + b >> 1);
S.insert(b + b - a), S.insert(a - (b - a));
cout << S.size() << endl;
return 0;
}
B - Piano 3
Problem Statement
Takahashi has a piano with 100 100 100 keys arranged in a row.
The i i i-th key from the left is called key i i i.
He will play music by pressing N N N keys one by one.
For the i i i-th press, he will press key A i A_i Ai, using his left hand if S i = S_i= Si= L, and his right hand if S i = S_i= Si= R.
Before starting to play, he can place both of his hands on any keys he likes, and his fatigue level at this point is 0.
During the performance, if he moves one hand from key x x x to key y y y, the fatigue level increases by ∣ y − x ∣ |y-x| ∣y−x∣ (conversely, the fatigue level does not increase for any reason other than moving hands).
To press a certain key with a hand, that hand must be placed on that key.
Find the minimum possible fatigue level at the end of the performance.
Constraints
1 ≤ N ≤ 100 1 \leq N \leq 100 1≤N≤100
1 ≤ A i ≤ 100 1 \leq A_i \leq 100 1≤Ai≤100
N N N and A i A_i Ai are integers.
S i S_i Si is L or R.
Input
The input is given from Standard Input in the following format:
N N N
A 1 A_1 A1 S 1 S_1 S1
A 2 A_2 A2 S 2 S_2 S2
⋮ \vdots ⋮
A N A_N AN S N S_N SN
Output
Print the minimum fatigue level at the end of the performance.
Sample Input 1
4
3 L
6 R
9 L
1 R
Sample Output 1
11
For example, the performance can be done as follows:
Initially, place the left hand on key 3 3 3 and the right hand on key 6 6 6.
Press key 3 3 3 with the left hand.
Press key 6 6 6 with the right hand.
Move the left hand from key 3 3 3 to key 9 9 9. The fatigue level increases by ∣ 9 − 3 ∣ = 6 |9-3| = 6 ∣9−3∣=6.
Move the right hand from key 6 6 6 to key 1 1 1. The fatigue level increases by ∣ 1 − 6 ∣ = 5 |1-6| = 5 ∣1−6∣=5.
Press key 9 9 9 with the left hand.
Press key 1 1 1 with the right hand.
In this case, the fatigue level at the end of the performance is 6 + 5 = 11 6+5 = 11 6+5=11, which is the minimum possible.
Sample Input 2
3
2 L
2 L
100 L
Sample Output 2
98
Sample Input 3
8
22 L
75 L
26 R
45 R
72 R
81 R
47 L
29 R
Sample Output 3
188
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
std::vector<int> a(n);
std::vector<char> s(n);
for (int i = 0; i < n; i ++)
cin >> a[i] >> s[i];
int res = 1e18;
for (int i = 1; i <= 100; i ++)
for (int j = 1; j <= 100; j ++) {
int lo = i, ro = j, ans = 0;
for (int k = 0; k < n; k ++) {
if (s[k] == 'L') ans += abs(a[k] - lo), lo = a[k];
else ans += abs(a[k] - ro), ro = a[k];
}
res = min(res, ans);
}
cout << res << endl;
return 0;
}
C - Count Arithmetic Subarrays
Problem Statement
You are given a sequence of N N N positive integers A = ( A 1 , A 2 , … , A N ) A=(A_1,A_2,\dots,A_N) A=(A1,A2,…,AN).
Find the number of pairs of integers ( l , r ) (l,r) (l,r) satisfying 1 ≤ l ≤ r ≤ N 1\leq l\leq r\leq N 1≤l≤r≤N such that the subsequence ( A l , A l + 1 , … , A r ) (A_l,A_{l+1},\dots,A_r) (Al,Al+1,…,Ar) forms an arithmetic progression.
A sequence ( x 1 , x 2 , … , x ∣ x ∣ ) (x_1,x_2,\dots,x_{|x|}) (x1,x2,…,x

最低0.47元/天 解锁文章
1730

被折叠的 条评论
为什么被折叠?



