Educational Codeforces Round 9 A B C D F

老奶奶在市场售卖苹果,顾客购买数量不一且存在欺诈情况。本文介绍了一个算法,用于计算老奶奶一天结束时应该有的总金额,帮助她检测是否有人欺骗了她。

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

链接:戳这里

A. Grandma Laura and Apples
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Grandma Laura came to the market to sell some apples. During the day she sold all the apples she had. But grandma is old, so she forgot how many apples she had brought to the market.

She precisely remembers she had n buyers and each of them bought exactly half of the apples she had at the moment of the purchase and also she gave a half of an apple to some of them as a gift (if the number of apples at the moment of purchase was odd), until she sold all the apples she had.

So each buyer took some integral positive number of apples, but maybe he didn't pay for a half of an apple (if the number of apples at the moment of the purchase was odd).

For each buyer grandma remembers if she gave a half of an apple as a gift or not. The cost of an apple is p (the number p is even).

Print the total money grandma should have at the end of the day to check if some buyers cheated her.

Input
The first line contains two integers n and p (1 ≤ n ≤ 40, 2 ≤ p ≤ 1000) — the number of the buyers and the cost of one apple. It is guaranteed that the number p is even.

The next n lines contains the description of buyers. Each buyer is described with the string half if he simply bought half of the apples and with the string halfplus if grandma also gave him a half of an apple as a gift.

It is guaranteed that grandma has at least one apple at the start of the day and she has no apples at the end of the day.

Output
Print the only integer a — the total money grandma should have at the end of the day.

Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

Examples


input
2 10
half
halfplus


output
15


input
3 10
halfplus
halfplus
halfplus


output
55


Note
In the first sample at the start of the day the grandma had two apples. First she sold one apple and then she sold a half of the second apple and gave a half of the second apple as a present to the second buyer.


题意:老奶奶卖苹果,每次卖当前苹果总量的一半,如果是奇数的话算一半的钱但是多出的半个苹果也送给顾客。

偶数的话直接算卖一半的钱,给出顾客的人数n和苹果的价格p,每个客人给出half或halfplus  分别代表偶数一半和奇数一半。


思路:halfplus 肯定是带0.5的  half 也就是一半   从后往前模拟就可以了。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF (1ll<<60)-1
using namespace std;
string s[55];
int n,p;
int main(){
    scanf("%d%d",&n,&p);
    for(int i=0;i<n;i++) cin>>s[i];
    ll ans=0;
    double x=0;
    for(int i=n-1;i>=0;i--){
        if(s[i]=="halfplus"){
            ans+=(ll)p*(x+0.5);
            x=(x+0.5)*2;
        } else{
            ans+=(ll)p*x;
            x=x*2;
        }
        ///printf("%f %I64d\n",x,ans);
    }
    printf("%I64d\n",ans);
    return 0;
}



B. Alice, Bob, Two Teams
time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.

The way to split up game pieces is split into several steps:

First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B.
Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once.
Alice will get all the pieces marked A and Bob will get all the pieces marked B.
The strength of a player is then the sum of strengths of the pieces in the group.

Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.

Input
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces.

The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.

The third line contains n characters A or B — the assignment of teams after the first step (after Alice's step).

Output
Print the only integer a — the maximum strength Bob can achieve.

Examples


input
5
1 2 3 4 5
ABABA


output
11


input
5
1 2 3 4 5
AAAAA


output
15


input
1
1
B


output
1


Note
In the first sample Bob should flip the suffix of length one.

In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.

In the third sample Bob should do nothing.



题意:Alice 和 Bob 玩一个游戏,游戏规则如下

1:Alice 分一段长为n*pi (1<=i<=n) 的字符串,仅有A B 两种字母,每个字母连续出现pi次

2:Bob 可以翻转最多一次该字符串的其中一段前缀或一段后缀,翻转的意思是把一段区间内的A->B或B->A

3:Alice得到A,Bob得到B。

计算出Bob最多可以得到多少B。


思路:数组记录前缀和,计算出当前i之前有多少A和B,当前i之后有多少A和B

然后枚举每个i的位置  翻转当前i的前缀或者后缀,记录max。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
ll a[500100],A[500100],B[500100];
char s[500100];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
    scanf("%s",s+1);
    A[0]=B[0]=0;
    for(int i=1;i<=n;i++){
        if(s[i]=='A') {
            A[i]=A[i-1]+a[i];
            B[i]=B[i-1];
        }
        else {
            B[i]=B[i-1]+a[i];
            A[i]=A[i-1];
        }
    }
    ll ans=max(B[n],A[n]);
    for(int i=1;i<=n;i++){
        ll x=A[n]-A[i-1];
        ll y=B[n]-B[i-1];
        ans=max(ans,B[i-1]+x);
        ans=max(ans,A[i-1]+y);
    }
    printf("%I64d\n",ans);
    return 0;
}
/*
5
6 1 1 1 1
BAAAA
*/



