Educational Codeforces Round 9 A B C D F
来源:互联网 发布:手机淘宝删除差评链接 编辑:程序博客网 时间:2024/06/05 13:21
链接:戳这里
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)-1using 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;}
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)-1using 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;}/*56 1 1 1 1BAAAA*/
#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)-1using 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;}
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
#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)-1using 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;}
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
input
4
0 1 2 3
1 0 3 4
2 3 0 5
3 4 5 0
output
NOT MAGIC
#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)-1using 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;}
- Educational Codeforces Round 9 A B C D F
- Codeforces Educational Round #18(Codeforces 792 A B C D)
- Codeforces-Educational Codeforces Round 32-(A,B,C,D)
- Educational Codeforces Round 5(A,B,C,D)
- Educational Codeforces Round 6 A.B.C.D.E
- Educational Codeforces Round 8 (A B C)
- Educational Codeforces Round 31 A B C
- Educational Codeforces Round 32 A B C
- Educational Codeforces Round 23 A-F
- Educational Codeforces Round 19 A+B+C+E!
- 解题报告: Educational Codeforces Round 24 A,B,C
- Educational Codeforces Round 26 A B C 三道水题
- Codeforces Round #300——A.B.C.D.E.F
- Codeforces Round #423 Div.2 A B C D E F
- Codeforces Round #424 Div.2 A B C D E F
- 解题报告:Codeforces Round #424 (Div. 2) A B C D E F
- Codeforces Round #351 A B C D
- Codeforces Round #377 A.B.C.D
- Learning Python(16)--多线程编程(threading,Queue模块)
- Android项目大全(总有你用的到的)
- 6. ZigZag Conversion
- go圣经笔记--第一章
- python编写工具之基础——处理命令行参数
- Educational Codeforces Round 9 A B C D F
- 【XHProf】 安装介绍
- MFC下DLL/lib的调用
- json字符串和java对象的相互转化
- Hadoop Streaming高级编程
- 笔记本搭建wifi
- 7. Reverse Integer
- Hadoop pipes设计原理
- 最详细的图文教程,教你一点一点的上传你的APP程序打包上传到APP Store.