Codeforces Round #553 (Div. 2)

A. Maxim and Biology

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Today in the scientific lyceum of the Kingdom of Kremland, there was a biology lesson. The topic of the lesson was the genomes. Let's call the genome the string "ACTG".

Maxim was very boring to sit in class, so the teacher came up with a task for him: on a given string ?s consisting of uppercase letters and length of at least 44, you need to find the minimum number of operations that you need to apply, so that the genome appears in it as a substring. For one operation, you can replace any letter in the string ?s with the next or previous in the alphabet. For example, for the letter "D" the previous one will be "C", and the next — "E". In this problem, we assume that for the letter "A", the previous one will be the letter "Z", and the next one will be "B", and for the letter "Z", the previous one is the letter "Y", and the next one is the letter "A".

Help Maxim solve the problem that the teacher gave him.

A string ?a is a substring of a string ?b if ?a can be obtained from ?b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Input

The first line contains a single integer ?n (4≤?≤504≤n≤50) — the length of the string ?s.

The second line contains the string ?s, consisting of exactly ?n uppercase letters of the Latin alphabet.

Output

Output the minimum number of operations that need to be applied to the string ?s so that the genome appears as a substring in it.

简单模拟,签到题。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define esp 1e-6
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
int N;
const int maxN = 105;
char s[maxN], a[maxN];
int solve(int x)
{
    int ans = 0;
    ans += min(abs('A' - a[x]), 26 - abs('A' - a[x]));
    ans += min(abs('C' - a[x + 1]), 26 - abs('C' - a[x + 1]));
    ans += min(abs('T' - a[x + 2]), 26 - abs('T' - a[x + 2]));
    ans += min(abs('G' - a[x + 3]), 26 - abs('G' - a[x + 3]));
    return ans;
}
int main()
{
    scanf("%d", &N);
    scanf("%s", a + 1);
    int ans = INF;
    for(int i=1; i<=N - 3; i++)
    {
        ans = min(ans, solve(i));
    }
    printf("%d\n", ans);
    return 0;
}

B. Dima and a Bad XOR

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Student Dima from Kremland has a matrix ?a of size ?×?n×m filled with non-negative integers.

He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him!

Formally, he wants to choose an integers sequence ?1,?2,…,??c1,c2,…,cn (1≤??≤?1≤cj≤m) so that the inequality ?1,?1⊕?2,?2⊕…⊕??,??>0a1,c1⊕a2,c2⊕…⊕an,cn>0holds, where ??,?ai,j is the matrix element from the ?i-th row and the ?j-th column.

Here ?⊕?x⊕y denotes the bitwise XOR operation of integers ?x and ?y.

Input

The first line contains two integers ?n and ?m (1≤?,?≤5001≤n,m≤500) — the number of rows and the number of columns in the matrix ?a.

Each of the next ?n lines contains ?m integers: the ?j-th integer in the ?i-th line is the ?j-th element of the ?i-th row of the matrix ?a, i.e. ??,?ai,j (0≤??,?≤10230≤ai,j≤1023).

Output

If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE".

Otherwise print "TAK" in the first line, in the next line print ?n integers ?1,?2,…??c1,c2,…cn (1≤??≤?1≤cj≤m), so that the inequality ?1,?1⊕?2,?2⊕…⊕??,??>0a1,c1⊕a2,c2⊕…⊕an,cn>0 holds.

If there is more than one possible answer, you may output any.

记忆化搜索

  一共就是2^10-1的最大的数,也就是说,二进制上最多也就是利用2^0 ~ 2^9,那么我们可以对每一位去锁定一个0、1情况,以此跑下去即可,期间可以用记忆化来优化时间,虽然没试过去掉记忆化会不会T。然后,遇到true情况,就入栈即可,回头从栈顶开始输出。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define esp 1e-6
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 505, maxE = 1e5 + 7;
int N, M, a[maxN][maxN], ans[maxN], tot, mp[maxN][12][2];
int dp[12][maxN][2];
/*struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxE];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}*/
bool dfs(int u, int deep, int now)
{
    if(dp[u][deep][now] ^ -1) return dp[u][deep][now] == 1;
    if(deep == N && now) return true;
    if(deep == N) return false;
    if(mp[deep + 1][u][0])
    {
        dp[u][deep][now] = dfs(u, deep + 1, now ^ 0);
        if(dp[u][deep][now])
        {
            ans[++tot] = mp[deep + 1][u][0];
            return true;
        }
    }
    if(mp[deep + 1][u][1])
    {
        dp[u][deep][now] = dfs(u, deep + 1, now ^ 1);
        if(dp[u][deep][now])
        {
            ans[++tot] = mp[deep + 1][u][1];
            return true;
        }
    }
    return false;
}
inline void init()
{
    memset(mp, 0, sizeof(mp));
    memset(dp, -1, sizeof(dp));
}
int main()
{
    scanf("%d%d", &N, &M);
    tot = 0;
    init();
    for(int i=1; i<=N; i++) for(int j=1; j<=M; j++)
    {
        scanf("%d", &a[i][j]);
        for(int wi=0; wi<=9; wi++)
        {
            if( (a[i][j] >> wi) & 1) mp[i][wi][1] = j;
            else mp[i][wi][0] = j;
        }
    }
    for(int i=0; i<=9; i++)
    {
        if(mp[1][i][0] && dfs(i, 1, 0))
        {
            ans[++tot] = mp[1][i][0];
            printf("TAK\n");
            for(int i=tot; i>=1; i--) printf("%d%c", ans[i], i==1?'\n':' ');
            return 0;
        }
        if(mp[1][i][1] && dfs(i, 1, 1))
        {
            ans[++tot] = mp[1][i][1];
            printf("TAK\n");
            for(int i=tot; i>=1; i--) printf("%d%c", ans[i], i==1?'\n':' ');
            return 0;
        }
    }
    printf("NIE\n");
    return 0;
}