C. The Smallest String Concatenation
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some order such that the resulting string would be lexicographically smallest.

Given the list of strings, output the lexicographically smallest concatenation.

Input
The first line contains integer n — the number of strings (1 ≤ n ≤ 5·104).

Each of the next n lines contains one string ai (1 ≤ |ai| ≤ 50) consisting of only lowercase English letters. The sum of string lengths will not exceed 5·104.

Output
Print the only string a — the lexicographically smallest string concatenation.

Examples

input
4
abba
abacaba
bcd
er

output
abacabaabbabcder

input
5
x
xx
xxa
xxaa
xxaaa

output
xxaaaxxaaxxaxxx

input
3
c
cb
cba

output
cbacbc

题意:给出n个字符串,输出这n个字符串组合之后字典序最小的字符串

思路:字符串可以直接比较大小,所以只需要判断a+b<b+a就可以了

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
string s[50010];
bool cmp(string a,string b){
    return (a+b)<(b+a);
}
int main(){
    scanf("%d\n",&n);
    for(int i=0;i<n;i++) cin>>s[i];
    sort(s,s+n,cmp);
    for(int i=0;i<n;i++) cout<<s[i];
    return 0;
}


D. Longest Subsequence
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given array a with n elements and the number m. Consider some subsequence of a and the value of least common multiple (LCM) of its elements. Denote LCM as l. Find any longest subsequence of a with the value l ≤ m.

A subsequence of a is an array we can get by erasing some elements of a. It is allowed to erase zero or all elements.

The LCM of an empty array equals 1.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 106) — the size of the array a and the parameter from the problem statement.

The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of a.

Output
In the first line print two integers l and kmax (1 ≤ l ≤ m, 0 ≤ kmax ≤ n) — the value of LCM and the number of elements in optimal subsequence.

In the second line print kmax integers — the positions of the elements from the optimal subsequence in the ascending order.

Note that you can find and print any subsequence with the maximum length.

Examples
input
7 8
6 2 9 2 7 2 3

output
6 5
1 2 4 6 7

input
6 4
2 2 2 3 3 3

output
2 3
1 2 3

题意:给定n个数 和m 要求满足a的最多个数子序列的LCM<=m    输出子序列的个数以及这段子序列的LCM

思路:(1<=n,m<=1e6) 所以直接枚举(1~1e6)每个数处于LCM的位置下  有多少answer
dp[i]记录可以整除i的数的作为最小公倍数的数量,处理下细节就可以了 O(mlogm+n)

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
int a[1000100];
int dp[1000100];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        if(a[i]<=m) dp[a[i]]++;
    }
    for(int i=m;i>=1;i--){
        if(dp[i]){
            for(int j=i*2;j<=m;j+=i){
                dp[j]+=dp[i];
            }
        }
    }
    int num=0,ans=0;
    for(int i=1;i<=m;i++){
        if(dp[i]>num) {
            num=dp[i];
            ans=i;
        }
    }
    if(!num) cout<<1<<" "<<0<<endl;
    else {
        cout<<ans<<" "<<num<<endl;
        for(int i=1;i<=n;i++){
            if(ans%a[i]==0) cout<<i<<" ";
        }
        cout<<endl;
    }
    return 0;
}


F. Magic Matrix
time limit per test5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
You're given a matrix A of size n × n.

Let's call the matrix with nonnegative elements magic if it is symmetric (so aij = aji), aii = 0 and aij ≤ max(aik, ajk) for all triples i, j, k. Note that i, j, k do not need to be distinct.

Determine if the matrix is magic.

As the input/output can reach very huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Input
The first line contains integer n (1 ≤ n ≤ 2500) — the size of the matrix A.

Each of the next n lines contains n integers aij (0 ≤ aij < 109) — the elements of the matrix A.

Note that the given matrix not necessarily is symmetric and can be arbitrary.

Output
Print ''MAGIC" (without quotes) if the given matrix A is magic. Otherwise print ''NOT MAGIC".

Examples

input
3
0 1 2
1 0 2
2 2 0

output
MAGIC

input
2
0 1
2 3

output
NOT MAGIC

input
4
0 1 2 3
1 0 3 4
2 3 0 5
3 4 5 0

output
NOT MAGIC


题意:给定n*n的矩阵,矩阵需要满足 aii==0 && aij==aji && aij<=max(aik,ajk) 对于任意的i,j,k(可以相同)

