Codeforces Round 1027 (Div. 3) A-D

A题Problem - A - Codeforces

由题意知道,年份s是个完全平方数,不是完全平方数直接pass。

其次,对于(a+b)^2如何取数,最方便的就是a=0b = \sqrt{s}

注意:可以有前缀0,所以应该用字符串

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
 
typedef long long LL;
 
const int N = 1e5 + 10;
 
int main()
{
	int t;
    cin >> t;
    while (t -- )
    {
        string s;
        cin >> s;
        int x = 1000*(s[0] - '0') + 100*(s[1] - '0') + 10*(s[2] - '0') + s[3] - '0';
        int t = int(sqrt(x));
        if (t * t == x) cout << 0 << " " << t << endl;
        else cout << -1 << endl;
    }   
}

B题Problem - B - Codeforces

题意:通过任意交换可以得到k个匹配的,并且其余不匹配。

观察:需要提供\tfrac{n}{2}-k个0和1使得不匹配,剩下的0 1需要一一成对形成k个匹配

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
 
typedef long long LL;
 
const int N = 1e5 + 10;
 
int main()
{
	int t;
    cin >> t;
    while (t -- )
    {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        int num0 = 0, num1 = 0;
        for (int i = 0; i < s.size(); i ++ )
        {
            if (s[i] == '0') num0 ++;
            else num1 ++ ;
        }

        int m = n / 2;
        if (num0 < m - k || num1 < m - k) cout << "NO" << endl;
        else
        {
            num0 -= m - k;
            num1 -= m - k;
            if (num0 % 2 == 1 || num1 % 2 == 1) cout << "NO" << endl;
            else
            {
                if (num0 / 2 + num1 / 2 == k) cout << "YES" << endl;
                else cout << "NO " << endl;
            }
        }
    }   
}

C题Problem - C - Codeforces

直接贪心,开辟新的数组,第一个放入,遍历原数组,如果这个数>新数组里的数+1,放入新数组

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
typedef long long LL;
 
const int N = 1e5 + 10;
 
int main()
{
	int t;
    cin >> t;
    while (t -- )
    {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i ++ ) cin >> a[i];
        int sum = 1, x = a[0];
        for (int i = 1; i < n; i ++ )
        {
            if (a[i] - x <= 1) continue;
            else
            {
                sum ++;
                x = a[i];
            }
        }
        cout << sum << endl;
    }   
}

D题Problem - D - Codeforces

遍历数组,选定一个可以变位置的数,其余的如何确定矩阵:找最左边,最上边,最右边,最下边的。也就是对x,y分别进行排序,然后判断选中的是不是最大(最小),如果是就下一个取。
对于确定完的矩阵,判断:矩阵大小是否等于n - 1(即是否完全),如果是,则向向下/向左右开辟,哪个小选哪个。如果不是,就是这个矩阵

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> PII; 


const int N = 1e5 + 10;
 
int main()
{
	int t;
    cin >> t;
    while (t -- )
    {
       int n;
       cin >> n;
       vector<PII> a(n);
       vector<LL> x(n), y(n); 
       for (int i = 0; i < n; i ++ )
       {
            cin >> a[i].first >> a[i].second;
            x[i] = a[i].first;
            y[i] = a[i].second;
       }
       sort(x.begin(), x.end());
       sort(y.begin(), y.end());
       LL ans = 1e18;
       if (n == 1)
       {
         cout << 1 << endl;
         continue;
       }
        if (n == 2)
        {
            cout << 2 << endl;
            continue;
        }
       for (int i = 0; i < n; i ++ )
       {
            int xl = 0, yl = 0, xr = n - 1, yr = n - 1;
            LL xx = a[i].first, yy = a[i].second;
            if (x[xl] == xx) xl ++;
            if (y[yl] == yy) yl ++;
            if (x[xr] == xx) xr --;
            if (y[yr] == yy) yr --;
            LL sum;
            LL xll = x[xr] - x[xl] + 1, yll = y[yr] - y[yl] + 1;
            sum = xll * yll;
            if (sum == n - 1) 
            {
                sum = min((xll + 1) * yll, (yll + 1) * xll);
            }
            ans = min(ans, sum);
       }
       cout << ans << endl;
    }   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值