├── Code Jam ├── Round 1A 2008 │ └── A. Minimum Scalar Product.py ├── Round 1A 2010 │ └── Problem A. Rotate.cpp ├── Round 1A 2011 │ └── Problem A. FreeCell Statistics.cpp ├── Round 1A 2013 │ └── Problem A. Bullseye.cpp ├── Round 1A 2015 │ └── Problem A. Mushroom Monster.cpp ├── Round 1A 2016 │ ├── Round 1A 2016 BFFs.cpp │ ├── Round 1A 2016 Rank and File.cpp │ └── Round 1A 2016 problem A(large).cpp ├── Round 1A 2017 │ └── Problem A. Alphabet Cake.cpp ├── Round 1A 2018 │ ├── Bit Party.cpp │ └── Waffle Choppers.cpp ├── Round 1B 2008 │ └── B. Number Sets.cpp ├── Round 1B 2010 │ └── Problem A. File Fix-it.cpp ├── Round 1B 2012 │ └── Problem A. Safety in Numbers.cpp ├── Round 1B 2013 │ └── Problem A. Osmos.cpp ├── Round 1B 2014 │ └── Problem A. The Repeater.cpp ├── Round 1B 2016 │ ├── Round 1B 2016 Getting the Digits.cpp │ └── Round 1B 2016 problem B(small).cpp ├── Round 1B 2017 │ ├── Problem A. Steed 2: Cruise Control.cpp │ ├── Problem B(small).cpp │ └── Problem C(small).cpp ├── Round 1C 2010 │ └── Problem A. Rope Intranet.cpp ├── Round 1C 2012 │ └── Problem A. Diamond Inheritance.cpp ├── Round 1C 2017 │ ├── Problem A(large).cpp │ ├── Problem A(small).cpp │ ├── Round 1C 2017 problem B(small).cpp │ └── Round 1C 2017 problem C(small).cpp ├── Round 1C 2018 │ ├── A Whole New Word.cpp │ └── Ant Stack.cpp ├── Round 1C 2020 │ └── Overexcited Fan.cpp ├── Round 2 2014 │ └── Round 2 2014 Problem A. Data Packing.cpp ├── Round 2 2017 │ ├── Round 2 2017 Problem A(large).cpp │ ├── Round 2 2017 problem A(small).cpp │ └── Round 2 2017 problem B(small).cpp ├── Round 3 2016 │ ├── Round 3 2016 Problem C(small) Dijkstra.cpp │ ├── Round 3 2016 Problem C(small) Floyd-Warshall.cpp │ └── Round 3 2016 Problem C(small) Kruskal.cpp └── Round 3 2017 │ ├── Round 3 2017 problem A(large).cpp │ └── Round 3 2017 problem A(small).cpp ├── Kick Start ├── Kick Start 2020 │ ├── Round A │ │ ├── Allocation.cpp │ │ ├── Bundling-TS1.cpp │ │ ├── Bundling-TS2.cpp │ │ ├── Plates.cpp │ │ └── Workout.cpp │ ├── Round B │ │ ├── Bike.cpp │ │ ├── Bus.cpp │ │ ├── Robot.cpp │ │ └── Wandering.cpp │ ├── Round C │ │ ├── Counrdown.cpp │ │ ├── candies.cpp │ │ ├── countdown.cpp │ │ ├── perfectsubarray.cpp │ │ └── stable.cpp │ └── Round E │ │ ├── High Buildings.cpp │ │ └── Longest Arithmetic.cpp ├── Kick Start Round A 2019 │ ├── Parcels.cpp │ └── Training.cpp ├── Kick Start Round B 2019 │ ├── Building Palindromes.cpp │ └── Energy Stones.cpp ├── Kick Start Round C 2019 │ ├── Circuit Board.cpp │ └── Wiggle Walk.cpp ├── Kick Start Round D 2019 │ ├── Food Stalls (small).cpp │ ├── Food Stalls.cpp │ ├── Latest Guest (small).cpp │ ├── Latest Guest.cpp │ ├── X or What (small).cpp │ └── X or What.cpp ├── Kick Start Round E 2019 │ ├── Cherries Mesh.cpp │ ├── Code-Eat Switcher.cpp │ └── Street Checkers.cpp ├── Kick Start Round G 2019 │ ├── Book Reading.cpp │ └── Shifts.cpp ├── Kick Start Round H 2019 │ └── H-index.cpp ├── Kickstart Round A 2018 │ ├── Prob.cpp │ ├── Problem A. Even Digits.cpp │ └── Problem B. Lucky Dip.cpp ├── Kickstart Round B 2018 │ └── Kickstart Round B 2018 Problem A. No Nine.cpp ├── Kickstart Round C 2017 │ └── Kickstart Round C 2017 Problem A. Ambiguous Cipher.cpp ├── Kickstart Round C 2018 │ └── Problem A. Planet Distance.cpp ├── Kickstart Round D 2017 │ └── Kickstart Round D 2017 Problem A(small).cpp ├── Kickstart Round E 2017 │ └── Kickstart Round E 2017 Problem B. Trapezoid Counting.cpp ├── Kickstart Round F 2017 │ └── Kickstart Round F 2017 Problem D. Eat Cake.cpp ├── Kickstart Round F 2018 │ └── Common Anagrams.cpp ├── Kickstart Round G 2017 │ ├── Kickstart Round G 2017 Problem A. Huge Numbers.cpp │ ├── Kickstart Round G 2017 Problem C. Matrix Cutting(small).cpp │ └── Kickstart Round G 2017 Problem C. Matrix Cutting.cpp ├── Kickstart Round G 2018 │ ├── Problem A. Product Triplets.cpp │ ├── Problem C. Cave Escape(small).cpp │ └── Problem C. Combinig Classes (small).cpp ├── Kickstart Round H 2018 │ ├── Problem A. Big Buttons.cpp │ ├── Problem B. Mural.cpp │ └── Problem C. Let Me Count The Ways.cpp ├── Round A 2020 │ ├── Allocation.cpp │ └── Plates.cpp ├── Round E 2020 │ └── Toys.cpp └── Round F 2019 │ └── teachme.cpp └── README.md /Code Jam/Round 1A 2008/A. Minimum Scalar Product.py: -------------------------------------------------------------------------------- 1 | t=int(input()) 2 | for nt in range(1,t+1): 3 | n=int(input()) 4 | v1=map(int,input().split()) 5 | v2=map(int,input().split()) 6 | v1=sorted(v1) 7 | v2=sorted(v2,reverse=True) 8 | ans=0 9 | for i in range(n): 10 | ans+=v1[i]*v2[i]; 11 | print('Case #',nt,': ',ans,sep='') 12 | -------------------------------------------------------------------------------- /Code Jam/Round 1A 2010/Problem A. Rotate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | freopen("input.txt","r",stdin); 6 | freopen("output.txt","w",stdout); 7 | int t; 8 | cin>>t; 9 | for(int c=1;c<=t;c++){ 10 | int n,k; 11 | string grid[50]; 12 | cin>>n>>k; 13 | for(int i=0;i>grid[i]; 15 | } 16 | int n_in=n-1; 17 | for(int i=0;i-1;j--){ 20 | if(grid[i][j]!='.'){ 21 | char tmp=grid[i][j]; 22 | grid[i][j]='.'; 23 | grid[i][n_in]=tmp; 24 | n_in--; 25 | } 26 | } 27 | } 28 | bool rslt[26]; 29 | rslt['B'-'A']=false; 30 | rslt['R'-'A']=false; 31 | bool vis[4][50][50]; 32 | for(int i=0;i=k){ 50 | rslt[tar-'A']=true; 51 | continue; 52 | } 53 | cnt=0; 54 | if(!vis[1][i][j]){ 55 | for(int xp=0;xp+j=k){ 63 | rslt[tar-'A']=true; 64 | continue; 65 | } 66 | cnt=0; 67 | if(!vis[2][i][j]){ 68 | for(int ip=i;ip=k){ 76 | rslt[tar-'A']=true; 77 | continue; 78 | } 79 | cnt=0; 80 | if(!vis[3][i][j]){ 81 | for(int x=0;x+i=0;x++){ 82 | if(grid[x+i][j-x]!=tar) 83 | break; 84 | cnt++; 85 | vis[3][x+i][j-x]=true; 86 | } 87 | } 88 | if(cnt>=k){ 89 | rslt[tar-'A']=true; 90 | } 91 | } 92 | } 93 | } 94 | cout<<"Case #"< 2 | #include 3 | using namespace std; 4 | int main(){ 5 | freopen("input.txt","r",stdin); 6 | freopen("output.txt","w",stdout); 7 | int t; 8 | cin>>t; 9 | for(int c=1;c<=t;c++){ 10 | int pg,pd; 11 | long long int n; 12 | cin>>n>>pd>>pg; 13 | cout<<"Case #"< 2 | using namespace std; 3 | int main(){ 4 | freopen("input","r",stdin); 5 | freopen("output","w",stdout); 6 | int T; 7 | cin>>T; 8 | for(int C=1;C<=T;C++){ 9 | long long int r,t,l,h,m; 10 | cin>>r>>t; 11 | l=0,h=min(2e9,2e18/r); 12 | while(lt) 15 | h=m-1; 16 | else 17 | l=m; 18 | } 19 | cout<<"Case #"< 2 | using namespace std; 3 | int main(){ 4 | freopen("input.txt","r",stdin); 5 | freopen("output.txt","w",stdout); 6 | int t; 7 | cin>>t; 8 | for(int c=1;c<=t;c++){ 9 | int spd=0,n,r1=0,r2=0,m[1000]; 10 | cin>>n>>m[0]; 11 | for(int i=1;i>m[i]; 13 | spd=max(spd,m[i-1]-m[i]); 14 | r1+=(max(0,m[i-1]-m[i])); 15 | } 16 | for(int i=0;ispd) 18 | r2+=spd; 19 | else 20 | r2+=m[i]; 21 | } 22 | cout<<"Case #"< 2 | using namespace std; 3 | #define L 1001 4 | int main(){ 5 | int tot_test,n; 6 | cin>>tot_test; 7 | for(int Case=1;Case<=tot_test;Case++){ 8 | cout<<"Case #"<>n; 11 | int ans=0; 12 | int twos_len[L]={0}; 13 | for(int i=0;i>bff[i]; 15 | bff[i]--; 16 | } 17 | int path[L]={0}; 18 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int t,member,n,cnt[2501]; 6 | ifstream cin("input.txt"); 7 | ofstream cout("output.txt"); 8 | cin>>t; 9 | for(int i=0;i>n; 13 | for(int i=0;i<(2*n-1);i++){ 14 | for(int j=0;j>member; 16 | cnt[member]++; 17 | } 18 | } 19 | for(int i=1;i<2501;i++){ 20 | if(cnt[i]%2!=0){ 21 | cout< 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | int main(){ 9 | ifstream cin("input.txt"); 10 | ofstream cout("output.txt"); 11 | int test; 12 | cin>>test; 13 | for(int i=1;i<=test;i++){ 14 | listl; 15 | string s; 16 | cin>>s; 17 | l.push_back(s[0]); 18 | char t=s[0]; 19 | for(int i=1;is[i]) 21 | l.push_back(s[i]); 22 | else{ 23 | t=s[i]; 24 | l.push_front(s[i]); 25 | } 26 | } 27 | list::iterator it=l.begin(); 28 | cout<<"Case #"< 2 | using namespace std; 3 | int row,col; 4 | string grd[25]; 5 | 6 | int alphabet_left(int sr,int sc,int er,int ec){ 7 | int cnt=0; 8 | for(int i=sr;i<=er;i++) 9 | for(int j=sc;j<=ec;j++) 10 | if(grd[i][j]!='?') 11 | cnt++; 12 | return cnt; 13 | } 14 | void _fill(int sr,int sc,int er,int ec){ 15 | char tar='?'; 16 | for(int i=sr;i<=er;i++){ 17 | for(int j=sc;j<=ec;j++){ 18 | if(grd[i][j]!='?') 19 | tar=grd[i][j]; 20 | } 21 | if(tar!='?') 22 | break; 23 | } 24 | for(int i=sr;i<=er;i++) 25 | for(int j=sc;j<=ec;j++) 26 | grd[i][j]=tar; 27 | } 28 | void fill(int sr,int sc,int er,int ec){ 29 | // print_grd(sr,sc,er,ec); 30 | int left = alphabet_left(sr,sc,er,ec); 31 | if(left == 1){ 32 | _fill(sr,sc,er,ec); 33 | return; 34 | } 35 | for(int i=sr;i<=er;i++){ 36 | for(int j=sc;j<=ec;j++){ 37 | if(grd[i][j]!='?'){ 38 | left = alphabet_left(i+1,sc,er,ec); 39 | if(left==0){ 40 | fill(sr,sc,er,j); 41 | fill(sr,j+1,er,ec); 42 | return; 43 | } 44 | else{ 45 | fill(sr,sc,i,ec); 46 | fill(i+1,sc,er,ec); 47 | return; 48 | } 49 | } 50 | } 51 | } 52 | 53 | } 54 | int main(){ 55 | int T; 56 | cin>>T; 57 | for(int c=1;c<=T;c++){ 58 | scanf("%d%d",&row,&col); 59 | for(int i=0;i>grd[i]; 61 | cout<<"Case #"< 2 | using namespace std; 3 | int main(){ 4 | int t; 5 | cin>>t; 6 | for(int C=1;C<=t;C++){ 7 | long long int r,b,c; 8 | long long int m[1001],s[1001],p[1001]; 9 | scanf("%lld%lld%lld",&r,&b,&c); 10 | for(int i=0;i1){ 14 | vector robs; 15 | mid=(l+h)/2; 16 | for(int i=0;i0) 19 | robs.push_back(min(val/s[i],m[i])); 20 | } 21 | sort(robs.begin(),robs.end(),greater()); 22 | long long int tot=0; 23 | for(int i=0;i=b) 26 | h=mid; 27 | else 28 | l=mid; 29 | 30 | } 31 | printf("Case #%d: %lld\n",C,h); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Code Jam/Round 1A 2018/Waffle Choppers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | string grd[100]; 4 | int r,c,h,v,chocntrow[101],chocntcol[101]; 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | for(int C=1;C<=t;C++){ 9 | cin>>r>>c>>h>>v; 10 | int totchocnt=0; 11 | for(int i=0;i>grd[i]; 16 | for(int j=0;j 2 | using namespace std; 3 | #define MAXN 1000000000001 4 | # define S 1000001 5 | typedef long long int LL; 6 | bool is_comp[S]; 7 | int parents[1000001]; 8 | int find(int a){ 9 | if(parents[a]!=a) 10 | parents[a]=find(parents[a]); 11 | return parents[a]; 12 | } 13 | void merge(int a, int b){ 14 | a=find(a); 15 | b=find(b); 16 | parents[b]=parents[a]; 17 | } 18 | LL P; 19 | vector < long long int > primes; 20 | int solve(long long int m, long long int n) { 21 | int cnt = 0; 22 | int is_prime[n - m + 2]; 23 | for (int i = 0; i + m <= n; i++){ 24 | is_prime[i] = 1; 25 | parents[i]=i; 26 | } 27 | int cut_id = lower_bound(primes.begin(),primes.end(),P)-primes.begin(); 28 | for (int j = cut_id; j < (int)primes.size() and primes[j] <= n; j++) { 29 | LL p = primes[j]; 30 | LL k = p; 31 | if (k < m) k = (m + p - 1) / p * p; 32 | LL pre = k; 33 | k+=p; 34 | if(pre<=n) 35 | is_prime[pre-m] = 2; 36 | for (; k <= n; k+=p){ 37 | is_prime[k - m] = 2; 38 | merge(pre-m,k-m); 39 | } 40 | } 41 | for (int j = 0; j < cut_id and primes[j] * primes[j] <= n; j++) { 42 | LL p = primes[j]; 43 | LL k = p * p; 44 | if (k < m) k = (m + p - 1) / p * p; 45 | for (; k <= n; k+=p){ 46 | is_prime[k - m] = is_prime[k-m]==2?2:0; 47 | } 48 | } 49 | 50 | for (int i = 0; m + i <= n; i++){ 51 | if(is_prime[i]<2 or parents[i]==i) cnt++; 52 | } 53 | return cnt; 54 | } 55 | int main() { 56 | int t,nt = 0; 57 | LL l, r; 58 | for (long long int i = 2; i * i < MAXN; i++) { 59 | if (!is_comp[i]) 60 | primes.push_back(i); 61 | for (unsigned long j = 0; j < primes.size() and primes[j] * i < S; j++) { 62 | is_comp[primes[j] * i] = true; 63 | if (i % primes[j] == 0) 64 | break; 65 | } 66 | } 67 | cin >> t; 68 | while (t--) { 69 | cin >> l >> r >> P; 70 | cout << "Case #" << ++nt << ": " << solve(l, r) << "\n"; 71 | } 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Code Jam/Round 1B 2010/Problem A. File Fix-it.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | freopen("input.txt","r",stdin); 5 | freopen("output.txt","w",stdout); 6 | int t; 7 | cin>>t; 8 | for(int c=1;c<=t;c++){ 9 | int n,m; 10 | mapmp; 11 | cin>>n>>m; 12 | for(int i=0;i>s; 15 | string st=""; 16 | for(int i=0;i>s; 32 | string st=""; 33 | for(int i=0;i 2 | #define max(a,b) a>b?a:b; 3 | using namespace std; 4 | int main(){ 5 | freopen("input.txt","r",stdin); 6 | freopen("output.txt","w",stdout); 7 | int t,n; 8 | double s[200]; 9 | cin>>t; 10 | for(int c=1;c<=t;c++){ 11 | double x=0; 12 | cin>>n; 13 | for(int i=0;i>s[i]; 15 | x+=s[i]; 16 | } 17 | cout<<"Case #"<.0000001){ 25 | m=(l+h)/2; 26 | spar=x; 27 | for(int i=0;i 2 | using namespace std; 3 | 4 | int a,n,m[100]; 5 | 6 | int rec(int indx,int cap,int cnt){ 7 | if(indx==n) 8 | return cnt; 9 | if(cap>m[indx]) 10 | return rec(indx+1,cap+m[indx],cnt); 11 | return min(rec(indx,2*cap-1,cnt+1),cnt+n-indx); 12 | } 13 | 14 | int main(){ 15 | freopen("input.txt","r",stdin); 16 | freopen("output.txt","w",stdout); 17 | int t; 18 | cin>>t; 19 | for(int c=1;c<=t;c++){ 20 | cin>>a>>n; 21 | for(int i=0;i>m[i]; 23 | } 24 | sort(m,m+n); 25 | cout<<"Case #"< 2 | using namespace std; 3 | int t,n; 4 | string s[100]; 5 | int main(){ 6 | freopen("input.txt","r",stdin); 7 | freopen("output.txt","w",stdout); 8 | scanf("%d",&t); 9 | for(int c=1;c<=t;c++){ 10 | printf("Case #%d: ",c ); 11 | scanf("%d",&n); 12 | for(int i=0;i>s[i]; 14 | } 15 | vectorv[100]; 16 | int cnt[100][100]; 17 | memset(cnt,0,sizeof(cnt)); 18 | v[0].push_back(s[0][0]); 19 | cnt[0][0]++; 20 | int it=0; 21 | for(int j=1;j 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main(){ 7 | string s; 8 | int test; 9 | //ifstream cin("input.txt"); 10 | //ofstream cout("output.txt"); 11 | cin>>test; 12 | for(int Case=1;Case<=test;Case++){ 13 | vectorv(10); 14 | cin>>s; 15 | int cnt[100]={0}; 16 | for(int i=0;i 2 | using namespace std; 3 | int p[]={0,10,100,1000}; 4 | int main(){ 5 | freopen("input","r",stdin); 6 | freopen("output","w",stdout); 7 | int test; 8 | cin>>test; 9 | for(int T=1;T<=test;T++){ 10 | string C,J; 11 | cin>>C>>J; 12 | int ci,ji,ans=1000000; 13 | for(int i=0;if; 15 | int n=i; 16 | while(n){ 17 | f.push_back(n%10); 18 | n/=10; 19 | } 20 | for(int it=f.size();its; 32 | int n=j; 33 | while(n){ 34 | s.push_back(n%10); 35 | n/=10; 36 | } 37 | for(int it=s.size();itans) continue; 49 | if(abs(d)i){ 55 | ci=i; 56 | ji=j; 57 | } 58 | else if(abs(d)==ans&&ci==i&&ji>j){ 59 | ji=j; 60 | } 61 | } 62 | } 63 | std::vector oc; 64 | std::vector oj; 65 | while(ci){ 66 | oc.push_back(ci%10); 67 | ci/=10; 68 | } 69 | while(ji){ 70 | oj.push_back(ji%10); 71 | ji/=10; 72 | } 73 | for(int i=oc.size();i 2 | using namespace std; 3 | int main(){ 4 | ifstream cin("input.txt"); 5 | ofstream cout("output.txt"); 6 | int t; 7 | cin>>t; 8 | for(int c=1;c<=t;c++){ 9 | double d,n,k,s; 10 | cin>>d>>n; 11 | double mx=0,temp; 12 | for(int i=0;i>k>>s; 14 | temp=(d-k)/s; 15 | if(temp>mx) 16 | mx=temp; 17 | } 18 | cout<<"Case #"< 2 | using namespace std; 3 | bool comp(vector a,vectorb){ 4 | return a.size()>b.size(); 5 | } 6 | int main(){ 7 | ifstream cin("input"); 8 | ofstream cout("out"); 9 | int test,n,col[7]; 10 | mapmp; 11 | mp[0]='R'; 12 | mp[2]='Y'; 13 | mp[4]='B'; 14 | //R O Y G B V 15 | cin>>test; 16 | for(int c=1;c<=test;c++){ 17 | vectors[6]; 18 | cin>>n; 19 | for(int i=0;i<6;i++){ 20 | cin>>col[i]; 21 | for(int j=0;js[1].size()+s[2].size()){ 28 | cout<<"IMPOSSIBLE\n"; 29 | //cout< 2 | using namespace std; 3 | int n,q; 4 | double e[101],s[101],d[101][101],u[101],v[101]; 5 | int main(){ 6 | ifstream cin("input"); 7 | ofstream cout("output"); 8 | int test; 9 | cin>>test; 10 | for(int c=1;c<=test;c++){ 11 | cout<<"Case #"<>n>>q; 13 | double dp[101]; 14 | for(int i=0;i<=n;i++) 15 | dp[i]=1e15; 16 | for(int i=0;i>e[i]>>s[i]; 18 | for(int i=0;i>d[i][j]; 21 | for(int i=0;i>u[i]>>v[i]; 23 | dp[n-1]=0; 24 | for(int i=n-1;i>0;i--){ 25 | double rest=e[i-1],dist=0; 26 | for(int j=i;je[i-1]) break; 29 | dp[i-1]=min(dp[i-1],dist/s[i-1]+dp[j]); 30 | } 31 | } 32 | cout< 2 | using namespace std; 3 | int main(){ 4 | freopen("input.txt","r",stdin); 5 | freopen("output.txt","w",stdout); 6 | int t; 7 | cin>>t; 8 | for(int c=1;c<=t;c++){ 9 | int n,a,b; 10 | cin>>n; 11 | vector > v; 12 | for(int i=0;i>a>>b; 14 | v.push_back(make_pair(a,b)); 15 | } 16 | int cnt=0; 17 | for(int i=0;i 2 | using namespace std; 3 | vectorv[1000]; 4 | bool vis[1000]; 5 | bool dfs(int in){ 6 | if(vis[in]) 7 | return true; 8 | vis[in]=true; 9 | bool ret=false; 10 | for(int i=0;i>t; 20 | for(int c=1;c<=t;c++){ 21 | int n,inh; 22 | cin>>n; 23 | for(int i=0;i>m; 26 | for(int j=0;j>inh; 28 | v[inh-1].push_back(i); 29 | } 30 | } 31 | bool is_daiamond=false; 32 | 33 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define P 3.14159265359 7 | using namespace std; 8 | struct food{ 9 | double r,h; 10 | bool operator<(const food& o) const 11 | { 12 | return r>o.r; 13 | } 14 | }; 15 | bool comp(food a,food b){ 16 | return a.h*a.r>b.h*b.r; 17 | } 18 | int main(){ 19 | ifstream cin("input"); 20 | ofstream cout("output"); 21 | int test; 22 | cin>>test; 23 | for(int C=1;C<=test;C++){ 24 | int n,k; 25 | vectorv; 26 | double ans=-1.0; 27 | cin>>n>>k; 28 | food cake[1000]; 29 | for(int i=0;i>cake[i].r>>cake[i].h; 31 | } 32 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define P 3.14159265359 7 | using namespace std; 8 | struct food{ 9 | double r,h; 10 | bool operator<(const food& o) const 11 | { 12 | return r>o.r; 13 | } 14 | }; 15 | int main(){ 16 | ifstream cin("input"); 17 | ofstream cout("output"); 18 | int test; 19 | cin>>test; 20 | for(int C=1;C<=test;C++){ 21 | int n,k; 22 | vectorv; 23 | double ans=-1.0; 24 | cin>>n>>k; 25 | food cake[1000]; 26 | for(int i=0;i>cake[i].r>>cake[i].h; 28 | } 29 | sort(cake,cake+n); 30 | for(int i=0;ians) ans=temp; 40 | }while(next_permutation(v.begin(),v.end())); 41 | cout<<"Case #"< 2 | using namespace std; 3 | struct shedule{ 4 | int st,end; 5 | bool operator<(const shedule& o)const 6 | { 7 | return st>t; 16 | for(int C=1;C<=t;C++){ 17 | cout<<"Case #"<>ac>>aj; 19 | for(int i=0;i>c[i].st>>c[i].end; 21 | } 22 | for(int i=0;i>j[i].st>>j[i].end; 24 | } 25 | if(ac==1||aj==1) cout<<"2\n"; 26 | else{ 27 | if(ac==2){ 28 | sort(c,c+2); 29 | if(c[0].st+1440-c[1].end>=720||c[1].st-c[0].end>=720) cout<<"2\n"; 30 | else cout<<"4\n"; 31 | } 32 | else{ 33 | sort(j,j+2); 34 | if(j[0].st+1440-j[1].end>=720||j[1].st-j[0].end>=720) cout<<"2\n"; 35 | else cout<<"4\n"; 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Code Jam/Round 1C 2017/Round 1C 2017 problem C(small).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | ifstream cin("input"); 5 | ofstream cout("output"); 6 | int t,n,k; 7 | double u,p[51]; 8 | cin>>t; 9 | for(int C=1;C<=t;C++){ 10 | cin>>n>>k>>u; 11 | for(int i=0;i>p[i]; 13 | } 14 | sort(p,p+n,greater()); 15 | double l=0,r=1,m; 16 | while(r-l>.000000001){ 17 | m=(r+l)/2; 18 | double sum=0; 19 | for(int i=0;i 2 | using namespace std; 3 | vector v[10]; 4 | bool is_found=false; 5 | string s[2000]; 6 | int n,l; 7 | void solve(string s_con,int in){ 8 | if(in==l){ 9 | for(int i=0;i>t; 27 | for(int c=1;c<=t;c++){ 28 | cin>>n>>l; 29 | set tot_char[10]; 30 | for(int i=0;i>s[i]; 32 | for(int j=0;j::iterator it=tot_char[i].begin();it!=tot_char[i].end();++it){ 38 | v[i].push_back(*it); 39 | } 40 | } 41 | is_found=false; 42 | cout<<"Case #"< 2 | using namespace std; 3 | typedef long long int lli; 4 | lli H=1000000000000000000; 5 | lli w[100001]; 6 | lli dp[100001][141]; 7 | lli solve(int in, int cnt){ 8 | if(cnt==0) 9 | return 0; 10 | if(in<0||cnt>in+1){ 11 | return H/1000; 12 | } 13 | if(dp[in][cnt]!=H) 14 | return dp[in][cnt]; 15 | lli w1=solve(in-1,cnt-1); 16 | if(w1<=6*w[in]) 17 | dp[in][cnt]=w1+w[in]; 18 | w1=solve(in-1,cnt); 19 | return dp[in][cnt]=min(dp[in][cnt],w1); 20 | } 21 | int main(){ 22 | int t; 23 | cin>>t; 24 | for(int c=1;c<=t;c++){ 25 | int n; 26 | scanf("%d",&n); 27 | for(int i=0;i>w[i]; 29 | } 30 | for(int i=0;i0;i--){ 34 | if(solve(n-1,i) 2 | using namespace std; 3 | string m; 4 | int t,nt=0,x,y,cx,cy,l; 5 | int solve(){ 6 | cx=x,cy=y; 7 | l=m.length(); 8 | for(int i=0;i>t; 19 | while(t--){ 20 | cin>>x>>y>>m; 21 | int ans=solve(); 22 | cout<<"Case #"<<++nt<<": "; 23 | if(ans<0) puts("IMPOSSIBLE"); 24 | else cout< 2 | using namespace std; 3 | int vol[10000],t,n,x,cnt,last,first; 4 | int main(){ 5 | freopen("input.txt","r",stdin); 6 | freopen("output.txt","w",stdout); 7 | cin>>t; 8 | for(int c=1;c<=t;c++){ 9 | scanf("%d%d",&n,&x); 10 | for(int i=0;i=first){ 15 | if(vol[last]+vol[first]<=x){ 16 | first++; 17 | } 18 | last--; 19 | cnt++; 20 | } 21 | printf("Case #%d: %d\n",c,cnt); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Code Jam/Round 2 2017/Round 2 2017 Problem A(large).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | ifstream cin("input"); 7 | ofstream cout("output"); 8 | int t; 9 | cin>>t; 10 | for(int c=1;c<=t;c++){ 11 | cout<<"Case #"<>n>>p; 15 | for(int i=0;i>a[i]; 17 | for(int i=0;i0){ 36 | re+=(rest+3)/4; 37 | } 38 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | int main(){ 6 | ifstream cin("input"); 7 | ofstream cout("output"); 8 | int t; 9 | cin>>t; 10 | for(int c=1;c<=t;c++){ 11 | cout<<"Case #"<>n>>p; 14 | for(int i=0;i>a[i]; 16 | if(p==2){ 17 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | ifstream cin("input"); 5 | ofstream cout("output"); 6 | int T; 7 | cin>>T; 8 | for(int C=1;C<=T;C++){ 9 | cout<<"Case #"<>n>>c>>m; 13 | for(int i=0;i<=n;i++){ 14 | one[i]=0; 15 | two[i]=0; 16 | sz[i]=0; 17 | } 18 | for(int i=0;i>p>>b; 20 | if(b==1){ 21 | one[p]++; 22 | sz[p]++; 23 | } 24 | else{ 25 | two[p]++; 26 | sz[p]++; 27 | } 28 | } 29 | for(int i=1;i<=n;i++){ 30 | while(one[i]!=0){ 31 | int t_sz=-1,in=-1; 32 | ride++; 33 | one[i]--; 34 | sz[i]--; 35 | found=false; 36 | for(int k=i+1;k<=n;k++){ 37 | if(two[k]>0){ 38 | if(sz[k]>t_sz||(sz[k]==t_sz&&in!=-1&&two[in]0){ 46 | two[i]--; 47 | tr++; 48 | sz[i]--; 49 | } 50 | else if(found){ 51 | sz[in]--; 52 | two[in]--; 53 | } 54 | } 55 | while(two[i]!=0){ 56 | ride++; 57 | two[i]--; 58 | sz[i]--; 59 | int t_sz=-1,in=-1; 60 | found=false; 61 | for(int k=i+1;k<=n;k++){ 62 | if(one[k]>0){ 63 | if(sz[k]>t_sz||(sz[k]==t_sz&&in!=-1&&one[in]0){ 71 | one[i]--; 72 | tr++; 73 | sz[i]--; 74 | } 75 | else if(found){ 76 | sz[in]--; 77 | one[in]--; 78 | } 79 | } 80 | } 81 | cout< 2 | using namespace std; 3 | typedef pair ii; 4 | typedef vector vi; 5 | int sq(int a){ 6 | return a*a; 7 | } 8 | int main(){ 9 | freopen("input","r",stdin); 10 | freopen("output","w",stdout); 11 | int t,n,s,x[1001],y[1001],z[1001],u,v; 12 | double w,val=0; 13 | cin>>t; 14 | for(int C=1;C<=t;C++){ 15 | cin>>n>>s; 16 | vi grid[n]; 17 | vector dist(1001,(double)10000000); 18 | for(int i=0;i>x[i]>>y[i]>>z[i]>>w>>w>>w; 20 | } 21 | for(int i=0;i,greater >Q; 27 | Q.push(ii(0,0)); 28 | dist[0]=0; 29 | while(!Q.empty()){ 30 | ii top=Q.top(); 31 | u=top.second; 32 | double d=top.first; 33 | Q.pop(); 34 | if(dist[u]==d) 35 | for(int i=0;i 2 | using namespace std; 3 | float sq(float a){ 4 | return a*a; 5 | } 6 | int main(){ 7 | freopen("input","r",stdin); 8 | freopen("output","w",stdout); 9 | int t,n,s; 10 | float x[1001],y[1001],z[1001],v,grid[1001][1001]; 11 | cin>>t; 12 | for(int C=1;C<=t;C++){ 13 | cin>>n>>s; 14 | for(int i=0;i>x[i]>>y[i]>>z[i]>>v>>v>>v; 16 | } 17 | for(int i=0;i 2 | using namespace std; 3 | int parent[1001],rank[1001]; 4 | int sq(int a){ 5 | return a*a; 6 | } 7 | int root(int a){ 8 | if(parent[a]!=a) parent[a]=root(parent[a]); 9 | return parent[a]; 10 | } 11 | void connect(int u,int v){ 12 | int ru=root(u); 13 | int rv=root(v); 14 | if(ru==rv) return; 15 | if(rank[ru]>rank[rv]){ 16 | parent[rv]=parent[ru]; 17 | rank[ru]+=rank[rv]; 18 | } 19 | else{ 20 | parent[ru]=parent[rv]; 21 | rank[rv]+=rank[ru]; 22 | } 23 | } 24 | int main(){ 25 | freopen("input","r",stdin); 26 | freopen("output","w",stdout); 27 | int t,n,s,x[1001],y[1001],z[1001]; 28 | double w,grid[1001][1001],ans; 29 | cin>>t; 30 | for(int C=1;C<=t;C++){ 31 | cin>>n>>s; 32 | for(int i=0;i>x[i]>>y[i]>>z[i]>>w>>w>>w; 35 | } 36 | for(int i=0;i > >Q; 40 | for(int i=0;i 2 | using namespace std; 3 | int fact(int a){ 4 | int re=1; 5 | for(int i=2;i<=a;i++) 6 | re*=i; 7 | return re; 8 | } 9 | int cnt; 10 | void solve(vectorv){ 11 | cnt++; 12 | int sum=0; 13 | vectorn; 14 | mapmp; 15 | for(int i=0;i<10;i++) 16 | mp[i]=0; 17 | for(int i=0;iv.size()) return; 24 | while(n.size()v.size()){ 31 | int add=fact(v.size()); 32 | for(int i=0;i<10;i++){ 33 | add/=fact(mp[i]); 34 | } 35 | cnt+=add; 36 | return; 37 | } 38 | sort(n.begin(),n.end()); 39 | do{ 40 | bool self_decay=true; 41 | for(int i=0;i>test; 54 | for(int Case=1;Case<=test;Case++){ 55 | string n; 56 | cnt=0; 57 | vectorv; 58 | cin>>n; 59 | for(int i=0;i 2 | using namespace std; 3 | int cnt; 4 | void prnt(vector v){ 5 | for(int i=0;iv){ 10 | cnt++; 11 | vectorn; 12 | for(int i=0;iv.size()) return; 18 | while(n.size()>test; 38 | for(int Case=1;Case<=test;Case++){ 39 | string n; 40 | cnt=0; 41 | vectorv; 42 | cin>>n; 43 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int t,n,b,a[100001],tot; 5 | cin>>t; 6 | for(int nt=1;nt<=t;nt++){ 7 | cin>>n>>b; 8 | for(int i=0;i>a[i]; 9 | sort(a,a+n); 10 | int i; 11 | tot=0; 12 | for(i=0;ib) break; 14 | tot+=a[i]; 15 | } 16 | cout<<"Case #"< 2 | using namespace std; 3 | int solve(){ 4 | int n,k; 5 | cin>>n>>k; 6 | string s[n]; 7 | for(int i=0;i>s[i]; 8 | map cntPrefix; 9 | for(int i=0;i>t; 23 | while(t--) cout<<"Case #"<<++nt<<": "< 2 | using namespace std; 3 | int n,k,ans; 4 | struct tri{ 5 | tri* nxt[26]; 6 | int cnt=0; 7 | }; 8 | void construct(tri* root, string& s, int id){ 9 | if(id==(int)s.length()) return; 10 | if(root->nxt[s[id]-'A']==NULL) root->nxt[s[id]-'A']=new tri(); 11 | root->nxt[s[id]-'A']->cnt++; 12 | construct(root->nxt[s[id]-'A'],s,id+1); //I love recursion 13 | } 14 | void get(tri* root){ 15 | if(root==NULL) return; 16 | ans+=root->cnt/k; 17 | for(int i=0;i<26;i++) get(root->nxt[i]); 18 | } 19 | int solve(){ 20 | cin>>n>>k; 21 | string s[n]; 22 | for(int i=0;i>s[i]; 23 | tri* root=new tri(); 24 | for(string a:s) construct(root,a,0); 25 | ans=0; 26 | get(root); 27 | return ans; 28 | } 29 | int main(){ 30 | int t,nt=0; 31 | cin>>t; 32 | while(t--) cout<<"Case #"<<++nt<<": "< 2 | using namespace std; 3 | int n,k,p,b[51][31],dp[51][31][1501]; 4 | int solve(int i,int j,int al){ 5 | if(i==n or al==p) return 0; 6 | if(j==k) return solve(i+1,0,al); 7 | if(dp[i][j][al]!=-1) return dp[i][j][al]; 8 | int cur=b[i][j]+solve(i,j+1,al+1); 9 | int nxt=solve(i+1,0,al); 10 | return dp[i][j][al]=max(cur,nxt); 11 | } 12 | int main(){ 13 | int t; 14 | cin>>t; 15 | for(int nt=1;nt<=t;nt++){ 16 | cin>>n>>k>>p; 17 | for(int i=0;i>b[i][j]; 20 | } 21 | } 22 | for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) for(int w=0;w<=p;w++) dp[i][j][w]=-1; 23 | cout<<"Case #"< 2 | using namespace std; 3 | int main(){ 4 | int t,n,k,m[100000]; 5 | cin>>t; 6 | for(int nt=1;nt<=t;nt++){ 7 | cin>>n>>k; 8 | for(int i=0;i>m[i]; 10 | } 11 | vectordif; 12 | for(int i=1;imid){ 19 | req+=(a+mid-1)/mid-1; 20 | } 21 | } 22 | if(req>k) lo=mid+1; 23 | else hi=mid; 24 | } 25 | cout<<"Case #"< 2 | using namespace std; 3 | int main(){ 4 | int t,nt=0,n,h[100]; 5 | cin>>t; 6 | while(t--){ 7 | cin>>n; 8 | for(int i=0;i>h[i]; 9 | int ans=0; 10 | for(int i=1;ih[i-1] and h[i]>h[i+1]) ans++; 12 | } 13 | cout<<"Case #"<<++nt<<": "< 2 | using namespace std; 3 | typedef long long int LL; 4 | int main(){ 5 | int t,nt=0,n; 6 | LL d,x[1001]; 7 | cin>>t; 8 | while(t--){ 9 | cin>>n>>d; 10 | for(int i=0;i>x[i]; 11 | reverse(x,x+n); 12 | for(int i=0;i 2 | using namespace std; 3 | typedef long long int LL; 4 | LL M=1e9; 5 | int n; 6 | struct cord{ 7 | LL x; 8 | LL y; 9 | }; 10 | mapmp; 11 | string s; 12 | cord get(int id){ 13 | LL mult=s[id]-'0'; 14 | int n=mp[id]; 15 | id++; 16 | cord cur={0LL,0LL}; 17 | for(int i=id;i='1'){ 19 | cord ret=get(i); 20 | cur.x+=ret.x; 21 | cur.y+=ret.y; 22 | i=mp[i]; 23 | continue; 24 | } 25 | if(s[i]=='N') cur.y--; 26 | if(s[i]=='S') cur.y++; 27 | if(s[i]=='E') cur.x++; 28 | if(s[i]=='W') cur.x--; 29 | if(cur.x<0) cur.x+=M; 30 | if(cur.y<0) cur.y+=M; 31 | cur.x%=M; 32 | cur.y%=M; 33 | i++; 34 | } 35 | return {cur.x*mult%M,cur.y*mult%M}; 36 | } 37 | void solve(){ 38 | cin>>s; 39 | s="1("+s+")"; 40 | n=s.length(); 41 | stack st; 42 | for(int i=1;i>t; 52 | while(t--){ 53 | cout<<"Case #"<<++nt<<": "; 54 | solve(); 55 | } 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Kick Start/Kick Start 2020/Round B/Wandering.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define N 200001 4 | double l2[N],lfac[N]; 5 | double prob(int x,int y){ 6 | return exp(lfac[x+y]-lfac[x]-lfac[y]-l2[x+y]); 7 | } 8 | double solve(){ 9 | int w,h,l,u,r,d; 10 | cin>>w>>h>>l>>u>>r>>d; 11 | w--,h--,l--,u--,r--,d--; 12 | double ans=0.0; 13 | int lx=l,gy=d; 14 | while(lx>0 and d=h) gy=h-1,m=0.5; 18 | ans+=prob(lx,gy)*m; 19 | } 20 | int rx=r,sy=u; 21 | while(sy>0 and r=w) rx=w-1,m=0.5; 25 | ans+=prob(rx,sy)*m; 26 | } 27 | return ans; 28 | } 29 | int main(){ 30 | lfac[0]=0.0; 31 | for(int i=1;i>t; 36 | while(t--) cout<<"Case #"<<++nt<<": "< 2 | using namespace std; 3 | int main(){ 4 | int t,nt=0; 5 | cin>>t; 6 | while(t--){ 7 | int n,k; 8 | cin>>n>>k; 9 | int a[n]; 10 | for(int i=0;i>a[i]; 11 | int ans=0; 12 | for(int i=0;i 2 | using namespace std; 3 | typedef long long int LL; 4 | #define N 200001 5 | int n,q,a[N]; 6 | LL mult[3*N],normal[3*N]; 7 | void build(int node, int l, int r){ 8 | if(l==r){ 9 | normal[node]=a[l]; 10 | mult[node]=l*a[l]; 11 | return; 12 | } 13 | int left=node<<1; 14 | int right=left|1; 15 | int mid=(l+r)>>1; 16 | build(left,l,mid); 17 | build(right,mid+1,r); 18 | normal[node]=normal[left]+normal[right]; 19 | mult[node]=mult[left]+mult[right]; 20 | } 21 | void update(int node, int l, int r, int id, int val){ 22 | if(idr) return; 23 | if(l==r){ 24 | normal[node]=val; 25 | mult[node]=l*val; 26 | return; 27 | } 28 | int left=node<<1; 29 | int right=left|1; 30 | int mid=(l+r)>>1; 31 | update(left,l,mid,id,val); 32 | update(right,mid+1,r,id,val); 33 | mult[node]=mult[left]+mult[right]; 34 | normal[node]=normal[left]+normal[right]; 35 | } 36 | LL query(int node, int st, int en, int l, int r){ 37 | if(ren) return 0; 38 | if(st<=l and r<=en) return mult[node]-normal[node]*(st-1); 39 | int left=node<<1; 40 | int right=left|1; 41 | int mid=(l+r)>>1; 42 | return query(left,st,en,l,mid)+query(right,st,en,mid+1,r); 43 | } 44 | LL getRangeSum(int l, int r){ 45 | LL sum=query(1,l,r,1,n); 46 | if(l%2==0) sum*=-1; 47 | return sum; 48 | } 49 | 50 | LL solve(){ 51 | scanf("%d%d",&n,&q); 52 | for(int i=1;i<=n;i++) scanf("%d",a+i); 53 | for(int i=1;i<=n;i++) if(i%2==0) a[i]*=-1; 54 | build(1,1,n); 55 | LL tot=0; 56 | while(q--){ 57 | char type; 58 | cin>>type; 59 | if(type=='U'){ 60 | int x,v; 61 | scanf("%d%d",&x,&v); 62 | if(x%2==0) v*=-1; 63 | update(1,1,n,x,v); 64 | } 65 | else{ 66 | int l,r; 67 | scanf("%d%d",&l,&r); 68 | tot+=getRangeSum(l,r); 69 | } 70 | } 71 | return tot; 72 | } 73 | int main(){ 74 | int t,nt=1; 75 | cin>>t; 76 | while(t--) cout<<"Case #"< 2 | using namespace std; 3 | int main(){ 4 | int t,nt=0; 5 | cin>>t; 6 | while(t--){ 7 | int n,k; 8 | cin>>n>>k; 9 | int a[n]; 10 | for(int i=0;i>a[i]; 11 | int ans=0; 12 | for(int i=0;i 2 | using namespace std; 3 | typedef long long int LL; 4 | LL solve(){ 5 | int n; 6 | scanf("%d",&n); 7 | int a[n],ma=0; 8 | for(int i=0;imp(2*ma+1); 13 | //for(int i=ma;i>t; 32 | while(t--) cout<<"Case #"<<++nt<<": "< 2 | using namespace std; 3 | string wall[30]; 4 | int r,c; 5 | bool already[26]; 6 | bool possible(char tar){ 7 | for(int i=0;i>r>>c; 17 | for(int i=0;i>wall[i]; 18 | for(auto &v:already) v=true; 19 | for(int i=0;i>t; 38 | while(t--) cout<<"Case #"< 2 | using namespace std; 3 | void solve(){ 4 | int n,a,b,c; 5 | cin>>n>>a>>b>>c; 6 | if(a+b-c>n or c>a or c>b or (a==1 and b==1 and n>1)){ 7 | puts("IMPOSSIBLE"); 8 | return; 9 | } 10 | vector as,bs,cs; 11 | for(int i=0;i res; 16 | for(int a:as){ 17 | res.push_back(a); 18 | as.pop_back(); 19 | break; 20 | } 21 | for(int c:cs){ 22 | if(!res.empty()) break; 23 | res.push_back(c); 24 | cs.pop_back(); 25 | } 26 | for(int i=0;i>t; 36 | while(t--){ 37 | cout<<"Case #"<<++nt<<": "; 38 | solve(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Kick Start/Kick Start 2020/Round E/Longest Arithmetic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void solve(){ 4 | int n,ans=2; 5 | cin>>n; 6 | int a[n]; 7 | for(int i=0;i>a[i]; 8 | for(int i=0;i>t; 19 | while(t--){ 20 | cout<<"Case #"<<++nt<<": "; 21 | solve(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round A 2019/Parcels.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long int LL; 4 | struct node{ 5 | int x,y,d; 6 | }; 7 | int dx[]={1,0,-1,0}; 8 | int dy[]={0,1,0,-1}; 9 | string grid[255]; 10 | int t,r,c; 11 | int dist[255][255]; 12 | bool is_valid(int x,int y){ 13 | if(x>=r||y>=c||x<0||y<0) return false; 14 | return true; 15 | } 16 | void calc_distance(){ 17 | queueq; 18 | for(int x=0;xcur.d+1){ 34 | q.push({x,y,cur.d+1}); 35 | dist[x][y]=cur.d+1; 36 | } 37 | } 38 | } 39 | } 40 | bool is_ok(int k){ 41 | int max_sum=-501,max_diff=-501,min_sum=501,min_diff=501; 42 | for(int i=0;ik){ 45 | max_sum=max(max_sum,i+j); 46 | min_sum=min(min_sum,i+j); 47 | max_diff=max(max_diff,i-j); 48 | min_diff=min(min_diff,i-j); 49 | } 50 | } 51 | } 52 | for(int i=0;ik) is_ok=false; 56 | if(max_diff-(i-j)>k) is_ok=false; 57 | if(i+j-min_sum>k) is_ok=false; 58 | if(i-j-min_diff>k) is_ok=false; 59 | if(is_ok) return true; 60 | } 61 | } 62 | return false; 63 | } 64 | 65 | int main(){ 66 | cin>>t; 67 | for(int nt=1;nt<=t;nt++){ 68 | cin>>r>>c; 69 | for(int i=0;i>grid[i]; 71 | for(int i=0;ilo){ 75 | k=lo+(hi-lo)/2; 76 | if(is_ok(k)) hi=k; 77 | else lo=k+1; 78 | } 79 | cout<<"Case #"< 2 | using namespace std; 3 | typedef long long int LL; 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int nt=1;nt<=t;nt++){ 8 | int n,p; 9 | cin>>n>>p; 10 | LL a[n]; 11 | for(int i=0;i 2 | #define N 100001 3 | int L[N][26]; 4 | using namespace std; 5 | int main(){ 6 | int t,tc=0,n,q,ans,l,r,cnt; 7 | string s; 8 | scanf("%d",&t); 9 | while(t--){ 10 | scanf("%d%d",&n,&q); 11 | cin>>s; 12 | for(int i=0;i<=n;i++) for(int j=0;j<26;j++) L[i][j]=0; 13 | for(int i=1;i<=n;i++){ 14 | for(int j=0;j<26;j++) L[i][j]=L[i-1][j]; 15 | L[i][s[i-1]-'A']++; 16 | } 17 | ans=0; 18 | while(q--){ 19 | scanf("%d%d",&l,&r); 20 | cnt=0; 21 | for(int i=0;i<26;i++) if((L[r][i]-L[l-1][i])%2) cnt++; 22 | if((r-l+1)%2==cnt) ans++; 23 | } 24 | printf("Case #%d: %d\n",++tc,ans); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round B 2019 /Energy Stones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct stone { 4 | int s, e, l; 5 | }; 6 | int t, n, s, e, l,dp[100][10001]; 7 | stone st[100]; 8 | int solve(int id, int time) { 9 | if (id == n) return 0; 10 | int & ans = dp[id][time]; 11 | if (ans != -1) 12 | return ans; 13 | ans = max(solve(id + 1, time + st[id].s) + st[id].e - st[id].l * time, solve(id + 1, time)); 14 | return ans; 15 | } 16 | int main() { 17 | scanf("%d", & t); 18 | for (int nt = 1; nt <= t; nt++) { 19 | scanf("%d", & n); 20 | for (int i = 0; i < n; i++) { 21 | scanf("%d%d%d", & s, & e, & l); 22 | st[i] = {s,e,l}; 23 | } 24 | sort(st, st + n, [](stone a, stone b) { 25 | return a.s * b.l < b.s * a.l; 26 | }); 27 | for(int i = 0; i < n; i++) for(int j = 0; j <= 10000; j++) dp[i][j] = -1; 28 | cout << "Case #" << nt << ": " << solve(0,0) << "\n"; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round C 2019/Circuit Board.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int t, r, c, k, v[301][301], hist[301], stmn[301][301][25], stmx[301][301][25], ans = 0, _log2[301]; 6 | 7 | int get_min(int i, int l, int r) { 8 | int j = _log2[r - l + 1]; 9 | return min(stmn[i][l][j], stmn[i][r - (1 << j) + 1][j]); 10 | } 11 | 12 | int get_max(int i, int l, int r) { 13 | int j = _log2[r - l + 1]; 14 | return max(stmx[i][l][j], stmx[i][r - (1 << j) + 1][j]); 15 | } 16 | 17 | int get_mx_rect_area_of_histo(int j) { 18 | for (int i = 0; i < r; i++) { 19 | int cnt = 0; 20 | for (int l = j + hist[i]; l < c and get_max(i, j, l) <= get_min(i, j, l) + k; l++) 21 | cnt++; 22 | hist[i] += cnt; 23 | } 24 | stack < int > st; 25 | int id = 0, area, mxarea = 0, x; 26 | while (id < r) { 27 | if (st.empty() or hist[st.top()] <= hist[id]) { 28 | st.push(id); 29 | id++; 30 | } else { 31 | x = st.top(); 32 | st.pop(); 33 | if (st.empty()) 34 | area = id * hist[x]; 35 | else 36 | area = (id - st.top() - 1) * hist[x]; 37 | mxarea = max(area, mxarea); 38 | } 39 | } 40 | while (!st.empty()) { 41 | x = st.top(); 42 | st.pop(); 43 | if (st.empty()) 44 | area = r * hist[x]; 45 | else 46 | area = (r - st.top() - 1) * hist[x]; 47 | mxarea = max(mxarea, area); 48 | } 49 | return mxarea; 50 | } 51 | 52 | int main() { 53 | _log2[1] = 0; 54 | for (int i = 2; i < 301; i++) _log2[i] = _log2[i / 2] + 1; 55 | cin >> t; 56 | for (int nt = 1; nt <= t; nt++) { 57 | cin >> r >> c >> k; 58 | ans = 0; 59 | for (int i = 0; i < r; i++) { 60 | hist[i] = 0; 61 | for (int j = 0; j < c; j++) { 62 | cin >> v[i][j]; 63 | } 64 | } 65 | for (int i = 0; i < r; i++) { 66 | for (int j = 0; j < c; j++) { 67 | stmn[i][j][0] = stmx[i][j][0] = v[i][j]; 68 | } 69 | for (int k = 1; k < 25; k++) { 70 | for (int j = 0; j + (1 << k) <= c; j++) { 71 | stmn[i][j][k] = min(stmn[i][j][k - 1], stmn[i][j + (1 << (k - 1))][k - 1]); 72 | stmx[i][j][k] = max(stmx[i][j][k - 1], stmx[i][j + (1 << (k - 1))][k - 1]); 73 | } 74 | } 75 | } 76 | for (int j = 0; j < c; j++) { 77 | ans = max(ans, get_mx_rect_area_of_histo(j)); 78 | for (int i = 0; i < r; i++) hist[i]--; 79 | } 80 | cout << "Case #" << nt << ": " << ans << "\n"; 81 | } 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round C 2019/Wiggle Walk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | map < char, pair < int, int > > dir; 4 | int main() { 5 | int t; 6 | dir['N']={-1,0}; 7 | dir['S']={1,0}; 8 | dir['W']={0,-1}; 9 | dir['E']={0,1}; 10 | cin >> t; 11 | for (int nt = 1; nt <= t; nt++) { 12 | int n, r, c, x, y; 13 | cin >> n >> r >> c >> x >> y; 14 | string s; 15 | cin >> s; 16 | unordered_set < int > us[r + 1]; 17 | us[x].insert(y); 18 | for (int i = 0; i < n; i++) { 19 | int dx = dir[s[i]].first; 20 | int dy = dir[s[i]].second; 21 | x += dx; 22 | y += dy; 23 | while (us[x].find(y) != us[x].end()) { 24 | x += dx; 25 | y += dy; 26 | } 27 | us[x].insert(y); 28 | } 29 | cout << "Case #" << nt << ": " << x << " " << y << "\n"; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round D 2019/Food Stalls (small).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define S 100001 //size of the array 4 | using namespace std; 5 | typedef long long int ll; 6 | 7 | int k, n; 8 | struct point { 9 | ll x, c; 10 | bool operator < (point o) { 11 | return x < o.x; 12 | } 13 | }; 14 | point p[S]; 15 | 16 | int get_cost(int i, int j) { 17 | return p[i].c + abs(p[i].x - p[j].x); 18 | } 19 | 20 | int main() { 21 | int t; 22 | cin >> t; 23 | for (int nt = 1; nt <= t; ++nt) { 24 | cin >> k >> n; 25 | for (int i = 0; i < n; i++) 26 | cin >> p[i].x; 27 | for (int i = 0; i < n; i++) 28 | cin >> p[i].c; 29 | sort(p, p + n); 30 | ll ans = LLONG_MAX; 31 | ll dp[101][101]; 32 | for (int j = 1; j < n; j++) { 33 | for (int i = 0; i < n; i++) 34 | dp[i][0] = 0; 35 | for (int i = 0; i < n; i++) 36 | for (int l = 1; l <= k; l++) 37 | dp[i][l] = LLONG_MAX / 2; 38 | 39 | dp[0][1] = get_cost(0, j); 40 | for (int i = 1; i < n; i++) { 41 | if (i == j) { 42 | for (int l = 1; l <= k; l++) 43 | dp[i][l] = dp[i - 1][l]; 44 | continue; 45 | } 46 | for (int l = 1; l <= k; l++) 47 | dp[i][l] = min(dp[i - 1][l - 1] + get_cost(i, j), dp[i - 1][l]); 48 | } 49 | ans = min(ans, dp[n - 1][k] + p[j].c); 50 | } 51 | cout << "Case #" << nt << ": " << ans << "\n"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round D 2019/Food Stalls.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define S 100001 //size of the array 4 | using namespace std; 5 | typedef long long int ll; 6 | int k, n; 7 | struct point { 8 | ll x, c; 9 | bool operator < (point o) { 10 | return x < o.x; 11 | } 12 | }; 13 | point p[S]; 14 | ll get_cost(int i, int j) { 15 | return p[i].c + abs(p[i].x - p[j].x); 16 | } 17 | int main() { 18 | int t; 19 | cin >> t; 20 | for (int nt = 1; nt <= t; ++nt) { 21 | cin >> k >> n; 22 | for (int i = 0; i < n; i++) 23 | cin >> p[i].x; 24 | for (int i = 0; i < n; i++) 25 | cin >> p[i].c; 26 | sort(p, p + n); 27 | ll ans = LLONG_MAX; 28 | priority_queue < ll > pq; 29 | ll sum = 0; 30 | vector < ll > b(n), e(n); 31 | for (int i = 0; i < n; i++) 32 | b[i] = e[i] = LLONG_MAX / 2; 33 | int fm = k / 2; 34 | int bm = k - k / 2; 35 | for (int i = 0; i < fm; i++) { 36 | sum += get_cost(i, n - 1); 37 | pq.push(get_cost(i, n - 1)); 38 | } 39 | for (int i = fm; i < n; i++) { 40 | b[i] = sum - fm * (p[n - 1].x - p[i].x); 41 | if (!pq.empty()) { 42 | ll mx = pq.top(); 43 | if (mx > get_cost(i, n - 1)) { 44 | pq.pop(); 45 | sum -= (mx - get_cost(i, n - 1)); 46 | pq.push(get_cost(i, n - 1)); 47 | } 48 | } 49 | } 50 | while (!pq.empty()) 51 | pq.pop(); 52 | sum = 0; 53 | for (int i = 0; i < bm; i++) { 54 | sum += get_cost(n - i - 1, 0); 55 | pq.push(get_cost(n - 1 - i, 0)); 56 | } 57 | for (int i = n - bm - 1; i >= 0; i--) { 58 | e[i] = sum - bm * (p[i].x - p[0].x); 59 | if (!pq.empty()) { 60 | ll mx = pq.top(); 61 | if (mx > get_cost(i, 0)) { 62 | pq.pop(); 63 | sum -= (mx - get_cost(i, 0)); 64 | pq.push(get_cost(i, 0)); 65 | } 66 | } 67 | } 68 | for (int i = 0; i < n; i++) 69 | ans = min(ans, b[i] + e[i] + p[i].c); 70 | cout << "Case #" << nt << ": " << ans << "\n"; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round D 2019/Latest Guest (small).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define S 100001 4 | using namespace std; 5 | int h[S], n, g, m, t; 6 | char c[S]; 7 | int new_pos(int a, char c) { 8 | a = (c == 'C' ? a + 1 : a - 1); 9 | return (a + n) % n; 10 | } 11 | int main() { 12 | cin >> t; 13 | for (int nt = 1; nt <= t; nt++) { 14 | cin >> n >> g >> m; 15 | vector < int > cnt(g, 0); 16 | vector < set < int >> s(n); 17 | for (int i = 0; i < g; i++) { 18 | cin >> h[i] >> c[i]; 19 | h[i]--; 20 | cnt[i] = 0; 21 | } 22 | for (int j = 0; j < g; j++) { 23 | s[h[j]].insert(j); 24 | } 25 | while (m--) { 26 | for (int i = 0; i < g; i++) { 27 | h[i] = new_pos(h[i], c[i]); 28 | s[h[i]].clear(); 29 | } 30 | for (int j = 0; j < g; j++) { 31 | s[h[j]].insert(j); 32 | } 33 | } 34 | for (int i = 0; i < n; i++) 35 | for (set < int > ::iterator it = s[i].begin(); it != s[i].end(); ++it) 36 | cnt[ * it]++; 37 | cout << "Case #" << nt << ": "; 38 | for (int i = 0; i < g; i++) 39 | cout << cnt[i] << " "; 40 | puts(""); 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round D 2019/Latest Guest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define S 100001 4 | int h[S], n, g, m, t; 5 | char c[S]; 6 | int new_pos(int a, char c) { 7 | a = (c == 'C' ? a + m : a - m); 8 | return (a % n + n) % n; 9 | } 10 | struct Stat { 11 | int type, tme, grp; 12 | Stat(int t, int tm, int g) { 13 | type = t; 14 | tme = tm; 15 | grp = g; 16 | } 17 | bool operator < (Stat s) { 18 | return tme < s.tme; 19 | } 20 | }; 21 | int main() { 22 | cin >> t; 23 | for (int nt = 1; nt <= t; nt++) { 24 | cin >> n >> g >> m; 25 | vector < int > cnt(g, 0); 26 | vector < int > groups[2][n]; 27 | for (int i = 0; i < g; i++) { 28 | cin >> h[i] >> c[i]; 29 | h[i]--; 30 | int dest = new_pos(h[i], c[i]); 31 | groups[c[i] == 'A'][dest].push_back(i); 32 | } 33 | vector < Stat > cons[n]; 34 | for (int i = 0; i < n; i++) { 35 | int tm = 0; 36 | while (i < n and groups[0][i].size() == 0) i++; 37 | if (i == n) 38 | break; 39 | cons[i].push_back({ 40 | 0, 41 | tm++, 42 | i 43 | }); 44 | for (int j = i - 1; tm <= m; j--) { 45 | if (groups[0][(j + n) % n].size() > 0) 46 | break; 47 | cons[(j + n) % n].push_back({ 48 | 0, 49 | tm++, 50 | i 51 | }); 52 | } 53 | } 54 | for (int i = 0; i < n; i++) { 55 | int tm = 0; 56 | while (i < n and groups[1][i].size() == 0) i++; 57 | if (i == n) 58 | break; 59 | cons[i].push_back({ 60 | 1, 61 | tm++, 62 | i 63 | }); 64 | sort(cons[i].begin(), cons[i].end()); 65 | for (int j = i + 1; tm <= m; j++) { 66 | if (groups[1][j % n].size() > 0) 67 | break; 68 | cons[j % n].push_back({ 69 | 1, 70 | tm++, 71 | i 72 | }); 73 | sort(cons[j % n].begin(), cons[j % n].end()); 74 | } 75 | } 76 | for (int i = 0; i < n; i++) { 77 | if (cons[i].size() == 0) 78 | continue; 79 | int tm = cons[i][0].tme; 80 | for (auto ob: cons[i]) { 81 | if (tm != ob.tme) 82 | break; 83 | vector < int > v = groups[ob.type][ob.grp]; 84 | for (int id: v) 85 | cnt[id]++; 86 | } 87 | } 88 | cout << "Case #" << nt << ": "; 89 | for (int i = 0; i < g; i++) 90 | cout << cnt[i] << " "; 91 | puts(""); 92 | } 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round D 2019/X or What (small).cpp: -------------------------------------------------------------------------------- 1 | /* This is the implementation for small data set as stated in the analysis. The idea is very interesting */ 2 | #include 3 | using namespace std; 4 | 5 | int S = 1e5 + 1; 6 | bool is_odd(int a) { 7 | int cnt = 0; 8 | for (int i = 0; 1 << i <= a; i++) 9 | cnt += min(1, 1 << i & a); 10 | return cnt % 2; 11 | } 12 | int main() { 13 | int t, n, q, a[S], v, p, s[S + 1]; 14 | cin >> t; 15 | s[0] = 0; 16 | for (int nt = 1; nt <= t; nt++) { 17 | cin >> n >> q; 18 | for (int i = 0; i < n; i++) 19 | cin >> a[i]; 20 | for (int i = 1; i <= n; i++) 21 | s[i] = s[i - 1] xor a[i - 1]; 22 | cout << "Case #" << nt << ":"; 23 | for (int i = 0; i < q; i++) { 24 | cin >> p >> v; 25 | if (is_odd(a[p]) != is_odd(v)) { 26 | a[p] = v; 27 | for (int i = p + 1; i <= n; i++) 28 | s[i] = s[i - 1] xor a[i - 1]; 29 | } 30 | int ans = 0; 31 | for (int i = 0; i < n; i++) { 32 | for (int j = i + 1; j <= n; j++) { 33 | if (!is_odd(s[j] xor s[i])) 34 | ans = max(ans, j - i); 35 | } 36 | } 37 | cout << " " << ans; 38 | } 39 | cout << "\n"; 40 | 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round D 2019/X or What.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int S = 1e5 + 1; 5 | bool is_odd(int a) { 6 | int cnt = 0; 7 | for (int i = 0; 1 << i <= a; i++) 8 | cnt += min(1, 1 << i & a); 9 | return cnt % 2; 10 | } 11 | int main() { 12 | int t, n, q, a[S], v, p; 13 | cin >> t; 14 | for (int nt = 1; nt <= t; nt++) { 15 | cin >> n >> q; 16 | set < int > s; 17 | for (int i = 0; i < n; i++) { 18 | cin >> a[i]; 19 | if (is_odd(a[i])) 20 | s.insert(i); 21 | } 22 | cout << "Case #" << nt << ":"; 23 | for (int i = 0; i < q; i++) { 24 | cin >> p >> v; 25 | 26 | if (!is_odd(a[p]) and is_odd(v)) 27 | s.insert(p); 28 | 29 | else if (is_odd(a[p]) and!is_odd(v)) 30 | s.erase(s.find(p)); 31 | 32 | a[p] = v; 33 | 34 | if (s.size() % 2 == 0) 35 | cout << " " << n; 36 | 37 | else 38 | cout << " " << max(n - * s.begin() - 1, * s.rbegin()); 39 | } 40 | cout << "\n"; 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round E 2019/Cherries Mesh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define S 100001 4 | int n, m, parent[S], ans; 5 | bool vis[S]; 6 | void initialize() { 7 | for (int i = 1; i <= n; i++) { 8 | parent[i] = i; 9 | vis[i] = false; 10 | } 11 | ans = 0; 12 | } 13 | int root(int x) { 14 | if (parent[x] != x) 15 | parent[x] = root(parent[x]); 16 | return parent[x]; 17 | } 18 | int main() { 19 | int t, x, y; 20 | cin >> t; 21 | for (int nt = 1; nt <= t; nt++) { 22 | cin >> n >> m; 23 | initialize(); 24 | while (m--) { 25 | cin >> x >> y; 26 | x = root(x); 27 | y = root(y); 28 | if (x != y) { 29 | ans++; 30 | parent[x] = y; 31 | } 32 | } 33 | vis[root(1)] = true; 34 | for (int i = 2; i <= n; i++) { 35 | if (!vis[root(i)]) 36 | ans += 2; 37 | vis[root(i)] = true; 38 | } 39 | cout << "Case #" << nt << ": " << ans << "\n"; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round E 2019/Code-Eat Switcher.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define S 100001 4 | long double a, b, c, e; 5 | struct slot { 6 | long double c, e; 7 | }; 8 | int main() { 9 | int t, d, s; 10 | cin >> t; 11 | for (int nt = 1; nt <= t; nt++) { 12 | cout << "Case #" << nt << ": "; 13 | cin >> d >> s; 14 | slot sl[s]; 15 | for (int i = 0; i < s; i++) { 16 | cin >> c >> e; 17 | sl[i] = { 18 | c, 19 | e 20 | }; 21 | } 22 | sort(sl, sl + s, [](slot a, slot b) { 23 | return a.c / a.e > b.c / b.e; 24 | }); 25 | long double presum[s + 1], sufsum[s + 1]; 26 | presum[0] = 0; 27 | sufsum[0] = 0; 28 | for (int i = 0; i < s; i++) { 29 | presum[i + 1] = sl[i].c + presum[i]; 30 | sufsum[i + 1] = sl[s - 1 - i].e + sufsum[i]; 31 | } 32 | for (int i = 0; i < d; i++) { 33 | cin >> a >> b; 34 | if (presum[s] < a) { 35 | cout << "N"; 36 | continue; 37 | } 38 | int indx = (int)(upper_bound(presum, presum + s + 1, a) - presum); 39 | if (indx == s + 1) { 40 | if (b != 0) 41 | cout << "N"; 42 | else 43 | cout << "Y"; 44 | continue; 45 | } 46 | long double f = (presum[indx] - a) / sl[indx - 1].c; 47 | b -= f * sl[indx - 1].e; 48 | int eatindx = (int)(lower_bound(sufsum, sufsum + s + 1, b) - sufsum); 49 | if (eatindx + indx > s) { 50 | cout << "N"; 51 | continue; 52 | } 53 | cout << "Y"; 54 | } 55 | puts(""); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round E 2019/Street Checkers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define MAXN 1000000001 4 | # define S 31624 5 | bool is_comp[S]; 6 | vector < int > primes; 7 | int number_of_primes_in(int m, int n) { 8 | int cnt = 0; 9 | bool is_prime[n - m + 2]; 10 | for (int i = 0; i < n - m + 2; i++) is_prime[i] = true; 11 | for (unsigned long j = 0; j < primes.size() and primes[j] * primes[j] <= n; j++) { 12 | int p = primes[j]; 13 | int k = p * p; 14 | if (k < m) k = (m + p - 1) / p * p; 15 | for (; k <= n; k += p) 16 | is_prime[k - m] = false; 17 | } 18 | for (int i = 0; m + i <= n; i++) 19 | if (is_prime[i]) cnt++; 20 | return cnt; 21 | } 22 | int main() { 23 | int t, l, r, nt = 0; 24 | for (int i = 2; i * i < MAXN; i++) { 25 | if (!is_comp[i]) 26 | primes.push_back(i); 27 | for (unsigned long j = 0; j < primes.size() and primes[j] * i < S; j++) { 28 | is_comp[primes[j] * i] = true; 29 | if (i % primes[j] == 0) 30 | break; 31 | } 32 | } 33 | cin >> t; 34 | while (t--) { 35 | cin >> l >> r; 36 | int ans = 0; 37 | ans += number_of_primes_in(l, r); //Case 1 38 | ans += r / 2 - (l + 1) / 2 - r / 4 + (l + 3) / 4; //Case 2 39 | ans -= (r >= 2 and l <= 2); //if X=2 is counted in both case 1 and 2 40 | ans += number_of_primes_in((l + 3) / 4, r / 4); //Case 3 and 4. 41 | cout << "Case #" << ++nt << ": " << ans << "\n"; 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round G 2019/Book Reading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int t,n,m,q,bis[100001],p,r; 5 | cin>>t; 6 | for(int nt=1;nt<=t;nt++){ 7 | cin>>n>>m>>q; 8 | for(int i=0;i<100001;i++) bis[i]=0; 9 | while(m--){ 10 | cin>>p; 11 | int i=1; 12 | for(;i*i>r; 23 | tot+=n/r+bis[r]; 24 | } 25 | cout<<"Case #"< 2 | using namespace std; 3 | #define MAXN 60000 4 | typedef long long int LL; 5 | int T, en, st, n, h; 6 | LL a[20], b[20], target, A[MAXN]; 7 | vector < LL > t[4 * MAXN]; 8 | vector < pair < LL, LL > > gen[2]; 9 | void generate(int i, int part, LL ha, LL hb) { 10 | if (i == en) { 11 | gen[part].push_back(make_pair(ha, hb)); 12 | return; 13 | } 14 | generate(i + 1, part, ha + a[i], hb); 15 | generate(i + 1, part, ha, hb + b[i]); 16 | generate(i + 1, part, ha + a[i], hb + b[i]); 17 | } 18 | void build(int v, int tl, int tr) { 19 | t[v].clear(); 20 | if (tl == tr) { 21 | t[v].push_back(A[tl]); 22 | return; 23 | } 24 | int tm = (tl + tr) / 2; 25 | build(v * 2, tl, tm); 26 | build(v * 2 + 1, tm + 1, tr); 27 | merge(t[v * 2].begin(), t[v * 2].end(), t[v * 2 + 1].begin(), t[v * 2 + 1].end(), 28 | back_inserter(t[v])); 29 | } 30 | int query(int v, int tl, int tr, int l, int r, LL x) { 31 | if (tl > r or tr < l) return 0; 32 | if (tl >= l and tr <= r) return (t[v].end() - lower_bound(t[v].begin(), t[v].end(), x)); 33 | int mid = (tl + tr) / 2; 34 | int left = v * 2; 35 | int right = left | 1; 36 | return query(left, tl, mid, l, r, x) + query(right, mid + 1, tr, l, r, x); 37 | } 38 | int main() { 39 | cin >> T; 40 | for (int nt = 1; nt <= T; nt++) { 41 | cin >> n >> h; 42 | gen[0].clear(); 43 | gen[1].clear(); 44 | for (int i = 0; i < n; i++) cin >> a[i]; 45 | for (int i = 0; i < n; i++) cin >> b[i]; 46 | en = n / 2; 47 | generate(0, 0, 0, 0); 48 | en = n; 49 | generate(n / 2, 1, 0, 0); 50 | sort(gen[1].begin(), gen[1].end()); 51 | for (int i = 0; i < gen[1].size(); i++) A[i] = gen[1][i].second; 52 | build(1, 0, gen[1].size() - 1); 53 | LL ans = 0LL; 54 | for (int i = 0; i < gen[0].size(); i++) { 55 | target = h - gen[0][i].first; 56 | st = lower_bound(gen[1].begin(), gen[1].end(), make_pair(target, 0LL)) - gen[1].begin(); 57 | target = h - gen[0][i].second; 58 | ans += query(1, 0, gen[1].size() - 1, st, gen[1].size() - 1, target); 59 | } 60 | cout << "Case #" << nt << ": " << ans << "\n"; 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Kick Start/Kick Start Round H 2019/H-index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int t,n,a; 5 | cin>>t; 6 | for(int nt=1;nt<=t;nt++){ 7 | priority_queue, std::greater > pq; 8 | cin>>n; 9 | cout<<"Case #"<>a; 12 | pq.push(a); 13 | while(pq.top()<(int)pq.size()) 14 | pq.pop(); 15 | cout< 2 | using namespace std; 3 | int main(){ 4 | int tn; 5 | cin>>tn; 6 | for(int tc=1;tc<=tn;tc++){ 7 | int n,k; 8 | cin>>n>>k; 9 | double v[n]; 10 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | int main(){ 7 | freopen("input.txt","r",stdin); 8 | freopen("output.txt","w",stdout); 9 | int t; 10 | cin>>t; 11 | for(int c=1;c<=t;c++){ 12 | long long int n,np; 13 | cin>>n; 14 | vectorv; 15 | np=n; 16 | while(np){ 17 | v.push_back(np%10); 18 | np/=10; 19 | } 20 | int gr=-1; 21 | reverse(v.begin(),v.end()); 22 | for(int i=0;i 2 | using namespace std; 3 | int main(){ 4 | int tn; 5 | cin>>tn; 6 | for(int tc=1;tc<=tn;tc++){ 7 | int n,k; 8 | cin>>n>>k; 9 | double v[n]; 10 | double vs[n+1]; 11 | vs[0]=0; 12 | for(int i=0;i 2 | using namespace std; 3 | typedef long long int lli; 4 | 5 | lli getLegalUpto(lli n){ 6 | lli actual_n=n; 7 | lli good_shape=n-n%10; 8 | n = good_shape; 9 | lli cnt=0; 10 | vector ongko; 11 | while(n){ 12 | ongko.push_back(n%10); 13 | n/=10; 14 | } 15 | reverse(ongko.begin(),ongko.end()); 16 | for(int i=0;i>t; 31 | for(int c=1;c<=t;c++){ 32 | lli f,l; 33 | cin>>f>>l; 34 | cout<<"Case #"< 2 | using namespace std; 3 | int main(){ 4 | freopen("input.txt","r",stdin); 5 | freopen("output.txt","w",stdout); 6 | string s; 7 | int b[51],a[51]; 8 | int t,l; 9 | cin>>t; 10 | for(int i=1;i<=t;i++){ 11 | cin>>s; 12 | cout<<"Case #"<=0;i--){ 28 | if(a[i+1] 2 | using namespace std; 3 | int n,x,y; 4 | vectorv[1001],cycle; 5 | int parent[1001],dis[1001]; 6 | bool vis[1001],insrtd[1001]; 7 | bool fnd=false; 8 | int hlt; 9 | void create_cycle(int node){ 10 | cycle.push_back(node); 11 | insrtd[node]=true; 12 | dis[node]=0; 13 | if(node==hlt) 14 | return; 15 | create_cycle(parent[node]); 16 | return; 17 | } 18 | void prnt_cycle(int indx){ 19 | if(vis[indx]){ 20 | fnd=true; 21 | hlt=indx; 22 | create_cycle(parent[indx]); 23 | return; 24 | } 25 | vis[indx]=true; 26 | for(int i=0;i>t; 48 | for(int c=1;c<=t;c++){ 49 | cin>>n; 50 | fnd=false; 51 | cycle.clear(); 52 | for(int i=0;i>x>>y; 54 | v[x-1].push_back(y-1); 55 | v[y-1].push_back(x-1); 56 | parent[i]=-1; 57 | vis[i]=false; 58 | insrtd[i]=false; 59 | } 60 | prnt_cycle(0); 61 | for(int i=0;i 2 | using namespace std; 3 | int n,ts,tf,s[2001],f[2001],d[2001]; 4 | int dp(int in,int t){ 5 | if(t>tf) return -100000; 6 | if(in==n-1) return 0; 7 | int tmax=t+ts,tmin=t; 8 | for(int x=0;;x++){ 9 | if(s[in]+x*f[in]>=tmin){ 10 | tmin=s[in]+x*f[in]; 11 | break; 12 | } 13 | } 14 | int a=dp(in+1,tmin+d[in]); 15 | for(int x=0;;x++){ 16 | if(s[in]+x*f[in]>=tmax){ 17 | tmax=s[in]+x*f[in]; 18 | break; 19 | } 20 | } 21 | int b=1+dp(in+1,tmax+d[in]); 22 | return max(a,b); 23 | } 24 | int main(){ 25 | freopen("input","r",stdin); 26 | freopen("output","w",stdout); 27 | int T; 28 | cin>>T; 29 | for(int C=1;C<=T;C++){ 30 | scanf("%d%d%d",&n,&ts,&tf); 31 | for(int i=0;i 2 | using namespace std; 3 | struct details{ 4 | int cnt=0; 5 | long long int val; 6 | bool operator<(details o){ 7 | return cnt>o.cnt; 8 | } 9 | }; 10 | int main(){ 11 | freopen("input.txt","r",stdin); 12 | freopen("output.txt","w",stdout); 13 | int t; 14 | scanf("%d",&t); 15 | for(int c=1;c<=t;c++){ 16 | cout<<"Case #"<mp; 20 | long long int ans=0; 21 | scanf("%d",&n); 22 | for(int i=0;i2) 34 | last_three=i; 35 | if(cnt[i].cnt>1) 36 | last_two=i; 37 | } 38 | for(int i=0;i<=last_three;i++){ 39 | for(int j=0;j 2 | using namespace std; 3 | #define MAX 100001 4 | bool is_sq[MAX]; 5 | int dp[MAX]; 6 | int solve(int n){ 7 | if(is_sq[n]) return 1; 8 | if(dp[n]!=MAX) return dp[n]; 9 | for(int i=1;i<=n/2;i++) 10 | dp[n]=min(dp[n],solve(n-i)+solve(i)); 11 | return dp[n]; 12 | } 13 | int main(){ 14 | for(int i=1;i>t; 20 | for(int c=1;c<=t;c++){ 21 | cin>>n; 22 | cout<<"Case #"< 2 | using namespace std; 3 | int n,dpa[51][26],dpb[51][26]; 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int nt=1;nt<=t;nt++){ 8 | string a,b; 9 | cin>>n>>a>>b; 10 | for(int i=0;i 2 | using namespace std; 3 | long long int f(int a,int n,int p){ 4 | if(n==0) 5 | return 1; 6 | long long int mod=f(a,n/2,p); 7 | mod=(mod*mod)%p; 8 | if(n%2==1) 9 | return (mod*a)%p; 10 | return mod; 11 | } 12 | int main(){ 13 | freopen("input.txt","r",stdin); 14 | freopen("output.txt","w",stdout); 15 | int t; 16 | scanf("%d",&t); 17 | for(int c=1;c<=t;c++){ 18 | long long int a,n,p; 19 | scanf("%lld%lld%lld",&a,&n,&p); 20 | for(int i=0;i 2 | using namespace std; 3 | int n,m,a[41],mn[41][41]; 4 | int f(int s,int e){ 5 | if(s==e) return 0; 6 | int result=0; 7 | for(int i=s;i 2 | using namespace std; 3 | int n,m,a[41][41],mn[41][41][41][41],dp[41][41][41][41]; 4 | int f(int l,int r,int p,int q){ 5 | if((l==r && p==q)||(r 2 | using namespace std; 3 | struct comp{ 4 | bool operator() (const int& lhs, const int& rhs) const 5 | {return lhs>rhs;} 6 | }; 7 | int main(){ 8 | int t,n; 9 | cin>>t; 10 | for(int C=1;C<=t;C++){ 11 | scanf("%d",&n); 12 | mapmp; 13 | sets; 14 | long long int res=0; 15 | int temp; 16 | for(int i=0;i::iterator it=s.begin();it!=s.end();++it){ 24 | if(*it==0){ 25 | res+=mp[*it]*(mp[*it]-1)*(mp[*it]-2)/6; 26 | continue; 27 | } 28 | if(*it==1){ 29 | res+=mp[*it]*(mp[*it]-1)*(mp[*it]-2)/6; 30 | if(mp.count(0)) 31 | res+=mp[*it]*(mp[0]-1)*mp[0]/2; 32 | continue; 33 | } 34 | int i=2; 35 | for(;i*i<*it;i++){ 36 | if(*it%i==0&&mp.count(i)&&mp.count(*it/i)){ 37 | // cout<<*it<<" "< 2 | #define M 1000000000 3 | using namespace std; 4 | int dx[]={1,0,-1,0}; 5 | int dy[]={0,1,0,-1}; 6 | int t,n,m,e,sr,sc,tr,tc,grid[100+1][100+1],dist[100+1][100+1]; 7 | struct node{ 8 | int x,y,d; 9 | bool operator<(const node& o) const{ 10 | return d>o.d; 11 | } 12 | }; 13 | int is_valid(int x, int y){ 14 | return x-1&&y>-1&&grid[x][y]!=-100000; 15 | } 16 | int main(){ 17 | cin>>t; 18 | for(int C=1;C<=t;C++){ 19 | cin>>n>>m>>e>>sr>>sc>>tr>>tc; 20 | sr--; 21 | sc--; 22 | tr--; 23 | tc--; 24 | for(int i=0;i>grid[i][j]; 27 | dist[i][j]=M; 28 | } 29 | priority_queue Q; 30 | Q.push({sr,sc,0}); 31 | dist[sr][sc]=0; 32 | while(!Q.empty()){ 33 | node nd=Q.top(); 34 | Q.pop(); 35 | if(dist[nd.x][nd.y]e) 50 | cout<<-1<<"\n"; 51 | else 52 | cout< 2 | using namespace std; 3 | typedef long long int LL; 4 | struct pair{ 5 | LL l,r; 6 | bool operator<(pair o){ 7 | return r>o.r; 8 | } 9 | }p[400001]; 10 | int main(){ 11 | int t,n,q; 12 | LL x,x1,x2,y,y1,y2,z,z1,z2,a1,a2,a3,b1,b2,b3,c1,c2,c3,m1,m2,m3,k[100001]; 13 | cin>>t; 14 | for(int nt=1;nt<=t;nt++){ 15 | cin>>n>>q>>x1>>x2>>a1>>b1>>c1>>m1>>y1>>y2>>a2>>b2>>c2>>m2>>z1>>z2>>a3>>b3>>c3>>m3; 16 | p[0].l=min(x1,y1)+1; 17 | p[0].r=max(x1,y1)+1; 18 | p[1].l=min(x2,y2)+1; 19 | p[1].r=max(x2,y2)+1; 20 | k[0]=z1+1; 21 | k[1]=z2+1; 22 | for(int i=2;i ans; 43 | for(int i=0;i=k[i]){ 53 | lo=mid; 54 | } 55 | else{ 56 | hi=mid-1; 57 | } 58 | 59 | } 60 | ans.push_back(lo); 61 | } 62 | 63 | for(int i=1;i<=q;i++){ 64 | result+=ans[i-1]*i; } 65 | cout< 2 | using namespace std; 3 | typedef long long int lli; 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int C=1;C<=t;C++){ 8 | int n,p; 9 | cin>>n>>p; 10 | string forbd[p]; 11 | lli res=(lli) pow(2,n); 12 | for(int i=0;i>forbd[i]; 14 | } 15 | for(int i=0;i 2 | using namespace std; 3 | typedef long long int lli; 4 | int main(){ 5 | int t; 6 | cin>>t; 7 | for(int C=1;C<=t;C++){ 8 | int n; 9 | string num; 10 | cin>>n>>num; 11 | static int sum[5000000+1]; 12 | sum[0]=num[0]-'0'; 13 | for(int i=1;i 2 | using namespace std; 3 | #define N 200001 4 | #define mod 1000000007 5 | typedef long long int LL; 6 | LL fact[N],p[N]; 7 | LL mult(LL a, LL b){ 8 | return a*b%mod; 9 | } 10 | LL inv(LL a){ 11 | LL e=mod-2; 12 | LL ans=1; 13 | while(e){ 14 | if(e&1) ans=mult(ans,a); 15 | a=mult(a,a); 16 | e>>=1; 17 | } 18 | return ans; 19 | } 20 | LL nck(LL n, LL k){ 21 | return mult(fact[n],inv(mult(fact[k],fact[n-k]))); 22 | } 23 | int solve(){ 24 | int n,m; 25 | scanf("%d%d",&n,&m); 26 | LL ans=0,sn=1; 27 | for(int i=0;i<=m;i++){ 28 | ans=(ans+sn*mult(nck(m,i),mult(fact[2LL*n-i],p[i])))%mod; 29 | sn*=-1LL; 30 | } 31 | return (ans+mod)%mod; 32 | } 33 | int main(){ 34 | int t,nt=0; 35 | fact[0]=p[0]=1LL; 36 | for(LL i=1;i 2 | using namespace std; 3 | int main(){ 4 | int t,n,b,a[100001],tot; 5 | cin>>t; 6 | for(int nt=1;nt<=t;nt++){ 7 | cin>>n>>b; 8 | for(int i=0;i>a[i]; 9 | sort(a,a+n); 10 | int i; 11 | tot=0; 12 | for(i=0;ib) break; 14 | tot+=a[i]; 15 | } 16 | cout<<"Case #"< 2 | using namespace std; 3 | int n,k,p,b[51][31],dp[51][31][1501]; 4 | int solve(int i,int j,int al){ 5 | if(i==n or al==p) return 0; 6 | if(j==k) return solve(i+1,0,al); 7 | if(dp[i][j][al]!=-1) return dp[i][j][al]; 8 | int cur=b[i][j]+solve(i,j+1,al+1); 9 | int nxt=solve(i+1,0,al); 10 | return dp[i][j][al]=max(cur,nxt); 11 | } 12 | int main(){ 13 | int t; 14 | cin>>t; 15 | for(int nt=1;nt<=t;nt++){ 16 | cin>>n>>k>>p; 17 | for(int i=0;i>b[i][j]; 20 | } 21 | } 22 | for(int i=0;i<=n;i++) for(int j=0;j<=k;j++) for(int w=0;w<=p;w++) dp[i][j][w]=-1; 23 | cout<<"Case #"< 2 | using namespace std; 3 | typedef long long int LL; 4 | struct node{ 5 | LL a; 6 | int i; 7 | bool operator<(node o)const { 8 | return a>n; 14 | LL e[n],r[n]; 15 | for(int i=0;i pq; 20 | cur_t=mx_t=sum; 21 | for(int i=0;isum){ 25 | auto c=pq.top(); 26 | pq.pop(); 27 | sum-=e[c.i]; 28 | cur_t-=2*e[c.i]; 29 | rm++; 30 | } 31 | if(mx_t>t; 43 | while(t--){ 44 | cout<<"Case #"<<++nt<<": "; 45 | solve(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Kick Start/Round F 2019/teachme.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define B 1009 4 | typedef long long int LL; 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | for(int nt=1;nt<=t;nt++){ 9 | int n,s,c,q; 10 | mapmp; 11 | cin>>n>>s; 12 | vectorus[n]; 13 | for(int i=0;i>c; 15 | setquality; 16 | while(c--){ 17 | cin>>q; 18 | quality.insert(q); 19 | us[i].push_back(q); 20 | } 21 | LL rep=0; 22 | for(int q:quality){ 23 | rep=n_rep*B+q; 24 | } 25 | mp[rep]++; 26 | } 27 | LL ans=0; 28 | for(vector v:us){ 29 | sort(v.begin(),v.end()); 30 | int p=v.size(); 31 | LL not_blocked=n; 32 | for(int i=1;i<(1<