C. Problem for Nazar

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.

Consider two infinite sets of numbers. The first set consists of odd positive numbers (1,3,5,7,…1,3,5,7,…), and the second set consists of even positive numbers (2,4,6,8,…2,4,6,8,…). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage — the first two numbers from the second set, on the third stage — the next four numbers from the first set, on the fourth — the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another.

The ten first written numbers: 1,2,4,3,5,7,9,6,8,101,2,4,3,5,7,9,6,8,10. Let's number the numbers written, starting with one.

The task is to find the sum of numbers with numbers from ?l to ?r for given integers ?l and ?r. The answer may be big, so you need to find the remainder of the division by 10000000071000000007 (109+7109+7).

Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.

Input

The first line contains two integers ?l and ?r (1≤?≤?≤10181≤l≤r≤1018) — the range in which you need to find the sum.

Output

Print a single integer — the answer modulo 10000000071000000007 (109+7109+7).

规律题

  不难发现,我们可以将时间优化至O(logN),可以不断的去减去2^K次,直到不能再减去2^K为止,我们再进行一个额外的加即可。期间注意大数的乘,不要超过了long long,对于尤其是最后额外加的时候,要注意多余项。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define esp 1e-6
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ll mod = 1e9 + 7;
ll L, R;
ll solve(ll id)
{
    if(!id) return 0;
    if(id == 1) return 1;
    ll ans = 1, ji = 1, ou = 0, tot = 1;  //奇数、偶数,目前的值
    id --;
    int flag = 1;
    while((1LL << tot) <= id)
    {
        flag ^= 1;
        id -= (1LL << tot);
        if(!flag)
        {
            ans += ( ((ou + ( (1LL << tot)%mod ) + 1LL)%mod) * ( (1LL << tot)%mod))%mod;
            ou = ou + (2LL * ((1LL << tot)%mod)%mod);
            ou %= mod;  //注意点
        }
        else
        {
            ans += ( ((ji + ( (1LL << tot)%mod ) + 1LL)%mod) * ( (1LL << tot)%mod))%mod;
            ji = ji + (2LL * ((1LL << tot)%mod)%mod);
            ji %= mod;  //注意点
        }
        ans %= mod;
        tot++;
    }
    if(id)
    {
        flag ^= 1;
        if(!flag)
        {
            ans += ((ou + 1LL + id) % mod) * (id%mod) % mod;
        }
        else
        {
            ans += ((ji + 1LL + id) % mod) * (id%mod) % mod;
        }
        ans %= mod;
    }
    return ans;
}
int main()
{
    scanf("%lld%lld", &L, &R);
    printf("%lld\n", ( solve(R) - solve(L - 1) + mod ) % mod);
    return 0;
}

D. Stas and the Queue at the Buffet

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of ?n high school students numbered from 11 to ?n. Initially, each student ?i is on position ?i. Each student ?i is characterized by two numbers — ??ai and ??bi. Dissatisfaction of the person ?i equals the product of ??ai by the number of people standing to the left of his position, add the product ??bi by the number of people standing to the right of his position. Formally, the dissatisfaction of the student ?i, which is on the position ?j, equals ??⋅(?−1)+??⋅(?−?)ai⋅(j−1)+bi⋅(n−j).

The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.

Although Stas is able to solve such problems, this was not given to him. He turned for help to you.

Input

The first line contains a single integer ?n (1≤?≤1051≤n≤105) — the number of people in the queue.

Each of the following ?n lines contains two integers ??ai and ??bi (1≤??,??≤1081≤ai,bi≤108) — the characteristic of the student ?i, initially on the position ?i.

Output

Output one integer — minimum total dissatisfaction which can be achieved by rearranging people in the queue.

贪心

  我们要知道贪心的策略,就可以从将(a, b)和(x, y)谁放在(i)位置和谁放在(i+1)位置来想,为了使得(a, b)放在(i)位置更优,就是需要a*(i - 1) + b * (N - i) + x*(i) + y*(N - i - 1) < a*(i) + b*(N - i - 1) + x*(i - 1) + y*(N - i),然后以化简得到的答案为排序的要求对所有的这样的对排序即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define esp 1e-6
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N;
struct node
{
    ll a, b;
}x[maxN];
//bool cmp(node e1, node e2) { return e1.a * e2.b > e2.a * e1.b; }
//bool cmp(node e1, node e2) { return e1.a == e2.a ? e1.b < e2.b : e1.a > e2.a; }
bool cmp(node e1, node e2) { return e1.b - e1.a + e2.a - e2.b < 0; }
int main()
{
    scanf("%d", &N);
    for(int i=1; i<=N; i++)
    {
        scanf("%lld%lld", &x[i].a, &x[i].b);
    }
    sort(x + 1, x + N + 1, cmp);
    ll ans = 0;
    for(int i=1; i<=N; i++)
    {
        ans += x[i].a * (i - 1) + x[i].b * (N - i);
    }
    printf("%lld\n", ans);
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值