Codeforces round #382

本文提供了四道算法竞赛题目的详细解答过程,包括了字符串处理、动态规划、矩阵操作及组合数学等内容。通过具体实现代码展示了如何高效解决复杂问题。

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

735A - Ostap and Grasshopper

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

#define INF 0x3f3f3f3f
#define eps 1e-5
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 25 * 1E8;
using namespace std;

char s[10005];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,k;
    cin >> n >> k;
    cin >> s;
    int len = strlen(s);
    int pos1,pos2;
    for(int i = 0;i < len;i++)
    {
        if(s[i] == 'G')
            pos1 = i + 1;
        if(s[i] == 'T')
            pos2 = i + 1;
    }
    int ans;
    if(pos1 > pos2)
        ans = pos1 - pos2;
    else
        ans = pos2 - pos1;
    int ok = 0;
    if(ans % k == 0)
        ok = 1;
    if(pos1 > pos2)
    {
        for(int i = pos2 - 1;i < pos1;i += k)
            if(s[i] == '#')
                ok = 0;
    }
    else
    {
        for(int i = pos1 - 1;i < pos2;i += k)
            if(s[i] == '#')
                ok = 0;
    }
    if(ok)
        puts("YES");
    else
        puts("NO");
    return 0;
}

735B - Urbanization

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

#define INF 0x3f3f3f3f
#define eps 1e-5
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 25 * 1E8;
using namespace std;

double a[100005];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,n1,n2;
    cin >> n >> n1 >> n2;
    for(int i = 1;i <= n;i++)
        cin >> a[i];
    sort(a + 1,a + n + 1);
    for(int i = 2;i <= n;i++)
        a[i] += a[i - 1];
    //for(int i = 1;i <= n;i++)
        //printf("%.2f ",a[i]);
    //puts("");
    double ans = 0;
    if(n1 > n2)
        ans = (a[n] - a[n - n2]) / n2 + (a[n - n2] - a[n - n2 - n1]) / n1;
    else
        ans = (a[n] - a[n - n1]) / n1 + (a[n - n1] - a[n - n2 - n1]) / n2;
    printf("%.10f\n",ans);
    return 0;
}

736A - Tennis Championship

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

#define INF 0x3f3f3f3f
#define eps 1e-5
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 25 * 1E8;
using namespace std;

LL a[105];

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);
    LL k;
    cin >> k;
    a[0] = a[1] = 1;
    for(int i = 2;i <= 87;i++)
        a[i] = a[i - 1] + a[i - 2];
    for(int i = 3;i <= 87;i++)
    {
        if(k < a[i])
        {
            printf("%d\n",i - 2);
            break;
        }
    }
    //printf("%I64d\n",a[i]);
    return 0;
}

736B - Taxes

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

#define INF 0x3f3f3f3f
#define eps 1e-5
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 25 * 1E8;
using namespace std;

bool isprime(int k)
{
    int l = (int)sqrt(k) + 1;
    for(int i = 2;i < l;i++)
        if(k % i == 0)
            return 0;
    return 1;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);
    int k;
    cin >> k;
    if(isprime(k))
        puts("1");
    else if(k % 2 == 0 || isprime(k - 2))
        puts("2");
    else
        puts("3");
    return 0;
}

736C - Ostap and Tree

Problem can be solved by the method of dynamic programming.

Let dp[v][i][j] be the number of possibilities to color subtree of vertex v in such a way that the closest black vertex is on depth i, and the closest white vertex — on depth j (we also store dp[v][-1][j] and dp[v][i][-1] in the cases where there are no black and white vertexes in diapason k of v respectively).

In order to connect two subtrees, we can check all pairs (i,j) in both subtrees (by brute-force algorithm).

Then let we have pair (a,c) in the first subtree and pair (b,d) in the second one.

If min(a,c)+max(b,d)<=k, then we update value of current vertex.

Complexity of the algorithm O(n*k^4), which is acceptable for this particular problem(n — the number of vertexes, k^4 brute force search of pairs (a,b); (c,d)).

736D - Permutations

This problem consists of 3 ideas.

Idea 1: remainder modulo 2 of the number of permutation is equal to the remainder modulo 2 of the determinant of the matrix whose entries are 1 if (ai,bi) is in our list and 0 otherwise.

Idea 2: If we cahnge 1 by 0, then the determinant will differ by algebraic compliment. That is, if we count inverse matrix, than it will reflect reminders modulo 2 (B(m,n)=A’(m,n)/detA, detA is odd).

Idea 3: Inverse matrix can be counted for O((n/32)^3) time.

However, we can work is field of integers modulo 2.

The summation can be replaced by XOR.

So if we store in one “int” not a single but 32 numbers, then we can reduce our assymptocy to O(n^3/32), which is OK.

736E - Chess Championship

Suppose set (a1,a2,…,am).

Then the list is valid if set {2m-2, 2m-4, 2m-6, …, 0} majorizes the set {a1,a2,…,am}.

Let us prove it! Part 1: Suppose n<=m.

Top n players will play n(n-1)/2 games with each other and n(m-n) games with low-ranked contestants.

In these games they will collect 2*n(n-1)/2 points (in each game there is exactly 2 points) for sure and at most 2*n*(m-n) points in games with others.

So they will have at most 2*(n*(n-1)/2+n*(m-n))=2*((m-1)+(m-2)+…+(m-n)) points.

Now construction: Let’s construct results of participant with most points and then use recursion.

Suppose the winner has even number of points (2*(m-n) for some n).

Then we consider that he lost against contestants holding 2,3,4,…,n places and won against others.

If champion had odd number of points (2*(m-n)-1 for some n), then we will construct the same results supposing that he draw with (n+1)th player instead of winning agianst him.

It is easy to check that majorization is invariant, so in the end we will have to deal with 1 men competition, when set of scores {a1} is majorized by set {0}.

So a1=0, and there is obvious construction for this case. So we have such an algorithm: we search for a compiment set which is majorized by {2m-2,2m-4,…,0}.

If there is no such set answer is NO.

Otherwise answer is YES and we construct our table as shown above.

Assymptosy is O(m^2logm) (calling recursion m times, sorting the array (we can lose non-decreasing order because of poor results) and then passing on it linearly.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值