链接:戳这里
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; }
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 */
#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; }
#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; }
#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; }