2022.5.12

Codeforces Round #790 (Div. 4)

A - Lucky?

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
int a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int ans = (s[0] - '0') + (s[1] - '0') + (s[2] - '0');
        int ans1 = (s[3] - '0')  + (s[4] - '0') + (s[5] - '0');
        if(ans==ans1)
            cout << "yes\n";
        else
            cout << "no\n";
    }

    return 0;
}

B - Equal Candies

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
int a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin>>n;
        int mmin = INF;
        for (int i = 1; i <= n ;i++)
        {
            cin >> a[i];
            mmin = min(a[i], mmin);
        }
        ll sum = 0;
        for (int i = 1;i<=n;i++)
        {
            sum += a[i] - mmin;
        }
        cout << sum << '\n';
    }

    return 0;
}

C - Most Similar Words

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e9;
string s[60];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for (int i = 1; i <= n;i++)
        {
            cin >> s[i];
        }
        int ans = INF;
        for (int i = 1; i < n;i++)
        {
            for (int j = i + 1; j <= n;j++)
            {
                int res = 0;
                for (int k = 0; k < m;k++)
                {
                    res += abs(s[i][k] - s[j][k]);
                }
                ans = min(ans, res);
            }
        }
        cout << ans << '\n';
    }

    return 0;
}

D - X-Sum

由于n很小直接暴力搜即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=200+10,INF=1e9;
int a[N][N],n,m;
int dx[] = {-1, 1, 1, -1}, dy[] = {1, 1, -1, -1};
bool check(int x,int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        ll ans = -1;
        cin >> n >> m ;
        for (int i = 1;i<=n;i++)
        {
            for (int j = 1; j <= m;j++)
            {
                cin >> a[i][j];
            }
        }
        for (int i = 1; i <= n;i++)
        {
            for (int j = 1; j <= m;j++)
            {
                ll res = 0;
                int cnt = 0;
                for (int k = 0; k < 4;k++)
                {
                    int x = i, y = j;
                    if(x==i&&y==j)
                    {
                        cnt++;
                    }
                    while(check(x,y))
                    {
                        res += a[x][y];
                        x += dx[k], y += dy[k];
                    }
                }
                cnt--;
                res -= cnt * a[i][j];
                ans = max(ans, res);
            }
        }
        cout << ans << '\n';
    }

    return 0;
}

E - Eating Queries

前缀和+二分

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e9;
int a[N];
ll sum[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n,m;
        cin >> n >> m;
        memset(sum,0, sizeof sum);
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
        }
        sort(a + 1, a + 1 + n);
        for (int i = n; i >= 1; i--)
        {
            sum[i]=sum[i+1]+a[i];
        }
        reverse(sum + 1, sum + 1 + n);
        while (m--)
        {
            ll x;
            cin >> x;
            if (x > sum[n])
                cout << "-1\n";
            else
            {
                int cnt = lower_bound(sum + 1, sum + 1 + n, x)-(sum);
                cout << cnt << '\n';
            }
        }
    }

    return 0;
}

F - Longest Strike

去重+map记录,边循环边判断,更新最大值

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e9;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        map<int, int> vis;
        int n, m;
        cin >> n >> m ;
        int mmax = 0, maxpos = 0;
        vector<int> a;
        for (int i = 1; i <= n;i++)
        {
            int x;
            cin >> x;
            if(!vis[x])
                a.push_back(x);
            vis[x]++;
            if(vis[x]>mmax)
            {
                mmax = vis[x];
                maxpos = x;
            }
        }
        if(mmax<m)
        {
            cout << "-1\n";
        }
        else
        {
            sort(a.begin(), a.end());
            int res = -1;
            pii ans = {0, 0};
            int pos = 0;
            int sz = a.size();
            for (int i = 1; i < sz;i++)
            {
                if(a[i]==a[i-1]+1&&vis[a[i-1]]>=m&&vis[a[i]]>=m)
                {
                    int len = a[i] - a[pos];
                    if(len>res)
                    {
                        res = len;
                        ans = {a[pos], a[i]};
                    }
                }
                else
                    pos = i;
            }
            if(ans.first==0&&ans.second==0)
            {
                ans = {maxpos, maxpos} ;
            }
            cout << ans.first << ' ' << ans.second << '\n';
        }
    }
    return 0;
}

