2025-11-23

CF

Problem - 1632C - Codeforces(枚举)(1600)

先加再按位或,一定最优

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;

void solve()
{
    int a,b;
    cin >> a >> b;
    int ans = b-a;
    for (int i = a; i <= b;i++){
        int aa = i | b;
        ans = min(ans, i - a + 1 + aa - b);
    }
    for (int i = 0; i <= b - a;i++){
        int bb = b + i;
        int aa = a | bb;
        ans = min(ans, i + 1 + aa - bb);
    }
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        solve();
    }
}

Problem - 1227D1 - Codeforces(排序)(1600虚高)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=105;
struct node{
    int num, id;
} a[N];

bool cmp1(node a,node b){
    if(a.num!=b.num)
        return a.num > b.num;
    return a.id < b.id;
}

bool cmp2(node a,node b){
    return a.id < b.id;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i].num;
        a[i].id = i;
    }
    int m;
    cin >> m;
    while(m--){
        sort(a, a + n, cmp1);
        int k, pos;
        cin >> k >> pos;
        sort(a, a + k, cmp2);
        cout << a[pos-1].num << endl;
    }
}

构造专题(1300)

Problem - 1902C - Codeforces(构造)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
LL gcd(LL a,LL b){
    return b?gcd(b,a%b):a;
}
LL lcm(LL a,LL b){
    return a/gcd(a,b)*b;
}
LL a[N];

void solve()
{
    map<int,int> mp;
    int n;
    cin >> n;
    for (int i = 0; i < n;i++)
    {
        cin >> a[i];
        mp[a[i]] = 1;
    }
    if(n==1){
        cout << 1 << endl;
        return;
    }
    sort(a, a + n);
    LL g = a[1] - a[0];
    for (int i = 2; i < n;i++){
        g = gcd(g, a[i] - a[i - 1]);
    }
    LL cnt = 0;
    for (int i = 0; i < n;i++){
        cnt += (a[n - 1] - a[i]) / g;
    }
    LL k = 1;
    while(1){//妙
        if(mp[a[n-1]-k*g]==0)
            break;
        k += 1;
    }
    LL ans = 1e18;//注意ans要定义为局部变量
    ans = min(ans, cnt + k);
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        solve();
    }
}
posted @ 2025-11-23 20:28  Seren_blingbling  阅读(2)  评论(0)    收藏  举报