A. Artifact Guarding 选出的守卫需要满足$\max(a+b)\leq \sum a$,从小到大枚举每个值作为$\max(a+b)$,在权值线段树上找到最大的若干个$a$即可. 时间复杂度$O(n\log n)$. #include<cstdio> #include<algorithm> #include<set> #include<map> using namespace std; typedef long long ll; const…
A. Array Factory 将下标按前缀和排序,然后双指针,维护最大的右边界即可. #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int N=200010; int n,i,j,anslen,ansl,ansr,mr,q[N]; ll a[N],lim; inline bool cmp(int x,int y){return a[x]<a[y];…
A. Centroid Tree 枚举至多两个重心作为根,检查对于每个点是否都满足$2size[x]\leq size[father[x]]$即可. #include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map&…
1. Ski race 枚举枚举倍数判断即可.时间复杂度$O(n\log m)$. #include<cstdio> #include<algorithm> using namespace std; int n,m,i,j,ans,flag,q[111111],a[111111]; bool v[11111111]; int main(){ freopen("input.txt","r",stdin); freopen("outpu…
A. Arithmetic Derivative 形如$p^p(p是质数)$的数的比值为$1$,用$k$个这种数相乘得到的数的比值为$k$,爆搜即可. #include<cstdio> #include<algorithm> typedef unsigned long long ll; int K,cnt,all;ll n,i,q[100000],ans[1000000]; ll po(ll a,ll b){ ll t=1; while(b--){ if(t>n/a)retu…
A. Add and Reverse 要么全部都选择$+1$,要么加出高$16$位后翻转位序然后再补充低$16$位. #include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map> #include<…
A. Apple 按题意模拟即可. #include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<ctype.h> #include<math.h> #include<set> #include<map> #include<vector> #include<queue> #include…
A. Count The Ones $ans=b-c+1$. #include <stdio.h> using namespace std ; int a , b , c ; void solve () { printf ( "%d\n" , 1 + b - c ) ; } int main () { while ( ~scanf ( "%d%d%d" , &a , &b , &c ) ) solve () ; return 0…
A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. 时间复杂度$O(n^3)$. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=310,inf=1000000; int n,i,j,m,all,…
题目:Problem A. Arithmetic DerivativeInput file: standard inputOutput file: standard inputTime limit: 1 secondMemory limit: 256 mebibytesLets define an arithmetic derivative:• if p = 1 then p0 = 0;• if p is prime then p0 = 1;• if p is not prime then n0…
题目: Problem D. Great AgainInput file: standard inputOutput file: standard outputTime limit: 2 secondsMemory limit: 512 megabytesThe election in Berland is coming. The party United Berland is going to use its influence to win themagain. The crucial co…
题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondMemory limit: 512 mebibytesThe civil war in Berland continues for five years already. The United Nation decided to end the bloodshed.Berland consists o…
题意:给你一张简单无向图(但可能不连通),再给你一个K,让你求解任意一个问题:K染色或者输出一条K长路径. 直接贪心染色,对一个点染上其相邻的点的颜色集合之中,未出现过的最小的颜色. 如果染成就染成了.如果到某个点,发现染不成,则倒着按照颜色从大到小回去,则一定恰好可以找出一条K长度的路径. #include<cstdio> #include<cstring> using namespace std; int first[1005],next[20005],v[20005],e,c…
题意:有n个土豆,每个有体积V(i),你可以将每个土豆等分为不超过K份,问你最大块和最小块比值最小为多少. 直接枚举切法,只有n*K种,然后保证其为最大块,去算其他块的切法,即让其他块切得尽可能大即可.O(n*n*K). #include<cstdio> #include<cstring> using namespace std; int n,K,a[105],ans1=150,ans2=1,x[105],ansx[105]; int main(){ // freopen(&quo…
给你n个字符串,问你最小的长度的前缀,使得每个字符串任意循环滑动之后,这些前缀都两两不同. 二分答案mid之后,将每个字符串长度为mid的循环子串都哈希出来,相当于对每个字符串,找一个与其他字符串所选定的子串不同的子串,是个二分图最大匹配的模型,可以匈牙利或者Dinic跑最大流看是否满流. 一个小优化是对于某个字符串,如果其所有不同的子串数量超过n,那么一定满足,可以直接删去. 卡常数,不能用set,map啥的,采取了用数组记录哈希值,排序后二分的手段进行去重和离散化. #include<cst…
思路: 直接二分长度不可行,因为有负数. 考虑枚举坐便删l个数,那如果可以在短时间内求出符合条件的右边最小删的数的个数,这题便可做了. 即:当左边删l个数时,要使sum[n]-sum[l]-fsum[n+1-x] <= s成立,求出最小的x.(sum为前缀和,fsum为后缀和) 思考后可以发现可行的fsum[x]必然要是正数,可能的答案x必然是单调递增的(x从0开始). 所以维护一个大于0的递增的单调栈即可,在里面二分找答案. #include <bits/stdc++.h> using…
题目: Problem F. Matrix GameInput file: standard inputOutput file: standard inputTime limit: 1 secondMemory limit: 256 mebibytesAlice and Bob are playing the next game. Both have same matrix N × M filled with digits from 0 to 9.Alice cuts the matrix ve…
题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemory limit: 256 mebibytesN programmers from M teams are waiting at the terminal of airport. There are two shuttles at the exitof terminal, each shuttle…
题目:Problem L. Canonical duelInput file: standard inputOutput file: standard outputTime limit: 2 secondsMemory limit: 256 megabytesIn the game «Canonical duel» board N × M is used. Some of the cells of the board contain turrets. Aturret is the unit wi…
题目:Problem D. Clones and TreasuresInput file: standard inputOutput file: standard outputTime limit: 1 secondMemory limit: 256 mebibytesThe magical treasury consists of n sequential rooms. Due to construction of treasury its impossible togo from room…
题意: 给你一个n,问你R(n)对应的字符串长度最小的是啥. dp打个表出来,f(i)表示i值对应的字符串的最小长度,发现f(1)=1,f(2)=2,其他的情况下,若是偶数,则恰好在其外面加一对中括号,然后中间填i/2最优,若是奇数,恰好在i-1前面加个1最优. 于是递归输出答案即可. #include<cstdio> #include<iostream> #include<string> using namespace std; string work(int x){…
给你一个网格(n<=2000,m<=2000),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸弹数. 转化成: 将行列当成点,炸弹当成边,然后你可以给这个二分图加1条边,问你最大的连通块的边的数量. 可以通过枚举所有可以建的边,通过并查集来尝试更新答案.由于一条边必然会让总度数+2,所以一个连通块的边数是所有点的度数之和/2. 并查集不必要动态维护集合的大小,一开始就建好并查集,提前统计好即可. 最后…
有两辆车,容量都为K,有n(10w)个人被划分成m(2k)组,依次上车,每个人上车花一秒.每一组的人都要上同一辆车,一辆车的等待时间是其停留时间*其载的人数,问最小的两辆车的总等待时间. 是f(i,j)表示前i组,j个人是否可行.w(i)表示第i组的人数. if f(i,j)==1 then f(i+1,j+w(i+1))=1. 这是个bitset可以做的事情,每次左移以后或上f(i-1)的bitset即可.其实可以滚动数组. 然后每更新一次bitset,求一下其最左侧的1的位置,就是对于第一辆…
给你一个n*m的字符矩阵,将横向(或纵向)全部裂开,然后以任意顺序首尾相接,然后再从中间任意位置切开,问你能构成的字典序最大的字符串. 以横向切开为例,纵向类似. 将所有横排从大到小排序,枚举最后切开的位置在哪一横排,将这一排提到排序后的字符串数组最前面,求个“最大表示法”,如果最大表示法的位置恰好在第一排的位置,那么可以用来更新答案. 如果不在第一排的位置,那么其所构成的仍然是合法的串,而且一定不会影响答案. 这是一个最小表示法的板子. #include<cstdio> #include&l…
给你n,K,问你要选出最少几个长度为2的K进制数,才能让所有的n位K进制数删除n-2个元素后,所剩余的长度为2的子序列至少有一个是你所选定的. 如果n>K,那么根据抽屉原理,对于所有n位K进制数,必然会至少有1个数字出现2次或以上,所以00,11,...,K-1 K-1这样的数对是必选的. 对于其他的情况下,我们需要让他构造不出来n位不含重复数字的K进制数. 于是可以把K个数尽可能平均地分成n-1组,每一组内部让他们选出任意两个数都不合法,于是只能组间互相拼,这样他只能构造出最多n-1位的K进制…
给你一行房间,有的是隐身药水,有的是守卫,有的是金币. 你可以任选起点,向右走,每经过一个药水或金币就拿走,每经过一个守卫必须消耗1个药水,问你最多得几个金币. 药水看成左括号,守卫看成右括号, 就从每个位置贪心地向右走,如果在 r 遇到不匹配,则把下一次的左端点置成r+1,接着走. O(n)即可. 因为如果把左端点放在上次的l和r之间,要么会发生不匹配,要么答案无法比上次走的更优. 队友代码: #include <iostream> #include <cstdio> #incl…
假设一个数有n个质因子a1,a2,..,an,那么n'=Σ(a1*a2*...*an)/ai. 打个表出来,发现一个数x,如果x'=Kx,那么x一定由K个“基础因子”组成. 这些基础因子是2^2,3^3,5^5,7^7,11^11,13^13.只有6个,K不超过30,于是可以dfs. 要注意搜索顺序(每次枚举的时候,都从大于等于前项的开始搜)和可行性剪枝(如果超过r则剪枝,虽说有可能爆long long,但其实整除就可以判,而且没有精度误差). #include<cstdio> //#incl…
1000w的数组,一开始都是2^31-1,然后经过5*10^7次随机位置的随机修改,问你每次的全局最小值. 有效的随机修改的期望次数很少,只有当修改到的位置恰好是当前最小值的位置时才需要扫一下更新最小值. 分个块或者直接暴力都可以. #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> using namespace std; int sz,l[10005],r[100…
A. Survival Route 留坑. B. Dispersed parentheses $f[i][j][k]$表示长度为$i$,未匹配的左括号数为$j$,最多的未匹配左括号数为$k$的方案数.时间复杂度$O(n^3)$. #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int P=1000000009; const int N=310; int n,m…
A. Associated Vertices 首先求出SCC然后缩点,第一次求出每个点能到的点集,第二次收集这些点集即可,用bitset加速,时间复杂度$O(\frac{nm}{64})$. #include<cstdio> #include<bitset> using namespace std; const int N=10010; int n,m,x,y,i,j,g[N],G[N],v[N*3],V[N*3],nxt[N*3],NXT[N*3],ed; int vis[N],…