Codeforces Round #536 (Div. 2)

本文解析了CodeForces平台上的三道算法题:A题通过检查矩阵中'X'字符的分布来计数,B题利用排序和贪婪策略解决资源分配问题,C题通过排序和配对策略达到平方和最小化的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://codeforces.com/contest/1106/problem/A

每次选取一个‘x’ 判断四角是不是’x’

#include <iostream>
#include <fstream>
using namespace std;
char matr[505][505];
bool check(int x,int y)
{
    for(int i=-1; i<2; i++)
        for(int j=-1; j<2; j++)
            if(abs(i)==1&&abs(j)==1)
                if(matr[i+x][j+y]!='X')
                    return false;
    return true;
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=0; i<n; i++)
            cin>>matr[i];
        int ans=0;
        for(int i=0; i<n; i++)
            for(int j=0 ; j<n ; j++)
                if(matr[i][j] == 'X' )
                    if( check(i,j) )
                        ans++;
        cout<<ans<<endl;
    }
}

https://codeforces.com/contest/1106/problem/B

排序

#include <bits/stdc++.h>
using namespace std;
const long long maxn=1e5;
struct node
{
    long long x,y,z;
} arr[maxn],tag[maxn];
bool cmp(node a,node b)
{
    return a.y<b.y;
}
int main()
{
    long long n,m;
    while(cin>>n>>m)
    {
        for(long long i=0; i<n; i++)
        {
            cin>>arr[i].x;
        }
        for(long long i=0; i<n; i++)
        {
            cin>>arr[i].y;
            tag[i].y=arr[i].y;
            tag[i].z=i;
        }
        sort(tag,tag+n,cmp);
        long long mx=0,x,y,s;
        for(long long i=0; i<m; i++)
        {
            cin>>y>>x;
            y--;
            long long ans=0;
            if(arr[y].x < x)
            {
                // cout<<"asd  "<<endl;
                ans+=arr[y].x*arr[y].y;
                x-=arr[y].x;
                arr[y].x=0;
                long long mex;
                if(mx<n)
                {
                    mex=tag[mx].z;
                    while( x>arr[mex].x&&mx<n)
                    {
                        ans+=arr[mex].x*arr[mex].y;
                        x-=arr[mex].x;
                        arr[mex].x=0;
                        mx++;
                        mex=tag[mx].z;
                    }
                    if(mx<n)
                    {
                        ans+=x*arr[mex].y;
                        arr[mex].x-=x;
                        cout<<ans<<endl;
                    }
                    else
                    {
                        mx=n+10;
                        cout<<0<<endl;
                    }
                }
                else
                    cout<<"0"<<endl;
            }
            else
            {
                arr[y].x-=x;
                cout<<arr[y].y*x<<endl;
            }
            // for(long long j=0;j<n;j++)
            //cout<<arr[j].x<<' ';cout<<endl;
        }




    }
}

https://codeforces.com/contest/1106/problem/C

排序- - 大小 不想写
平方和最小 肯定是让每组数的大小尽可能平均 - -。
排序 两两组合就是最优 = = 可以证明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值