首页 > 其他分享 >2025.1.18——1300

2025.1.18——1300

时间:2025-01-19 22:10:08浏览次数:1  
标签:city le 2025.1 10 int 18 contains 1300 define

2025.1.18——1300


A 1300

There are \(n\) cities located on the number line, the \(i\)-th city is in the point \(a_i\). The coordinates of the cities are given in ascending order, so \(a_1 < a_2 < \dots < a_n\).

The distance between two cities \(x\) and \(y\) is equal to \(|a_x - a_y|\).

For each city \(i\), let's define the closest city \(j\) as the city such that the distance between \(i\) and \(j\) is not greater than the distance between \(i\) and each other city \(k\). For example, if the cities are located in points \([0, 8, 12, 15, 20]\), then:

  • the closest city to the city \(1\) is the city \(2\);
  • the closest city to the city \(2\) is the city \(3\);
  • the closest city to the city \(3\) is the city \(4\);
  • the closest city to the city \(4\) is the city \(3\);
  • the closest city to the city \(5\) is the city \(4\).

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 \([1, 2, 3]\), since this would mean that the city \(2\) has two closest cities (\(1\) and \(3\), both having distance \(1\)).

You can travel between cities. Suppose you are currently in the city \(x\). Then you can perform one of the following actions:

  • travel to any other city \(y\), paying \(|a_x - a_y|\) coins;
  • travel to the city which is the closest to \(x\), paying \(1\) coin.

You are given \(m\) queries. In each query, you will be given two cities, and you have to calculate the minimum number of coins you have to spend to travel from one city to the other city.
Input

The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.

Each test case is given in the following format:

  • the first line contains one integer \(n\) (\(2 \le n \le 10^5\));
  • the second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_1 < a_2 < \dots < a_n \le 10^9\));
  • the third line contains one integer \(m\) (\(1 \le m \le 10^5\));
  • then \(m\) lines follow; the \(i\)-th of them contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)), denoting that in the \(i\)-th query, you have to calculate the minimum number of coins you have to spend to travel from the city \(x_i\) to the city \(y_i\).

Additional constraints on the input:

  • in every test case, for each city, the closest city is determined uniquely;
  • the sum of \(n\) over all test cases does not exceed \(10^5\);
  • the sum of \(m\) over all test cases does not exceed \(10^5\).

B 1300

In this problem, you are initially given an empty multiset. You have to process two types of queries:

  1. ADD \(x\) — add an element equal to \(2^{x}\) to the multiset;
  2. GET \(w\) — say whether it is possible to take the sum of some subset of the current multiset and get a value equal to \(w\).
    Input

The first line contains one integer \(m\) (\(1 \le m \le 10^5\)) — the number of queries.

Then \(m\) lines follow, each of which contains two integers \(t_i\), \(v_i\), denoting the \(i\)-th query. If \(t_i = 1\), then the \(i\)-th query is ADD \(v_i\) (\(0 \le v_i \le 29\)). If \(t_i = 2\), then the \(i\)-th query is GET \(v_i\) (\(0 \le v_i \le 10^9\)).


C 1300

You are given an integer array \(a_1, a_2, \dots, a_n\), all its elements are distinct.

First, you are asked to insert one more integer \(a_{n+1}\) into this array. \(a_{n+1}\) should not be equal to any of \(a_1, a_2, \dots, a_n\).

Then, you will have to make all elements of the array equal. At the start, you choose a positive integer \(x\) (\(x > 0\)). In one operation, you add \(x\) to exactly one element of the array. Note that \(x\) is the same for all operations.

What's the smallest number of operations it can take you to make all elements equal, after you choose \(a_{n+1}\) and \(x\)?
Input

The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of testcases.

The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)). All \(a_i\) are distinct.

The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\).


D 1300

Vlad found an array \(a\) of \(n\) integers and decided to sort it in non-decreasing order.

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 [\(4, 3, 1, 2, 6, 4\)], it will change as follows:

  • [\(\color{red}{4}, 3, 1, 2, 6, 4\)];
  • [\(3, 1, 2, 6, 4, \color{red}{4}\)];
  • [\(3, 1, 2, 6, \color{red}{4}, 4\)];
  • [\(3, 1, 2, \color{red}{4}, 6, 4\)].

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 \(n\) notes that he wants to use in his new melody. However, since their integers can be very large, he has written them down as an array \(a\) of length \(n\), then the note \(i\) is \(b_i = 2^{a_i}\). The integers in array \(a\) can be repeated.

The melody will consist of several combinations of two notes. Yarik was wondering how many pairs of notes \(b_i, b_j\) \((i < j)\) exist such that the combination \((b_i, b_j)\) is equal to the combination \((b_j, b_i)\). In other words, he wants to count the number of pairs \((i, j)\) \((i < j)\) such that \(b_i^{b_j} = b_j^{b_i}\). Help him find the number of such pairs.
Input

The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.

The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of the arrays.

