VP Codeforces Round 944 (Div 4)

感受:

A~G 其实都不难,都可以试着补起来。 H看到矩阵就放弃了。

A题:

思路:

打开编译器

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
const int N = 1e8;
inline void solve() {
    int a, b; cin >> a >> b;
    if (a > b) swap(a, b);
    cout << a << ' ' << b << endl;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tt; cin >> tt;
    while (tt -- ) solve();
    return 0;
}

B题:

思路:

 思维一点。如果这个字符串不是全都一样的话,那么一定就是输出Yes的,具体在实现。如果在后面遇到过与开头不一样的字符,我们进行交换输出即可。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
const int N = 1e8;
inline void solve() {
    string a; cin >> a;
    if (a.size() == 1) return cout << "NO\n", void();
    char c = a[0];
    for (int i = 1; i < a.size(); i ++ ) {
        if (a[i] != c) {
            swap(a[i], a[0]);
            cout << "YES" << endl;
            cout << a << endl;
            return;
        }
    }
    cout << "NO" << endl;
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tt; cin >> tt;
    while (tt -- ) solve();
    return 0;
}

C题:

思路:

要判断是否相交,即要满足以下条件:(假设我们已经排好序)

1.第一条线段的开头小于第二条线段的开头

2.第一条线段的末尾大于第二条线段的开头

3.第二条线段的末尾大于第一条线段的末尾

排序过程可以想象成将12-1这段拆开来,将环变成线段。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1e8;
inline void solve() {
    PII a, b;
    cin >> a.x >> a.y;
    cin >> b.x >> b.y;
    if (a.x > a.y) swap(a.x, a.y);
    if (b.x > b.y) swap(b.x, b.y);
    if (a > b) swap(a, b);
    if (a.x < b.x && b.x < a.y && a.y < b.y) cout << "YES\n";
    else cout << "NO\n";
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tt; cin >> tt;
    while (tt -- ) solve();
    return 0;
}

 D题:

思路:

我们要尽可能少剪,那么可能是剪一连串的0或者1.

然后我们进行分类讨论

如果开头是0,有0001111这样子的子串,我们是不是还可以少剪一个?

如果开头是1,有1100111这样子的子串,我们还可以少剪一个。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
#define x first
#define y second

using namespace std;
typedef pair<int, int> PII;
const int N = 1e8;

inline void solve() {
    string a; cin >> a;
    int n = a.size();
    if (n == 1) return cout << 1 << endl, void();
    // 001001001
    int cnt = 1;
    for (int i = 1; i < n; i ++ ) {
        if (a[i] != a[i - 1]) cnt += 1;
    }
    if (a[0] == '0') {
        cout << (cnt > 1 ? cnt - 1 : 1) << endl;
    }else {
        cout << (cnt > 2 ? cnt - 1 : cnt) << endl;
    }
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tt; cin >> tt;
    while (tt -- ) solve();
    return 0;
}

E题:

思路:

速度是匀速的,主要问题是确定在哪段,我们直接二分即可。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
#define x first
#define y second

using namespace std;
typedef pair<int, int> PII;
const int N = 1e8;

inline void solve() {
    int n, k, q; cin >> n >> k >> q;
    vector<PII> a(k + 2);
    a[1].first = a[1].second = 0;
    for (int i = 2; i <= k + 1; i ++ ) cin >> a[i].first;
    for (int i = 2; i <= k + 1; i ++ ) cin >> a[i].second;
    while (q -- ) {
        int x; cin >> x;
        int l = 1, r = k + 2;
        while (l + 1 != r) {
            int mid = (l + r) >> 1;
            if (a[mid].first < x) l = mid;
            else r = mid;
        }
        int len = a[r].second - a[l].second;
        int d = a[r].first - a[l].first;
        int t = x - a[l].first;
        int ans = a[l].second + t * len / d;
        cout << ans << ' ';
    }
    cout << endl;
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tt; cin >> tt;
    while (tt -- ) solve();
    return 0;
}

F题: 

思路:

饶有兴趣的是,它是r的总和小于1e5.

所以我们可以直接枚举 x。

那么我们为什么要枚举 x 呢?

因为当我们移动 x 的时候,y的值也在进行变动

x^{2} + y^{2} = r ^{2}

y的值即可以用二分求出来了。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
#define x first
#define y second

using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 9;
inline int fac(int t) {
    int ans = 0;
    for (int x = 0; x < t; x ++ ) {
        int now = t * t - x * x;
        int l = -1, r = t;
        while (l + 1 != r) {
            int mid = (l + r) >> 1;
            if (mid * mid < now) l = mid;
            else r = mid;
        }
        ans += r;
    }
    return ans;
}

inline void solve() {
    int r; cin >> r;
    int ans = fac(r + 1) - fac(r);
    ans *= 4;
    ans -= 4;
    cout << ans << endl;
}
inline void pre_work() {

}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    pre_work();
    int tt; cin >> tt;
    while (tt -- ) solve();
    return 0;
}

G题:

思路:

一个数 x 跟哪些数异或起来会小于等于3?

答案是 x, x ^ 1, x ^ 2, x ^ 3

因为二进制从第三位开始就必须要一模一样了。

交换可以用map存数量,用的时候减去即可。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#define int long long
#define x first
#define y second

using namespace std;
typedef pair<int, int> PII;
const int N = 1e5 + 9;

inline void solve() {
    int n; cin >> n;
    vector<int> a(n + 1);
    map<int, int> cnt;
    for (int i = 1; i <= n; i ++ ) {
        cin >> a[i];
        cnt[a[i]] += 1;
    }
    for (int i = 1; i <= n; i ++ ) {
        int b[4];
        b[0] = a[i], b[1] = a[i] ^ 1, b[2] = a[i] ^ 2, b[3] = a[i] ^ 3;
        sort(b, b + 4);
        for (int j = 0; j < 4; j ++ ) {
            if (cnt[b[j]]) {
                cout << b[j] << ' ';
                cnt[b[j]] -= 1;
                break;
            }
        }
    }
    cout << endl;
}
inline void pre_work() {

}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    pre_work();
    int tt; cin >> tt;
    while (tt -- ) solve();
    return 0;
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值