[codeforces] 暑期训练之打卡题(二)

每个标题都做了题目原网址的超链接


Day11《Given Length and Sum of Digits...

题意:

给定一个数 m 和 一个长度 s,计算最大和最小在 s 长度下,各位数字之和为 m 的值

如果无法生成,则输出两个-1

题解:

需要注意:在输出最大值时,判一下 k 是否为 0

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int len, sum; cin >> len >> sum;
    if (sum == 0 && len != 1 || sum > len * 9)
        cout << -1 << ' ' << -1 << endl;
    else 
    {
        for (int i = len - 1, k = sum; i >= 0; --i) 
        {
            int x = max(0, k - 9 * i);
            if (!x && i == len - 1 && k)x = 1;
            cout << x; k -= x;
        }
        cout << ' ';
        for (int i = len - 1, k = sum; i >= 0; --i) 
        {
            int x = min(9, k);
            cout << x;
            k -= x;
        }
    }
    return 0;
}

Day12《Cheap Travel

题意:

一个 ride 需要 a 卢布,m 个 ride 需要 b 卢布,这两种方案都可以无限制地采用,要完成 n rides 最少需要多少卢布

注:由样例可知,不一定要刚好完成 n rides,可以完成 cnt rides(cnt > n)

题解:

本题我采用的分类讨论:

  1. 当 n <= m  时,一张多程票 b 就可以完成 n rides,故需要比较 a*n 和 b 的大小
  2. 当 n > m 时,比较 a 和 b/m 的价格(b/m 即为 m rides 中的单程价)

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int n, m;
    double a, b;
    while (scanf("%d%d%lf%lf", &n, &m, &a, &b) != EOF)
    {
        int ans;
        if (n <= m) ans = (a * n > b ? b : a * n);
        else if (a >= b / m)//单程a的价格比买多程b的平均单程价格b/m高
        {
            ans = n / m * b;//尽可能购买b
            ans += min(n % m * a, b);
        }
        else ans = n * a;
        printf("%d\n", ans);
    }
    return 0;
}

Day13《BerSU Ball

题意:

给出两个数组,两个元素的差小于等于 1 时才能配对,问两组之间进行配对,求最多对数。

题解:

暴力出奇迹

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    vector<int>g;
    vector<int>b;
    int n, m, i, c, j, k = 0, num = 0;
    cin >> n;
    for (i = 0;i < n;i++)
    {
        cin >> c;
        b.push_back(c);
    }
    cin >> m;
    for (i = 0;i < m;i++)
    {
        cin >> c;
        g.push_back(c);
    }
    sort(g.begin(), g.end());
    sort(b.begin(), b.end());
    for (i = 0;i < n;i++)
    {
        for (j = k;j < m;j++)
        {
            if (abs(b.at(i) - g.at(j)) <= 1)
            {
                num++;
                k = j + 1;
                break;
            }
        }
    }
    cout << num << '\n';
    return 0;
}

Day14《Pashmak and Flowers

题意:

给出一个数组,要找一个最大值一个最小值,求最大值和最小值的差以及可以有多少种组合

题解:

  1. 输入数组,用 sort 排序
  2. 循环,记录最大值的个数,最小值的个数,差值
  3. 如果差值为 0,则可以有 n*(n-1)/2 种组合
  4. 如果不为 0,则将最大值个数和最小值个数相乘输出

坑:记得开 long long !

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 200005;
int a[MAX];

int main()
{
    int n;
    cin >> n;
    for (int i = 0;i < n;i++) 
        cin >> a[i];
    sort(a, a + n);
    long long int  cnt = 0;
    int ans = a[n - 1] - a[0];
    if (ans == 0)
    {
        cnt = (long long)(n - 1) * n / 2;
    }
    else
    {
        int m1 = 0, m2 = 0;
        for (int i = 0;i < n;i++)
        {
            if (a[i] == a[0])m1++;
            else break;
        }
        for (int i = n-1;i >=0;i--)
        {
            if (a[i] == a[n-1])m2++;
            else break;
        }
        cnt = (long long)m1 * m2;
    }
    cout << ans << " " << cnt << endl;
    return 0;
}

Day15《Two Substrings

题意:

给一个字符串,找"AB"和"BA"。

注意:"AB"和"BA"不能重合

题解:

//strstr()函数用法

char  str[]= "1234xyz" ;
char  *str1= strstr (str, "34" );
cout << str1 << endl;

strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。 该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[100001];
int main()
{
    char* c;
    scanf("%s", &s);
    if ((c = strstr(s, "AB")) != NULL && strstr(c + 2, "BA") != NULL)
        cout << "YES\n";
    else if ((c = strstr(s, "BA")) != NULL && strstr(c + 2, "AB") != NULL)
        cout << "YES\n";
    else cout << "NO\n";
    return 0;
}

Day16《Random Teams

题意:

n个人,m个队,在同一个队的人可以两两成为朋友,问朋友的对数的最大值和最小值为多少。