G - White-Black Balanced Subtrees

简单DFS,每次返回子树白色和黑色点的个数,如果相同cnt++,最后判断以1为根节点的数颜色是否相等即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=4e3+10,INF=1e9;
vector<int> p[N];
map<int, char> mp;
ll cnt = 0;
bool vis[N];
pii dfs(int x)
{
    pii sum;
    if(mp[x]=='W')
        sum = {1, 0};
    else
        sum = {0, 1};
    if(!x)
        return sum;
    for (int i = 0, j = p[x].size(); i < j;i++)
    {
        int son = p[x][i];
        if(!vis[son])
        {
            vis[son] = 1;
            pii res = dfs(son);
            if(res.first==res.second)
               cnt++;
            sum = {sum.first + res.first, sum.second + res.second};
        }
    }
    return sum;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        cnt = 0;
        int n;
        cin>>n;
        for (int i = 0; i <= n;i++)
        {
            p[i].clear();
            vis[i] = 0;
        }
        for (int i = 2; i <= n;i++)
        {
            int x;
            cin>>x;
            p[x].push_back(i);
        }
        string s;
        cin >> s;
        for (int i = 0; i < n;i++)
        {
            mp[i + 1] = s[i];
        }
        pii res=dfs(1);
        if(res.first==res.second)
            cnt++;
        cout << cnt << '\n';
    }

    return 0;
}

H1 - Maximum Crossings (Easy Version)

求逆序对加了个等于

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e3+10,INF=1e9;
int a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
        }
        int cnt = 0;
        for (int i = 2; i <= n;i++)
        {
            for (int j = 1; j < i;j++)
            {
                if(a[j]>=a[i])
                    cnt++;
            }
        }
        cout << cnt << '\n';
    }

    return 0;
}

H2 - Maximum Crossings (Hard Version)

归并排序求逆序对,也可以树状数组

#include<iostream>
using namespace std;
const int maxn=1e6+10;
int a[maxn],tmp[maxn];
long long cnt;
void mergesort(int l,int r)
{
    if(l>=r) return ;
    int mid=l+r>>1;
    mergesort(l,mid);
    mergesort(mid+1,r);
    int k=0,i=l,j=mid+1;
    while(i<=mid&&j<=r)
    {
        if(a[i]<a[j]) tmp[k++]=a[i++];
        else
        {
            tmp[k++]=a[j++];
            cnt+=mid-i+1;
        }
    }
    while(i<=mid) tmp[k++]=a[i++];
    while(j<=r) tmp[k++]=a[j++];
    for(int i=l,j=0;i<=r;j++,i++) a[i]=tmp[j];
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        cnt = 0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        mergesort(1,n);
        cout << cnt << '\n';
    }
    return 0;
}

树状数组

#include<bits/stdc++.h>
const int N=200005;
using namespace std;
typedef long long ll;
ll a[N],d[N],tree[N],n;
int lowbit(int x)
{
	return x&-x;
}
void add(int x)
{
	while(x<=n)
	{
		tree[x]++;
		x+=lowbit(x);
	}
}
ll sum(int x)
{
	ll res=0;
	while(x>=1)
	{
		res+=tree[x];
		x-=lowbit(x);
	}
	return res;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        ll ans=0;
        scanf("%lld",&n);
        for (int i = 0; i <= n;i++)
            tree[i] = 0;
        for (int i = 1; i <= n; i++)
            scanf("%lld", &a[i]);
        for(int i=1;i<=n;i++)
        {
            add(a[i]);
            ans+=i-1-sum(a[i]-1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
posted @ 2022-05-12 22:18  menitrust  阅读(24)  评论(0编辑  收藏  举报