思路:第一和第二个条件很好处理,问题就是在处理第三个条件。
首先对于任意的i,j,k  aij<=max(aik,ajk) 我们把它看成n个点,aij表示i->j的距离 ,那么我们需要知道在i到j的任意路径上的最大值需要大于等于aij  ,也就是aij必须是要小于取任意i到j的路径上的最大值
仔细想想,我们跑最小生成树,如果aij的权值> i到j路径上的任意值的话  那就是not magic  反之magic
我们可以按边的大小排个序,用并查集存当前最小边的点,当前判断ij是否已经在集合中,在的话说明aij这条边有问题,也就是aij的权值大于i到j路径上的任意值了,那么答案就出来了。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
struct edge{
    int u,v,w,next;
}e[2501*2501];
int head[2501],a[2501][2501],fa[2501];
int n,tot=0;
void add(int u,int v,int w){
    e[tot].u=u;
    e[tot].v=v;
    e[tot].w=w;
    e[tot].next=head[u];
    head[u]=tot++;
}
bool cmp(edge a,edge b){
    return a.w<b.w;
}
int find(int x){
    if(x!=fa[x]) fa[x]=find(fa[x]);
    return fa[x];
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        fa[i]=i;
        head[i]=-1;
        for(int j=1;j<=n;j++) scanf("%d",&a[i][j]);
    }
    int flag=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j && a[i][j]) flag=1;
            if(a[i][j]!=a[j][i]) flag=1;
            if(i>j){
                add(i,j,a[i][j]);
                add(j,i,a[j][i]);
            }
        }
    }
    if(flag) cout<<"NOT MAGIC"<<endl;
    else {
        sort(e,e+tot,cmp);
        int last=0;
        for(int i=0;i<tot;i++){
            if(i+1<tot && e[i].w==e[i+1].w) continue;
            for(int j=last;j<=i;j++){
                int xx=find(e[j].u);
                int yy=find(e[j].v);
                if(xx==yy) {
                    cout<<"NOT MAGIC"<<endl;
                    return 0;
                }
            }
            for(int j=last;j<=i;j++){
                int xx=find(e[j].u);
                int yy=find(e[j].v);
                fa[xx]=yy;
            }
            last=i+1;
        }
        cout<<"MAGIC"<<endl;
    }
    return 0;
}


### Codeforces Educational Round 26 比赛详情 Codeforces是一个面向全球程序员的比赛平台,其中Educational Rounds旨在帮助参与者提高算法技能并学习新技巧。对于具体的Educational Round 26而言,这类比赛通常具有如下特点: - **时间限制**:每道题目的解答需在规定时间内完成,一般为1秒。 - **内存限制**:程序运行所占用的最大内存量被限定,通常是256兆字节。 - 输入输出方式标准化,即通过标准输入读取数据并通过标准输出打印结果。 然而,关于Educational Round 26的具体题目细节并未直接提及于提供的参考资料中。为了提供更精确的信息,下面基于以往的教育轮次给出一些常见的题目类型及其解决方案思路[^1]。 ### 题目示例与解析 虽然无法确切描述Educational Round 26中的具体问题,但可以根据过往的经验推测可能涉及的问题类别以及解决这些问题的一般方法论。 #### 类型一:贪心策略的应用 考虑一个问题场景,在该场景下需要照亮一系列连续排列的对象。假设存在若干光源能够覆盖一定范围内的对象,则可以通过遍历整个序列,并利用贪心的思想决定何时放置新的光源以确保所有目标都被有效照射到。这种情况下,重要的是保持追踪当前最远可到达位置,并据此做出决策。 ```cpp #include <bits/stdc++.h> using namespace std; bool solve(vector<int>& a) { int maxReach = 0; for (size_t i = 0; i < a.size(); ++i) { if (maxReach < i && !a[i]) return false; if (a[i]) maxReach = max(maxReach, static_cast<int>(i) + a[i]); } return true; } ``` #### 类型二:栈结构处理匹配关系 另一个常见问题是涉及到成对出现元素之间的关联性判断,比如括号表达式的合法性验证。这里可以采用`<int>`类型的栈来记录左括号的位置索引;每当遇到右括号时就弹出最近一次压入栈底的那个数值作为配对依据,进而计算两者间的跨度长度累加至总数之中[^2]。 ```cpp #include <stack> long long calculateParens(const string& s) { stack<long long> positions; long long num = 0; for(long long i = 0 ; i<s.length() ;++i){ char c=s[i]; if(c==&#39;(&#39;){ positions.push(i); }else{ if(!positions.empty()){ auto pos=positions.top(); positions.pop(); num+=i-pos; } } } return num; } ``` #### 类型三:特定模式下的枚举法 针对某些特殊条件约束下的计数类问题,如寻找符合条件的三位整数的数量。此时可通过列举所有可能性的方式逐一检验是否符合给定规则,从而统计满足要求的结果数目。例如求解形如\(abc\)形式且不含重复数字的正整数集合大小[^3]。 ```cpp vector<int> generateSpecialNumbers(int n) { vector<int> result; for (int i = 1; i <= min(n / 100, 9); ++i) for (int j = 0; j <= min((n - 100 * i) / 10, 9); ++j) for (int k = 0; k <= min(n % 10, 9); ++k) if ((100*i + 10*j + k)<=n&&!(i==0||j==0)) result.emplace_back(100*i+10*j+k); sort(begin(result), end(result)); return result; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值