A
每一次连边,贪心选取 最大的一对 进行连边。最后的 就是过程中 的最小值。
直接暴力枚举是 的。
事实上,注意到度数之和是不超过 的,对于一对 , 只会增加 次。
于是按照度数之和 为下标维护 个队列,每一轮按照下标从大往小依次寻找没有加入的边,加入之后再以 为一个端点枚举另一个端点 更新队列。
如果每轮查找的时候碰见已加入的边就把它删掉,这样均摊下来是 的。
B
每一次操作,耗费的代价都放在更小的那个数上考虑。
构造一个合法序列的过程可以看成是:按照从小到大的顺序枚举每个数,每次选择将这个数放在序列的左边或右边。
不难发现,每次放到左边或右边需要移动的次数是初始序列中左边或右边比这个数大的数的个数,与其它数选择放在左边还是右边无关。
用树状数组计算即可。时间复杂度 。
C
考虑给定 ,怎么求 ?给 建 01 trie 并且在 trie 树上贪心即可。现在 是 连续变化的,再考虑 trie 上贪心的过程。
可以发现, 是一段一段的,并且 一定会出现。
考虑分治求解这个问题,假设我们考虑到 这个区间, 在前 位是完全相同的。
分情况讨论:
-
(序列一一对应比较),那么 在 这一位只有可能全 ,方案数乘 递归求解。
-
, 在 这一位一定有 有 。因为是贪心,所以一定是左边一半 位是 ,右边是 。如果 如果有相同元素在两边那么肯定无解。
复杂度 。
D
subtask 5:
考虑枚举前串 ,对于优美串 ,我们可以 的求出 的所有前缀 满足 作为 的后缀出现,因此所有以 作为前缀的串作为后缀与 配对都能获得了 的优美度。于是对询问再建立 AC 自动机,在 上记录二元组 。问题转化为对于每个后缀 ,求它的前缀上标记的互不相同二元组的数量,这等价于在 AC 自动机的 fail 树上到祖先链上的不同颜色的数。
于是可以通过在 fail 树上的 dfs 一遍求出答案,这部分的复杂度为染色次数。由于一共进行了 次染色,因此复杂度也为 。
subtask 6:
考虑枚举优美串 ,对于 的每一个前缀 ,找出所有满足 是该串最长后缀的前缀集合 (最长后缀是指最长的满足它为 前缀出现的后缀),那么 作为这些后缀出现时的前缀都是 的 border,同时任何一个后缀如果与 中任意一个串组合得到 ,那么一定会与 中所有的串组合得到 ,因此可以将 中的串与 的后缀合并为同一种颜色,暴力下放到 的 border 上进行染色。
求 集合可以通过对优美串建立 AC 自动机,对于每个询问串 在 AC 自动机上走一遍,那么最终节点 fail 树的祖先就是在 中作为后缀出现的询问前缀。对于“最长后缀”这一限制可以暴力在根处挂上全都打标记,最后对于询问串的每个前缀,容易求出它为 border 的前缀的被标记次数即可。接下来就不需要暴力染色,染完色后的部分就与上一个 subtask 相同了。
考虑这个算法的复杂度,求 集合是 的,对于后面的部分,如果是当前枚举优美串的大小 (),那么对它暴力做的复杂度应该是 ,否则该优美串中至多 个串会被标记到,暴力做的复杂度是 的,因此总复杂度为 。
subtask 7: 所有数据
考虑优化上一个做法,先把每个询问串对应的关键点在 AC 自动机上找出,于是之前的染色相当于对某一个优美串前缀,求出有多少个关键点在该前缀的 border 的子树里。那么可以对枚举的这个优美串建立 kmp 自动机,在自动机上 dfs 每一个前缀,那么只需要支持维护一个集合,在集合里增加一个点,动态求有多少个关键点在当前集合中点的子树里。用线段树维护区间加、出现次数 的点数量即可,复杂度为 。
#include<bits/stdc++.h>
using namespace std;
#define re register
#define ll long long
inline int read()
{
int f=1,lzx=0;char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-f;c=getchar();}
while(c<='9'&&c>='0'){lzx=lzx*10+c-'0';c=getchar();}
return lzx*f;
}
const int N=550;
int n,m,id[N][N],mapp[N*N],q[N*N],du[N],tot,h,t,cnt,dot[N*N][2];
inline void link(int x)
{
tot++;
mapp[x]=1;
du[dot[x][0]]++;
du[dot[x][1]]++;
q[++h]=x;
return;
}
inline void above(int x)
{
for(re int i=1;i<=cnt;i++)
{
if(mapp[i])continue;
if(du[dot[i][0]]+du[dot[i][1]]>=x)
link(i);
}
return;
}
inline void spread(int K)
{
while(h>t)
{
t++;int x=dot[q[t]][0],y=dot[q[t]][1];
for(re int i=1;i<=n;i++)
if(i!=x&&!mapp[id[i][x]]&&du[x]+du[i]>=K)
link(id[i][x]);
for(re int i=1;i<=n;i++)
if(i!=y&&!mapp[id[i][y]]&&du[y]+du[i]>=K)
link(id[i][y]);
}
return;
}
int main()
{
n=read(),m=read();
for(re int i=1;i<=n;i++)
for(re int j=i+1;j<=n;j++)
id[i][j]=id[j][i]=++cnt,dot[cnt][0]=i,dot[cnt][1]=j;
while(m--)
{
int x=read(),y=read();
du[x]++,du[y]++,tot++;
q[++h]=id[x][y];mapp[id[x][y]]=1;
}
int K=n*2-2;
while(1)
{
above(K);spread(K);
if(tot==n*(n-1)/2)
{
cout<<K<<endl;
return 0;
}
K--;
}
return 0;
}
#include <bits/stdc++.h>
template <class T>
inline void read(T &res)
{
char ch;
while (ch = getchar(), !isdigit(ch));
res = ch ^ 48;
while (ch = getchar(), isdigit(ch))
res = res * 10 + ch - 48;
}
typedef long long ll;
const int N = 3e5 + 5;
int n, a[N], lef[N], c[N]; ll ans;
template <class T>
inline T Min(T x, T y) {return x < y ? x : y;}
inline void Modify(int x)
{
for (; x <= n; x += x & -x)
++c[x];
}
inline int Query(int x)
{
int res = 0;
for (; x; x ^= x & -x)
res += c[x];
return res;
}
int main()
{
read(n);
for (int i = 1; i <= n; ++i)
read(a[i]), a[i] = n - a[i] + 1;
for (int i = 1; i <= n; ++i)
{
lef[i] = Query(a[i] - 1);
Modify(a[i]);
}
for (int i = 1; i <= n; ++i)
c[i] = 0;
for (int i = n; i >= 1; --i)
{
ans += Min(lef[i], Query(a[i] - 1));
Modify(a[i]);
}
std::cout << ans << std::endl;
return 0;
}
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a, bb = b; i <= bb; ++i)
#define Rof(i, a, b) for (int i = a, bb = b; i >= bb; --i)
#define s64 long long
template <class T>
inline void get(T &res)
{
char ch;
bool bo = false;
while (ch = getchar(), !isdigit(ch))
if (ch == '-') bo = true;
res = ch ^ 48;
while (ch = getchar(), isdigit(ch))
res = (res << 1) + (res << 3) + (ch ^ 48);
if (bo) res = ~ res + 1;
return;
}
template <class T>
inline void _put(T x)
{
if (x > 9) _put(x / 10);
putchar(x % 10 + '0');
return;
}
template <class T>
inline void put(T x, char ch)
{
if (x < 0)
{
putchar('-');
x = ~ x + 1;
}
_put(x);
putchar(ch);
return;
}
#define k2 k << 1
#define k3 k << 1 | 1
const int MaxN = 1 << 16, mod = 1e9 + 7;
int a[MaxN], n, m, ans = 1;
bool apr[MaxN];
inline void solve(int l, int r)
{
if (l == r)
return;
int mid = l + r >> 1;
bool flag = false;
For(i, l, mid)
flag |= a[i] ^ a[i + mid - l + 1];
if (!flag)
{
ans <<= 1;
if (ans >= mod) ans -= mod;
return solve(l, mid);
}
For(i, l, mid)
apr[a[i]] = true;
For(i, mid + 1, r)
if (apr[a[i]])
ans = 0;
For(i, l, mid)
apr[a[i]] = false;
if (ans)
solve(l, mid), solve(mid + 1, r);
return;
}
int main()
{
get(n), get(m);
m = 1 << m;
For(i, 0, m - 1)
{
get(a[i]);
apr[a[i]] = true;
}
For(i, 1, n)
if (!apr[i])
ans = 0;
else
apr[i] = false;
if (ans)
solve(0, m - 1);
put(ans, '\n');
fclose(stdin), fclose(stdout);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
namespace iobuff{
const int LEN=1000000;
char in[LEN+5],out[LEN+5];
char *pin=in,*pout=out,*ed=in,*eout=out+LEN;
inline char gc(void){return pin==ed&&(ed=(pin=in)+fread(in,1,LEN,stdin),ed==in)?EOF:*pin++;}
inline void pc(char c){pout==eout&&(fwrite(out,1,LEN,stdout),pout=out);(*pout++)=c;}
inline void flush(){fwrite(out,1,pout-out,stdout),pout=out;}
template<typename T> inline void read(T &x){
static char c;
c=gc(),x=0;
while(c<'0'||c>'9')c=gc();
while(c>='0'&&c<='9')x=10*x+c-'0',c=gc();
}
template<typename T> inline void putint(T x, char div='\n'){
static char s[15];
static int top;
top=0;
while(x)s[top++]=x%10,x/=10;
!top?pc('0'),0:0;
while(top--)pc(s[top]+'0');
pc(div);
}
inline void scanstring(char *s){
static char c;c=gc();
while(c<'a'||c>'z')c=gc();
int cnt=0;
for(;c>='a'&&c<='z';c=gc(),++cnt)s[cnt]=c;
}
}
using namespace iobuff;
typedef unsigned long long ll;
#define nya(neko...) fprintf(stderr, neko)
__attribute__((destructor))
inline void ptime() {
nya("\nmy Time: %.3lf(s)\n", 1. * clock() / CLOCKS_PER_SEC);
}
const int N=1e6+10;
int n,m,w[N],tot,ct[N],dfn,ru[N],chu[N],num[N],q[N];
char *s[N],*t[N];
char st1[N<<1],*spo=st1,st2[N<<1],*tpo=st2;
ll ans,sum[N],anss[N];
vector<int> suf[N],pre[N];
namespace SGT{//Çø¼ä³Ë-1 Çø¼äÇóºÍ
#define mid ((l+r)>>1)
#define lc (p<<1)
#define rc (p<<1|1)
int mn[N<<2],sum[N<<2];int tag[N<<2];
inline void pushup(int p){
mn[p]=min(mn[lc],mn[rc]);
sum[p]=(mn[lc]==mn[p]?sum[lc]:0)+(mn[rc]==mn[p]?sum[rc]:0);
}
inline void pushdown(int p){
if(!tag[p]) return ;
mn[lc]+=tag[p];tag[lc]+=tag[p];
mn[rc]+=tag[p];tag[rc]+=tag[p];
tag[p]=0;
}
inline void build(int p=1,int l=1,int r=dfn){
tag[p]=0;
if(l==r){mn[p]=0;sum[p]=ct[num[l]];return ;}
build(lc,l,mid);build(rc,mid+1,r);
pushup(p);
}
inline void modify(int p,int ql,int qr,int v,int l=1,int r=dfn){
if(ql<=l&&r<=qr){mn[p]+=v;tag[p]+=v;;return ;}
pushdown(p);
if(ql<=mid) modify(lc,ql,qr,v,l,mid);
if(qr>mid) modify(rc,ql,qr,v,mid+1,r);
pushup(p);
}
}
inline int query(){
if(SGT::mn[1]>0) return n;
else return n-SGT::sum[1];
}
struct AC{
int ch[N][26],fail[N],tot=1;
vector<int> to[N];
inline void insert(char *s,int pos){
int now=1,len=strlen(s);
for(int i=0;i<len;++i){
int c=s[i]-'a';
if(!ch[now][c]) ch[now][c]=++tot;
now=ch[now][c];
pre[pos].push_back(now);
}
}
inline void insert_rev(char *s,int pos){
int now=1,len=strlen(s);
suf[pos].resize(len);
for(int i=len-1;~i;--i){
int c=s[i]-'a';
if(!ch[now][c]) ch[now][c]=++tot;
now=ch[now][c];
suf[pos][i]=now;
}
}
inline void dfs(int u){
ru[u]=++dfn;num[dfn]=u;
for(int v:to[u]) dfs(v);
chu[u]=dfn;
}
inline void build(){
for(int i=0;i<26;++i) ch[0][i]=1;fail[1]=0;
int l=1,r=0;q[++r]=1;
while(l<=r){
int u=q[l++];
for(int i=0;i<26;++i){
if(ch[u][i]) fail[ch[u][i]]=ch[fail[u]][i],q[++r]=ch[u][i];
else ch[u][i]=ch[fail[u]][i];
}
}
for(int i=1;i<=tot;++i) if(fail[i]) to[fail[i]].push_back(i);
}
}T1,T2;
vector<int> e[N];
int fail[N],pos[N],rt;
inline void dfs_fail(int u,int w){
if(u!=rt){
SGT::modify(1,ru[pos[u]],chu[pos[u]],1);
ans+=1ll*query()*w*anss[u];
}
for(int v:e[u]) dfs_fail(v,w);
if(u!=rt) SGT::modify(1,ru[pos[u]],chu[pos[u]],-1);
}
void work(){
for(int i=1;i<=n;++i){
int now=1,len=strlen(s[i]);
for(int j=0;j<len&&now;++j)
now=T2.ch[now][s[i][j]-'a'];
if(now)
for(;now;now=T2.fail[now]) sum[now]++;
}
for(int i=1;i<=m;++i){
int len=strlen(t[i]);
if(len==1) continue;
rt=len;pos[rt]=0;
for(int j=0;j<=len;++j) e[j].clear();
fail[0]=-1;anss[0]=sum[pre[i][0]];e[rt].push_back(0);
pos[0]=suf[i][1];
for(int j=1;j<len-1;++j){
pos[j]=suf[i][j+1];anss[j]=sum[pre[i][j]];
for(int k=fail[j-1];;k=fail[k]){
if(t[i][k+1]==t[i][j]){fail[j]=k+1;break;}
if(k==-1){fail[j]=-1;break;}
}
if(fail[j]==-1) e[rt].push_back(j);
else e[fail[j]].push_back(j);
if(fail[j]!=-1) anss[fail[j]]-=anss[j];
}
dfs_fail(rt,w[i]);
}
}
int main(){
read(n);read(m);
for(int i=1;i<=m;++i){
scanstring(spo);t[i]=spo;read(w[i]);
T1.insert_rev(t[i],i);
T2.insert(t[i],i);spo+=strlen(t[i])+1;
}
for(int i=1;i<=n;++i){
scanstring(tpo);s[i]=tpo;
tpo+=strlen(s[i])+1;
}
T1.build();T2.build();T1.dfs(1);
for(int i=1;i<=n;++i){
int len=strlen(s[i]);
int now=1;
for(int j=0;j<len&&now;++j) now=T1.ch[now][s[i][len-j-1]-'a'];
if(now) ct[now]++;
}
SGT::build();
work();
printf("%lld\n",ans);
return 0;
}