Professor GukiZ likes programming contests. He especially likes to rate his students on the contests he prepares. Now, he has decided to prepare a new contest.
In total, n students will attend, and before the start, every one of them has some positive integer rating. Students are indexed from 1 to n. Let's denote the rating of i-th student as ai. After the contest ends, every student will end up with some positive integer position. GukiZ expects that his students will take places according to their ratings.
He thinks that each student will take place equal to . In particular, if student A has rating strictly lower then student B, A will get the strictly better position than B, and if two students have equal ratings, they will share the same position.
GukiZ would like you to reconstruct the results by following his expectations. Help him and determine the position after the end of the contest for each of his students if everything goes as expected.
The first line contains integer n (1 ≤ n ≤ 2000), number of GukiZ's students.
The second line contains n numbers a1, a2, ... an (1 ≤ ai ≤ 2000) where ai is the rating of i-th student (1 ≤ i ≤ n).
In a single line, print the position after the end of the contest for each of n students in the same order as they appear in the input.
3 1 3 3
3 1 1
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stdlib.h> #include<vector> #include<stack> #include<queue> #include<map> #include<string> using namespace std; #define LL long long #define ULL unsigned long long struct sw{ int a,loc,rank; }t[2005]; int n; int cmp1(const void *aa,const void *bb){ return ((sw *)bb)->a-((sw *)aa)->a; } int cmp2(const void *aa,const void *bb){ return ((sw *)aa)->loc-((sw *)bb)->loc; } int main(void){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&t[i].a); t[i].loc=i; } qsort(t+1,n,sizeof(t[0]),cmp1); for(int i=1;i<=n;i++) if(t[i].a==t[i-1].a) t[i].rank=t[i-1].rank; else t[i].rank=i; qsort(t+1,n,sizeof(t[0]),cmp2); for(int i=1;i<=n;i++) printf("%d ",t[i].rank); return 0; }
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 stringsk?
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
By senwil, contest: Codeforces Round #307 (Div. 2), problem: (B) ZgukistringZ, Accepted, # #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stdlib.h> #include<vector> #include<stack> #include<queue> #include<map> #include<string> using namespace std; #define LL long long #define ULL unsigned long long char a[100005],b[100005],c[100005]; int ca[26],cb[26],cc[26]; int cha[26],ma,mb; int ans,ta,tb; int coub,couc; int main(void){ scanf("%s%s%s",a,b,c); for(int i=0;a[i];i++) ca[a[i]-'a']++; for(int i=0;b[i];i++) cb[b[i]-'a']++; for(int i=0;c[i];i++) cc[c[i]-'a']++; int flag=1; for(int i=0;i<26;i++) if(cb[i]){ if(flag){ flag=0; ma=ca[i]/cb[i]; } else ma=min(ma,ca[i]/cb[i]); } for(;ma>=0;ma--){ for(int i=0;i<26;i++) cha[i]=ca[i]-cb[i]*ma; mb=0; flag=1; for(int i=0;i<26;i++) if(cc[i]){ if(flag){ flag=0; mb=cha[i]/cc[i]; } else mb=min(mb,cha[i]/cc[i]); } if(ma+mb>ans){ ans=ma+mb; ta=ma,tb=mb; } } for(int i=0;i<ta;i++) printf("%s",b); for(int i=0;i<tb;i++) printf("%s",c); for(int i=0;i<26;i++) cha[i]= ca[i]-ta*cb[i]-tb*cc[i]; for(int i=0;i<26;i++) for(int j=0;j<cha[i];j++) printf("%c",'a'+i); 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
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stdlib.h> #include<vector> #include<stack> #include<queue> #include<map> #include<string> using namespace std; #define LL long long #define ULL unsigned long long int n,m; LL a[100005]; LL b[100005]; bool isok(LL x){ for(int i=1;i<=n;i++) b[i]=a[i]; int cur=n; LL tmp; for(int i=1;i<=m;i++){ while(cur>=1&&b[cur]==0) cur--; if(cur==0) return 1; if(cur>x) return 0; tmp = cur; while(cur>0&&b[cur]+tmp<=x){ tmp+=b[cur]; cur--; } if(cur==0) return 1; b[cur]-=(x-tmp); } return 0; } int main(void){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%I64d",&a[i]); LL l=0,r=(1LL<<60); while(l<=r){ LL mid = (l+r)>>1; if(isok(mid)) r=mid-1; else l=mid+1; } printf("%I64d\n",l); 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 , inC/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
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; #define MAXN 2 #define MAXM 2 #define LL long long LL mod; LL mult_mod(LL a,LL b,LL c) { a %= c; b %= c; LL ret = 0; LL tmp = a; while(b) { if(b & 1) { ret += tmp; if(ret > c)ret -= c;//直接取模慢很多 } tmp <<= 1; if(tmp >=c)tmp -= c; b >>= 1; } return ret; } LL mypow(LL a,LL b){ if(b==1) return a; else if(b%2) return a*mypow(a*a%mod,b/2)%mod; else return mypow(a*a%mod,b/2); } struct Mat{ int n,m; long long mat[MAXN][MAXM]; Mat(){}; Mat(int _n){ n=_n; m=_n; memset(mat,0,sizeof(mat)); } Mat(int _n,int _m){ n=_n; m=_m; memset(mat,0,sizeof(mat)); } }; Mat mult(Mat a , Mat b){ Mat res = Mat(a.n,b.m); for(int i=0;i<a.n;i++) for(int k=0;k<a.m;k++) if(a.mat[i][k]!=0) for(int j=0;j<b.m;j++) if(b.mat[k][j]!=0) res.mat[i][j]=(res.mat[i][j]+a.mat[i][k]*b.mat[k][j]%mod)%mod; //如果快速幂里相乘超MOD,可用mult_mod return res; } Mat pow_m(Mat a,int n){ Mat res = Mat(a.n); for(int i=0;i<a.n;i++) res.mat[i][i]=1; while(n){ if(n&1) res=mult(res,a); a=mult(a,a); n>>=1; } return res; } int last(long long x){ return (x%mod+mod)%mod; } int main(void){ LL n,k; int l; scanf("%I64d%I64d%d%I64d",&n,&k,&l,&mod); if(k>(1LL<<l)){ printf("0\n"); return 0; } Mat res = Mat(2); res.mat[0][0]=res.mat[0][1]=res.mat[1][0]=1; res = pow_m(res,n); LL a = (res.mat[0][0]+res.mat[0][1])%mod; LL b = ((mypow(2,n)-a)%mod+mod)%mod; LL ans = 1; for(int i=0;i<l;i++) if(i==63||((1<<i)&k)==0) ans=(ans*a)%mod; else ans=(ans*b)%mod; printf("%I64d\n",ans); 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
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<algorithm> #include<stdlib.h> #include<vector> #include<stack> #include<queue> #include<map> #include<string> #include<cmath> #include<iostream> using namespace std; #define LL long long #define ULL unsigned long long struct sw{ LL s; int loc; }num[500005],hehe; int n,q; int c; int ever,all; LL val[500005]; int cmp(const void *a,const void *b){ if(((sw *)a)->s!=((sw *)b)->s) return ((sw *)a)->s-((sw *)b)->s>0?1:-1; else return ((sw *)a)->loc-((sw *)b)->loc; } int low(int l,int r,LL x){ while(l<=r){ int mid = (l+r)>>1; if(num[mid].s>=x) r=mid-1; else l=mid+1; } return l; } int up(int l,int r,LL x){ while(l<=r){ int mid = (l+r)>>1; if(num[mid].s>x) r=mid-1; else l=mid+1; } return l-1; } void built(){ for(int i=1;i<=all;i++){ int l = (i-1)*ever+1; int r = min(ever*i,n); qsort(num+l,r-l+1,sizeof(num[0]),cmp); } } void update(int l,int r,LL x){ int s = ((l-1)/ever)+1; int e = ((r-1)/ever)+1; for(int i=s+1;i<e;i++) val[i]+=x; int ll = (s-1)*ever+1; int rr = min(ever*s,n); for(int i=ll;i<=rr;i++) if(num[i].loc>=l&&num[i].loc<=r){ num[i].s+=x; } qsort(num+ll,rr-ll+1,sizeof(num[0]),cmp); if(s==e) return; ll = (e-1)*ever+1; rr = min(ever*e,n); for(int i=ll;i<=rr;i++) if(num[i].loc>=l&&num[i].loc<=r){ num[i].s+=x; } qsort(num+ll,rr-ll+1,sizeof(num[0]),cmp); } int query(LL x){ int ans1=-1,ans2=-1; for(int i=1;i<=all;i++){ int l = (i-1)*ever+1; int r = min(i*ever,n); int now = low(l,r,x-val[i]); if(now>=l&&now<=r&&num[now].s==x-val[i]){ ans1=num[now].loc; break; } } for(int i=all;i>=1;i--){ int l = (i-1)*ever+1; int r = min(i*ever,n); int now = up(l,r,x-val[i]); if(now>=l&&now<=r&&num[now].s==x-val[i]){ ans2=num[now].loc; break; } } if(ans1==-1&&ans2==-1) return -1; else return ans2-ans1; } int main(void){ scanf("%d%d",&n,&q); for(int i=1;i<=n;i++){ int t; scanf("%d",&t); num[i].s=t; num[i].loc=i; } all = (int)sqrt(n); ever = n/all; if(all*ever<n) all++; built(); for(int i=1;i<=q;i++){ int l,r; LL x; scanf("%d",&c); if(c==1){ scanf("%d%d%I64d",&l,&r,&x); update(l,r,x); } else { scanf("%I64d",&x); printf("%d\n",query(x)); } } return 0; }