题解:

  • 最大值的分法是有 n-1 个队中只放一个人, 把剩下的人全放同一个队。
  • 最少的分法是尽可能的均分 n 个人到 m 个队。
  • k个人两两之间的朋友对数为  k*(k-1)/2 

坑:记得开 long long !

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll m, n, min, max, a, b;
    cin >> n >> m;
    max = (n - m + 1) * (n - m) / 2;
    a = n % m;
    b = n / m;
    min = (m * b * b - m * b + 2 * a * b) / 2;
    printf("%lld %lld\n", min, max);
    return 0;
}

Day17《Cobb

题意:

数组an,以及一个整数 k。 找到最大值 i * − ⋅ ai aj i * j − k * ( ai | aj ) 

题解:暴力循环

  • 注意减少一下循环次数,由于下标 i * j 一定大于0,故在 n 较大的时候可以不考虑下标较小的数

对于样例:

3 3
1 1 3

  • f(1,2)=12k(a1|a2)=23(1|1)=1f(1,2)=1⋅2−k⋅(a1|a2)=2−3⋅(1|1)=−1.
  • f(1,3)=13k(a1|a3)=33(1|3)=6f(1,3)=1⋅3−k⋅(a1|a3)=3−3⋅(1|3)=−6.
  • f(2,3)=23k(a2|a3)=63(1|3)=3f(2,3)=2⋅3−k⋅(a2|a3)=6−3⋅(1|3)=−3.
  • 最大值为f(1,2)=1f(1,2)=−1.

记得开 long long !

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t, n, k, a[100005];
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        ll ans = -100000000;
        for (int i = max(1, n - 200); i <= n; i++)
        {
            for (int j = i + 1; j <= n; j++)
            {
                ans = max(ans, ((ll)i) * j - (a[i] | a[j]) * k);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

Day18《Same Parity Summands

题意:

给定和n与数字个数k,问能否有k个偶数/奇数之和为n

题解:

  1. 若为 k 个奇数,需满足:n-(k-1) 的差为奇数
  2. 同理,若为 k 个偶数,需满足: n-(k-1)*2 的差为偶数
  3. 如果两个都不行,那么就是NO

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1000 + 10];
int b[1000 + 10];
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        int num = n - (k - 1);
        if (num > 0 && num % 2 != 0) {
            cout << "YES" << endl;
            for (int i = 1; i <= k - 1; i++) cout << "1 ";
            cout << num << endl;
            continue;
        }
        num = n - (k - 1) * 2;
        if (num > 0 && num % 2 == 0) {
            cout << "YES" << endl;
            for (int i = 1; i <= k - 1; i++) cout << "2 ";
            cout << num << endl;
            continue;
        }
        cout << "NO" << endl;
    }
    return 0;
}

Day19《Pashmak and Garden

题意:

给出两个点 (x1,y1),(x2,y2) 问能不能构成正方形,能则输出另外两点。不能则输出 -1。

题解:

  1. 计算 x2-x1,y2-y1(取绝对值)
  2. 若有一个值为零,那么说明这两个点在同一条(与 x/y 轴平行的)直线上
  3. 若两个值都不为零,那么需要两个值相等(两个值代表了两条边长)
  4. 如果两种情况都不满足,那么输出 "-1"

因为题目很水,所以这题没有注释

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int lenx = abs(x1 - x2);
    int leny = abs(y1 - y2);

    if (lenx && leny && lenx != leny) printf("-1\n");
    else if (!lenx) printf("%d %d %d %d\n", (x1 + leny), y1, (x1 + leny), y2);
    else if (!leny) printf("%d %d %d %d\n", x1, y1 + lenx, x2, y1 + lenx);
    else printf("%d %d %d %d\n", x2, y1, x1, y2);
    return 0;
}

Day20《K-th Beautiful String

题意:

打印一个长度为 n 的字符串,原始字符串为 n-2 个 a 和最后两个 b (设 n = 4,则 str == aabb ),然后打印这个字符串第 m 个字典排序的字符串。

题解:

  1. 考虑用前缀和加二分查找找区间,太麻烦
  2. 至于规律:第一个b的位数对应m的数,找这方面的规律
  3. 有个坑,要开longlong

上板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 998244353
const int MAX = 1e5 + 10;
int main(void)
{
    int T;
    cin >> T;
    while (T--)
    {
        int n, m;
        ll x, y;
        cin >> n >> m;
        for (ll i = 1; i <= MAX; i++)
        {
            if (m <= i * (i - 1) / 2)
            {
                x = i - 1;
                break;
            }
        }
        y = m - (x * (x - 1)) / 2;
        string str(n, 'a');//建立一个全为'a'的字符串
        str[n - x - 1] = 'b';
        str[n - y] = 'b';
        cout << str << endl;
    }
    return 0;
}

 

posted @ 2021-08-04 23:39  流白李  阅读(80)  评论(0编辑  收藏  举报