2025.1.18——1300
2025.1.18——1300
A 1300
There are
The distance between two cities
For each city
- the closest city to the city
is the city ; - the closest city to the city
is the city ; - the closest city to the city
is the city ; - the closest city to the city
is the city ; - the closest city to the city
is the city .
The cities are located in such a way that for every city, the closest city is unique. For example, it is impossible for the cities to be situated in points
You can travel between cities. Suppose you are currently in the city
- travel to any other city
, paying coins; - travel to the city which is the closest to
, paying coin.
You are given
Input
The first line contains one integer
Each test case is given in the following format:
- the first line contains one integer
( ); - the second line contains
integers ( ); - the third line contains one integer
( ); - then
lines follow; the -th of them contains two integers and ( ; ), denoting that in the -th query, you have to calculate the minimum number of coins you have to spend to travel from the city to the city .
Additional constraints on the input:
- in every test case, for each city, the closest city is determined uniquely;
- the sum of
over all test cases does not exceed ; - the sum of
over all test cases does not exceed .
B 1300
In this problem, you are initially given an empty multiset. You have to process two types of queries:
- ADD
— add an element equal to to the multiset; - GET
— say whether it is possible to take the sum of some subset of the current multiset and get a value equal to .
Input
The first line contains one integer
Then
C 1300
You are given an integer array
First, you are asked to insert one more integer
Then, you will have to make all elements of the array equal. At the start, you choose a positive integer
What's the smallest number of operations it can take you to make all elements equal, after you choose
Input
The first line contains a single integer
The first line of each testcase contains a single integer
The second line contains
The sum of
D 1300
Vlad found an array
To do this, Vlad can apply the following operation any number of times:
- Extract the first element of the array and insert it at the end;
- Swap that element with the previous one until it becomes the first or until it becomes strictly greater than the previous one.
Note that both actions are part of the operation, and for one operation, you must apply both actions.
For example, if you apply the operation to the array [
- [
]; - [
]; - [
]; - [
].
Vlad doesn't have time to perform all the operations, so he asks you to determine the minimum number of operations required to sort the array or report that it is impossible.
E 1300
Yarik has already chosen
The melody will consist of several combinations of two notes. Yarik was wondering how many pairs of notes
Input
The first line of the input contains one integer
The first line of each test case contains one integer
The next line contains
It is guaranteed that the sum of
------------------------思考------------------------
-
前缀维护信息+二进制/充分性+数学+发现结论+配对优化
A
- 前缀维护信息,区间询问。
- 思考点是如何计算前缀。
B
- 充分性:判断是否可以组成时,枚举每一位进行判断。
- 思考点是判断某位是否可以被组成。
C
- 将一数组元素任意加某一确定数,致所有数相同。加的数为所有数
操作次数最少。 - 之前做过类似的题,但想不到为什么这样思考。自己数学推一下就想明白了。
- 至于
,一开始出了假思路 。实际最大值减某个倍数的最大公约数才是最优解。
D
- 模拟一下,观察发现结论即可。
E
- 除了相同的情况,二进制数中仅有24==42,统计对数,维护信息扫一遍即可。
------------------------代码------------------------
A
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST) \
for (int i = ST; i < VEC.size(); i++) \
cout << VEC[i] << ' '; \
el;
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i];
vector<int> pre(n + 1), suf(n + 2);
auto closest = [&](int u, int r, int l)
{
return abs(a[u] - a[r]) <= abs(a[u] - a[l]);
};
for (int i = 2; i <= n; i++)
{
pre[i] = a[i] - a[i - 1];
if (i == 2 || closest(i - 1, i, i - 2))
pre[i] = 1;
pre[i] += pre[i - 1];
}
for (int i = n - 1; i; i--)
{
suf[i] = a[i + 1] - a[i];
if (i == n - 1 || closest(i + 1, i, i + 2))
suf[i] = 1;
suf[i] += suf[i + 1];
}
// bugv(pre, 1);
int q;
cin >> q;
while (q--)
{
int st, ed;
cin >> st >> ed;
int res = pre[ed] - pre[st];
if (st > ed)
res = suf[ed] - suf[st];
cout << res << endl;
}
}
B
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST) \
for (int i = ST; i < VEC.size(); i++) \
cout << VEC[i] << ' '; \
el;
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
int T = 1;
// cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int q;
cin >> q;
vector<int> x(32);
while (q--)
{
int op, v;
cin >> op >> v;
if (op == 1)
x[v]++;
else
{
auto t = x;
bool f = 1;
for (int i = 0; v >> i; i++)
if (v >> i & 1)
{
int ned = 1, j = i;
for (int j = i; ned && j >= 0; j--, ned <<= 1)
{
int has = min(t[j], ned);
t[j] -= has, ned -= has;
}
if (ned)
f = 0;
}
cout << (f ? "YES" : "NO") << endl;
}
}
}
C
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST) \
for (int i = ST; i < VEC.size(); i++) \
cout << VEC[i] << ' '; \
el;
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++)
cin >> a[i], a[i] += 1e9;
sort(a.begin() + 1, a.end());
int g = 0;
for (int i = 2; i <= n; i++)
g = __gcd(g, a[i] - a[i - 1]);
if (!g)
g = 1;
map<int, bool> has;
for (int i = 1; i <= n; i++)
has[a[i]] = 1;
for (int st = a[n] - g;; st -= g)
if (!has[st])
{
a[0] = st;
break;
}
int res = 0;
for (auto v : a)
res += (a[n] - v) / g;
cout << res << endl;
}
// void _()
// {
// int n;
// cin >> n;
// vector<int> a(n + 1);
// for (int i = 1; i <= n; i++)
// cin >> a[i], a[i] += 1e9;
// sort(a.begin() + 1, a.end());
// int g = 0;
// for (int i = 2; i <= n; i++)
// g = __gcd(g, a[i] - a[i - 1]);
// if (!g)
// g = 1;
// bool f = 1;
// for (int i = 1; i <= n; i++)
// if (a[i] == a[n] - g)
// f = 0;
// a[0] = f ? a[n] - g : a[n] + g;
// int mx = max(a[0], a[n]);
// int res = 0;
// for (auto v : a)
// res += (mx - v) / g;
// cout << res << endl;
// }
D
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST) \
for (int i = ST; i < VEC.size(); i++) \
cout << VEC[i] << ' '; \
el;
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n;
cin >> n;
vector<int> a(n);
int mn = 1e10, id = -1;
for (int i = 0; i < n; i++)
{
int &x = a[i];
cin >> x;
if (x < mn)
mn = x, id = i;
}
bool f = 1;
for (int i = id + 1; i < n; i++)
if (a[i] < a[i - 1])
f = 0;
cout << (f ? id : -1) << endl;
}
E
#include <bits/stdc++.h>
#define int long long //
#define endl '\n' // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST) \
for (int i = ST; i < VEC.size(); i++) \
cout << VEC[i] << ' '; \
el;
void _();
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
int T = 1;
cin >> T;
while (T--)
_();
return 0;
}
void _()
{
int n;
cin >> n;
int res = 0;
map<int, int> cnt;
while (n--)
{
int x;
cin >> x;
res += cnt[x];
if (x == 1)
res += cnt[2];
if (x == 2)
res += cnt[1];
cnt[x]++;
}
cout << res << endl;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!