The next line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 10^9\)) — array \(a\).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).


------------------------思考------------------------

  • 前缀维护信息+二进制/充分性+数学+发现结论+配对优化


A

  1. 前缀维护信息,区间询问。
  2. 思考点是如何计算前缀。

B

  1. 充分性:判断是否可以组成时,枚举每一位进行判断。
  2. 思考点是判断某位是否可以被组成。

C

  1. 将一数组元素任意加某一确定数,致所有数相同。加的数为所有数 \(gcd\) 操作次数最少。
  2. 之前做过类似的题,但想不到为什么这样思考。自己数学推一下就想明白了。
  3. 至于 \(a[i+1]\) ,一开始出了假思路 \(a[n]+g /a[n]-g\) 。实际最大值减某个倍数的最大公约数才是最优解。

D

  1. 模拟一下,观察发现结论即可。

E

  1. 除了相同的情况,二进制数中仅有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;
}

标签:city,le,2025.1,10,int,18,contains,1300,define
From: https://www.cnblogs.com/jkkk/p/18680381

相关文章

  • 188. 买卖股票的最佳时机 IV
    买卖股票的最佳时机IV类比j为奇数是买,偶数是卖的状态。/***@param{number[]}prices*@return{number}*/​dp[0]:无操作;​dp[1]:第一次买入;​dp[2]:第一次卖出;​dp[3]:第二次买入;​dp[4]:第二次卖出; //2*k+1varmaxProfit......
  • AGC018
    AGC018B题目大意举办一场运动会,有\(N\)人,\(M\)个项目,每个人所有项目都有一个排名,会选择参加排名最高且开设的项目,现在要开设若干项目使得人数最多的项目人数尽可能小,求这个最小值。解题思路考虑贪心。一开始,我们不妨开设所有项目,设人数最多的项目为\(x\)。如果我们不关......
  • [2025.1.19 JavaSE学习]网络编程-2(netstat指令 && TCP补充)
    netstatnetstat-an:可以查看当前主机网络情况,包括端口监听情况和网络连接情况netstat-an|more:可以分页显示在dos控制台执行Listening表示某个端口在监听如果有一个外部程序(客户端)连接到该端口,就会显示一条连接信息PS:netstat-anb,可以发现,8888端口号在上一节程序运行......
  • 洛谷P1807 最长路(拓扑排序)
    题目链接:P1807最长路-洛谷|计算机科学教育新生态题目描述设 G 为有 n 个顶点的带权有向无环图,G  中各顶点的编号为 1 到 n,请设计算法,计算图 GG中 1,n 间的最长路径。输入格式输入的第一行有两个整数,分别代表图的点数 n 和边数 m。第 2 到第 (m+1)......
  • 250118 ABC389总结
    昨天激情地打了1场ABC。A一眼秒了。B一眼秒了。C两眼秒了。D1.5眼秒了,然后发现题读错了,不过问题不大,最后还是秒了。第一发没开longlong见祖宗了。全军复诵:不开longlong见祖宗。E一眼dp,但是我不会dp,所以想了一小下直接去看F了。最后的最后试图码了一下单调队列+暴......
  • AGC018做题笔记
    AtcoderGrandContest018B-SportsFestival题目大意有\(N\)个人参加\(M\)个项目的运动会,这\(M\)可以至多被删去\(M-1\)个。第\(i\)个人会参加序列\(a_i\)中第一个可以被参加的项目\(a_{i,j}\)。现在要使参加人数最多的项目人数最少,求这个最小人数。解题思......
  • 518. 零钱兑换 II
    518.零钱兑换II给你一个整数数组 coins 表示不同面额的硬币,另给一个整数 amount 表示总金额。请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 0 。假设每一种面额的硬币有无限个。 题目数据保证结果符合32位带符号整数。示......
  • 例题_树基础 P5318
    洛谷P5318分析关键词n篇文章m条参考文献引用关系x文章有y参考文献BFS&&DFS结果步骤定义不仅要定义关键词,还要再定义一个容器这里用\(set\)set<int>e[100009];注意要初始化输入输入nmxy这几个关键字计算过程分两步深搜广搜输出先调用函数,在......
  • [2025.1.18 JavaSE学习]IP地址
    ip地址概念:用于唯一标识网络中的每台计算机/主机查看ip地址:ipconfigip地址的表示形式:点分十进制xx.xx.xx.xx,每一个十进制数的范围为0~255ip地址的组成=网络地址+主机地址,比如:192.168.16.69IPv6(16个字节)是互联网工程任务组设计的用于替代IPv4(4个字节)的下一代IP协议(其地......
  • 十分钟写作Day6 1.18
    前言今天没有特定的题目,所以记录记录今天的碎片时光。正文今天一个教我们的新老师第二次来给我们讲课,讲了一下昨天Codeforces的contest2056.here.不过D题是道好题。巧的地方就在于将合法的统计改为总的数量减去不合法的数量的这一个操作能将一个不等式条件改为等式条件......