Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.
GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Substring of string x is a string formed by consecutive segment of characters from x. Two substrings of string x overlap if there is position i in string x occupied by both of them.
GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings k?
The first line contains string a, the second line contains string b, and the third line contains string c (1 ≤ |a|, |b|, |c| ≤ 105, where |s| denotes the length of string s).
All three strings consist only of lowercase English letters.
It is possible that b and c coincide.
Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them.
aaa a b
aaa
pozdravstaklenidodiri niste dobri
nisteaadddiiklooprrvz
abbbaaccca ab aca
ababacabcc
In the third sample, this optimal solutions has three non-overlaping substrings equal to either b or c on positions 1 – 2 (ab), 3 – 4 (ab), 5 – 7 (aca). In this sample, there exist many other optimal solutions, one of them would be acaababbcc.
思路:暴力枚举有多少个b,然后算c
#include<bits/stdc++.h>
using namespace std;
const int INF=1000000000;
string a,b,c;
int cnta[30],cntb[30],cntc[30];
void solve(int cnt[],string s)
{
for(int i=0;i<s.size();i++)
cnt[s[i]-'a']++;
}
int main()
{
while(cin>>a>>b>>c)
{
memset(cnta,0,sizeof(cnta));
memset(cntb,0,sizeof(cntb));
memset(cntc,0,sizeof(cntc));
solve(cnta,a);
solve(cntb,b);
solve(cntc,c);
int len=a.size()/b.size();
int ans=0,ansa=0,ansb=0;
for(int i=0;i<=len;i++)
{
bool flag=true;
for(int j=0;j<26;j++)
if(cntb[j]*i>cnta[j]){flag=false;break;}
if(!flag)break;
int minv=INF;
for(int j=0;j<26;j++)
if(cntc[j]&&(cnta[j]-cntb[j]*i)/cntc[j]<minv)minv=(cnta[j]-cntb[j]*i)/cntc[j];
if(ans<i+minv)
{
ans=i+minv;
ansa=i;
ansb=minv;
}
}
for(int i=0;i<ansa;i++)cout<<b;
for(int i=0;i<ansb;i++)cout<<c;
for(int i=0;i<26;i++)
cnta[i]-=cntb[i]*ansa+cntc[i]*ansb;
for(int i=0;i<26;i++)
for(int j=0;j<cnta[i];j++)cout<<char('a'+i);
cout<<endl;
}
return 0;
}
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way.
In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≤ i ≤ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are:
- If i ≠ n, move from pile i to pile i + 1;
- If pile located at the position of student is not empty, remove one box from it.
GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed.
The first line contains two integers n and m (1 ≤ n, m ≤ 105), the number of piles of boxes and the number of GukiZ's students.
The second line contains n integers a1, a2, ... an (0 ≤ ai ≤ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty.
In a single line, print one number, minimum time needed to remove all the boxes in seconds.
2 1 1 1
4
3 2 1 0 2
5
4 100 3 4 5 4
5
First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second).
Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds.
Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.
思路:二分,然后贪心判断,一个人一个人的,让每个人移动尽量多的box
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=100010;
int N,M,a[maxn],b[maxn];
bool judge(LL x)
{
int cur=N;
LL tmp=0;
copy(a+1,a+1+N,b+1);
for(int i=1;i<=M;i++)
{
while(cur>=1&&b[cur]==0)cur--;
if(cur<=0)return true;
tmp=cur;
if(tmp>x)return false;
while(cur>=1&&b[cur]+tmp<=x)
{
tmp+=b[cur];
b[cur]=0;
cur--;
}
if(cur<=0)return true;
b[cur]-=(x-tmp);
}
return false;
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
LL l=0,r=N+1;
for(int i=1;i<=N;i++)
{
scanf("%d",&a[i]);
r+=a[i];
}
while(l<r)
{
LL mid=(l+r)>>1;
if(judge(mid))r=mid;
else l=mid+1;
}
cout<<r<<endl;
}
return 0;
}
We all know that GukiZ often plays with arrays.
Now he is thinking about this problem: how many arrays a, of length n, with non-negative elements strictly less then 2l meet the following condition: ? Here operation means bitwise AND (in Pascal it is equivalent to and, in C/C++/Java/Python it is equivalent to &), operation means bitwise OR (in Pascal it is equivalent to , in C/C++/Java/Python it is equivalent to |).
Because the answer can be quite large, calculate it modulo m. This time GukiZ hasn't come up with solution, and needs you to help him!
First and the only line of input contains four integers n, k, l, m (2 ≤ n ≤ 1018, 0 ≤ k ≤ 1018, 0 ≤ l ≤ 64, 1 ≤ m ≤ 109 + 7).
In the single line print the number of arrays satisfying the condition above modulo m.
2 1 2 10
3
2 1 1 3
1
3 3 2 10
9
In the first sample, satisfying arrays are {1, 1}, {3, 1}, {1, 3}.
In the second sample, only satisfying array is {1, 1}.
In the third sample, satisfying arrays are {0, 3, 3}, {1, 3, 2}, {1, 3, 3}, {2, 3, 1}, {2, 3, 3}, {3, 3, 0}, {3, 3, 1}, {3, 3, 2}, {3, 3, 3}思路:首先把k化成二进制,那么对于0的位,这n个数的这一位,不能有连续的两个1,如果为1,正好相反,至少有一个连续的两个1那么,问题就转化成了如何求没有连续的两个1的个数,那么这个可以用简单的递推,但是n太大,那么可以矩阵快速幂,其实就是个斐波那契额数,然后k把每一位对应的情况乘起来
注意l=64的时候,要特别注意一下
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef unsigned long long LL;
LL N,K,L,MOD;
struct Matrix
{
LL mat[2][2];
Matrix(){memset(mat,0,sizeof(mat));}
Matrix operator*(Matrix A)
{
Matrix res;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
res.mat[i][j]=(res.mat[i][j]+mat[i][k]*A.mat[j][k]%MOD)%MOD;
return res;
}
};
LL pow_mul(LL x,LL n)
{
LL res=1;
while(n)
{
if(n&1)res=(res*x)%MOD;
x=(x*x)%MOD;
n>>=1;
}
return res;
}
Matrix matrix_pow_mul(Matrix A,LL n)
{
Matrix res;
for(int i=0;i<2;i++)res.mat[i][i]=1;
while(n)
{
if(n&1)res=res*A;
A=A*A;
n>>=1;
}
return res;
}
int main()
{
while(cin>>N>>K>>L>>MOD)
{
if(L!=64&&K>=(1ULL<<L)){printf("0\n");continue;}
Matrix A,B;
A.mat[0][0]=A.mat[0][1]=A.mat[1][0]=1;
A=matrix_pow_mul(A,N-2);
B.mat[0][0]=3;
B.mat[0][1]=2;
A=A*B;
LL ans=1;
LL sum=pow_mul(2,N);
for(LL i=0;i<L;i++)
{
if(K&(1LL<<i))ans=(ans*((sum-A.mat[0][0]+MOD)%MOD))%MOD;
else ans=(ans*A.mat[0][0])%MOD;
}
cout<<ans%MOD<<endl;
}
return 0;
}
Professor GukiZ was playing with arrays again and accidentally discovered new function, which he called GukiZiana. For given array a, indexed with integers from 1 to n, and number y, GukiZiana(a, y) represents maximum value of j - i, such that aj = ai = y. If there is no y as an element in a, then GukiZiana(a, y) is equal to - 1. GukiZ also prepared a problem for you. This time, you have two types of queries:
- First type has form 1 l r x and asks you to increase values of all ai such that l ≤ i ≤ r by the non-negative integer x.
- Second type has form 2 y and asks you to find value of GukiZiana(a, y).
For each query of type 2, print the answer and make GukiZ happy!
The first line contains two integers n, q (1 ≤ n ≤ 5 * 105, 1 ≤ q ≤ 5 * 104), size of array a, and the number of queries.
The second line contains n integers a1, a2, ... an (1 ≤ ai ≤ 109), forming an array a.
Each of next q lines contain either four or two numbers, as described in statement:
If line starts with 1, then the query looks like 1 l r x (1 ≤ l ≤ r ≤ n, 0 ≤ x ≤ 109), first type query.
If line starts with 2, then th query looks like 2 y (1 ≤ y ≤ 109), second type query.
For each query of type 2, print the value of GukiZiana(a, y), for y value for that query.
4 3 1 2 3 4 1 1 2 1 1 1 1 1 2 3
2
2 3 1 2 1 2 2 1 2 3 2 4
0 -1
思路:分块大法,对于每一块排序,找的时候二分,刚开始写一直wa,我是每次更新的时候把头上的两段对应的add都加上,然后在更新,查询的时候判断加上add的值,后来改成不加,二分判的时候减掉add就过了
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF=1000000000;
const int maxn=500010;
const int maxm=1010;
int N,Q;
LL a[maxn];
int size,num;
LL add[maxm],cnt[maxm];
struct node
{
LL x;
int id;
bool operator<(const node &A)const
{
if(x==A.x)return id>A.id;
return x<A.x;
}
}block[maxm][maxm];
void init()
{
for(int i=0;i<N;i++)cin>>a[i];
size=int(sqrt(N));
int j=0;
num=0;
memset(add,0,sizeof(add));
for(int i=0;i<N;i++)
{
block[num][j].x=a[i];
block[num][j].id=i;
if(++j==size)cnt[num]=size,num++,j=0;
}
for(int i=0;i<num;i++)sort(block[i],block[i]+size);
if(j)sort(block[num],block[num]+j),cnt[num]=j,num++;
}
void Add(int l,int r,LL x)
{
int x_pos=l/size,y_pos=r/size;
if(x_pos==y_pos)
{
for(int i=l;i<=r;i++)a[i]+=x;
for(int i=x_pos*size;i<x_pos*size+cnt[x_pos];i++)
block[x_pos][i-x_pos*size].x=a[i],
block[x_pos][i-x_pos*size].id=i;
sort(block[x_pos],block[x_pos]+cnt[x_pos]);
}
else
{
for(int i=l;i<(x_pos+1)*size;i++)a[i]+=x;
for(int i=x_pos*size;i<x_pos*size+cnt[x_pos];i++)
block[x_pos][i-x_pos*size].x=a[i],
block[x_pos][i-x_pos*size].id=i;
sort(block[x_pos],block[x_pos]+cnt[x_pos]);
for(int i=y_pos*size;i<=r;i++)a[i]+=x;
for(int i=y_pos*size;i<y_pos*size+cnt[y_pos];i++)
block[y_pos][i-y_pos*size].x=a[i],
block[y_pos][i-y_pos*size].id=i;
sort(block[y_pos],block[y_pos]+cnt[y_pos]);
for(int i=x_pos+1;i<y_pos;i++)
add[i]+=x;
}
}
int lower(int pos,int l,int r,LL x)
{
while(l<r)
{
int mid=(l+r)>>1;
if(block[pos][mid].x>=x)r=mid;
else l=mid+1;
}
return r;
}
int upper(int pos,int l,int r,LL x)
{
while(l<r)
{
int mid=(l+r)>>1;
if(block[pos][mid].x<=x)l=mid+1;
else r=mid;
}
return r;
}
void Query(LL x)
{
int minv=INF,maxv=-1;
for(int i=0;i<num;i++)
{
int pos=lower(i,0,cnt[i],x-add[i]);
int pos1=upper(i,0,cnt[i],x-add[i]);
if(pos<cnt[i]&&block[i][pos].x==x-add[i])
{
minv=min(minv,block[i][pos].id);
maxv=max(maxv,block[i][pos].id);
}
if(pos1>0&&block[i][pos1-1].x==x-add[i])
{
minv=min(minv,block[i][pos1-1].id);
maxv=max(maxv,block[i][pos1-1].id);
}
}
if(minv!=INF&&maxv!=-1)printf("%d\n",maxv-minv);
else printf("-1\n");
}
int main()
{
int op,l,r;
LL x,y;
scanf("%d%d",&N,&Q);
init();
while(Q--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d",&l,&r);
cin>>x;
Add(l-1,r-1,x);
}
else
{
cin>>y;
Query(y);
}
}
return 0;
}