Loading

AtCoder Beginner Contest 261 A - F 个人题解

传送门

A - Intersection

区间标记一下

#include <iostream>
#include <vector>
using namespace std;
const int maxn = 110;

int main()
{
    vector<int>vis(110, 0);
    for(int i=0; i<2; i++)
    {
        int l, r;
        cin >> l >> r;
        r--;
        for(int i=l; i<=r; i++)
            vis[i]++;
    }
    int ans = 0;
    for(int i=0; i<100; i++) if(vis[i] == 2) ans++;
    cout << ans << endl;
    return 0;
}

B - Tournament Result

暴力一下

#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int maxn = 1010;
string s[maxn];

int main()
{
    int n;
    cin >> n;
    for(int i=0; i<n; i++) cin >> s[i];
    int f = 0;
    for(int i=0; i<n && !f; i++)
    {
        for(int j=i+1; j<n && !f; j++)
        {
            if(s[i][j] == 'D') f = s[j][i] != 'D';
            else f = (s[j][i] ^ 'L' ^ 'W') != s[i][j];
        }
    }
    if(f) cout << "incorrect" << endl;
    else cout << "correct" << endl;
    return 0;
}

C - NewFolder(1)

map 用一下,计算次数

#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
const int maxn = 1010;
string s[maxn];

int main()
{
    int n;
    cin >> n;
    map<string, int>mp;
    for(int i=0; i<n; i++)
    {
        string s;
        cin >> s;
        cout << s;
        if(mp[s]) cout << '(' << mp[s] << ')';
        cout << "\n";
        mp[s]++;
    }
    // cout << endl;
    return 0;
}

D - Flipping and Bonus

\(dp\)

\(dp[i][j]\) 表示第 \(i\) 次投币连续正面次数为 \(j\) 的最优值

两个转移:

  1. 正面:\(dp[i][j] = dp[i-1][j-1] + a[i] + b[j]\)

  2. 反面:\(dp[i][0] = \max_{j=0}^{j<i}(dp[i-1][j])\)

#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 5010;
string s[maxn];
ll a[maxn], b[maxn];
ll dp[maxn][maxn];

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=0; i<m; i++)
    {
        int x, y;
        cin >> x >> y;
        b[x] = y;
    }
    for(int i=0; i<=n; i++)
        for(int j=0; j<=n; j++)
            dp[i][j] = -1;
    dp[0][0] = 0;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=i; j++)
        {
            if(dp[i-1][j-1] != -1) dp[i][j] = dp[i-1][j-1] + a[i] + b[j];
            dp[i][0] = max(dp[i][0], dp[i-1][j]);
        }
    }
    ll ans = 0;
    for(int i=0; i<=n; i++) ans = max(ans, dp[n][i]);
    cout << ans << endl;
    return 0;
}

E - Many Operations

对每个位进行分析

其实每个位原始只有 \(0\)\(1\),经过若干操作后产生变化,抽象地理解成为经过一个函数 \(f(x)\),会变成什么,然后不断维护这个 \(f(x)\) 就好了,每次转换肯定都是 \(O(1)\)

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
int t[maxn], val[maxn];
ll ans[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n, now;
    cin >> n >> now;
    for(int i=0; i<n; i++) cin >> t[i] >> val[i];
    for(int i=0; i<30; i++)
    {
        ll x = now >> i & 1;
        ll a = 0, b = 1;
        for(int j=0; j<n; j++)
        {
            ll bit = val[j] >> i & 1;
            if(t[j] == 1) {a &= bit; b &= bit;}
            else if(t[j] == 2) {a |= bit; b |= bit;}
            else {a ^= bit; b ^= bit;}
            if(x) x = b;
            else x = a;
            ans[j] |= x << i;
        }
    }
    for(int i=0; i<n; i++)
        cout << ans[i] << "\n";
    cout << endl;
    return 0;
}

F - Sorting Color Balls

树状数组

维护逆序对,然后再减去求出相同颜色的逆序对

不断初始化树状数组会导致超时,所以初始化直接减回去就好

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 10;
#define pii pair<int, int>
vector<int>tr(maxn, 0);
int n;

inline int lowbit(int x)
{
    return x & (-x);
}

void add(int x, int val)
{
    for(int i=x; i<=n; i+=lowbit(i))
        tr[i] += val;
}

int query(int x)
{
    int ans = 0;
    for(int i=x; i; i-=lowbit(i))
        ans += tr[i];
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    vector<int>x(n);
    vector<vector<int> >col(n + 1);
    for(int i=0; i<n; i++)
    {
        int c;
        cin >> c;
        col[c].push_back(i);
    }
    for(int i=0; i<n; i++) cin >> x[i];
    ll ans = 0;
    for(int i=0; i<n; i++)
    {
        ans += i - query(x[i]);
        add(x[i], 1);
    }
    for(int i=0; i<n; i++) add(x[i], -1);
    for(int i=1; i<=n; i++)
    {
        int cur = 0;
        for(int a : col[i])
        {
            ans -= cur - query(x[a]);
            add(x[a], 1);
            cur++;
        }
        for(int a : col[i])
            add(x[a], -1);
    }
    cout << ans << endl;
    return 0;
}
posted @ 2022-07-24 16:20  dgsvygd  阅读(65)  评论(0编辑  收藏  举报