【CF套题】 Avito Cool Challenge 2018

【题目】
原题地址

A.Definite Game

n = 2 n=2 n=2时答案为 2 2 2,否则为 1 1 1

#include<bits/stdc++.h>
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;

int read()
{
	int ret=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=0;c=getchar();}
	while(isdigit(c)) ret=ret*10+(c^48),c=getchar();
	return f?ret:-ret;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("A.in","r",stdin);
	freopen("A.out","w",stdout);
#endif
	int n=read();
	if(n==2) puts("2");
	else puts("1");

	return 0;
}

B.Farewell Party

c i = n − a i c_i=n-a_i ci=nai,则 c i c_i ci相同的每 c i c_i ci个人会是一个颜色,用队列存储即可。

#include<bits/stdc++.h>
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
const int N=1e5+10;
int n,tot;
int col[N],a[N];
queue<int>st[N];
ll sum;

int read()
{
	int ret=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=0;c=getchar();}
	while(isdigit(c)) ret=ret*10+(c^48),c=getchar();
	return f?ret:-ret;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("B.in","r",stdin);
	freopen("B.out","w",stdout);
#endif
	n=read();
	for(int i=1;i<=n;++i) a[i]=read(),st[a[i]].push(i);
	for(int i=1;i<=n;++i)
	{
		if(col[i]) continue; 
		int num=n-a[i];++tot;
		while(num && !st[a[i]].empty()) col[st[a[i]].front()]=tot,st[a[i]].pop(),--num;
		if(num) {puts("Impossible");exit(0);}
	}
	puts("Possible");
	for(int i=1;i<=n;++i) printf("%d ",col[i]);

	return 0;
}

C.Colorful Bricks

f i , j f_{i,j} fi,j表示前 i i i块有 j j j块和左边颜色不一样,转移显然。

#include<bits/stdc++.h>
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
const int mod=998244353,N=2005;
int n,m,k,f[N][N];

int read()
{
	int ret=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=0;c=getchar();}
	while(isdigit(c)) ret=ret*10+(c^48),c=getchar();
	return f?ret:-ret;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("C.in","r",stdin);
	freopen("C.out","w",stdout);
#endif
	n=read();m=read();k=read();
	f[1][0]=m;
	for(int i=2;i<=n;++i) for(int j=0;j<min(i,k+1);++j)
	{
		if(j) (f[i][j]+=(ll)f[i-1][j-1]*(m-1)%mod)%=mod;
		(f[i][j]+=f[i-1][j])%=mod;
	}
	printf("%d\n",f[n][k]);

	return 0;
}

D.Maximum Distance

显然最后的答案都是一样的。我们跑最小生成树,当新加入一条边所连通的其中一个连通块内没有关键点,则这条边没有贡献,输出有贡献的最大边即可。

#include<bits/stdc++.h>
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
const int N=2e5+10;
int n,m,k,totE,ans;
int fa[N],sp[N];

int read()
{
	int ret=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=0;c=getchar();}
	while(isdigit(c)) ret=ret*10+(c^48),c=getchar();
	return f?ret:-ret;
}

struct data
{
	int u,v,w;
	data(int u=0,int v=0,int w=0):u(u),v(v),w(w){};
}E[N];
bool cmp(const data&A,const data&B){return A.w<B.w;}
int findf(int x){return fa[x]==x?x:fa[x]=findf(fa[x]);}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("D.in","r",stdin);
	freopen("D.out","w",stdout);
#endif
	n=read();m=read();k=read();
	for(int i=1;i<=k;++i) sp[read()]=1;
	for(int i=1;i<=m;++i)
	{
		int u=read(),v=read(),w=read();
		if(u>v) swap(u,v);
		E[i]=data(u,v,w);
	}
	sort(E+1,E+m+1,cmp);
	for(int i=1;i<=n;++i) fa[i]=i;
	for(int i=1;i<=m;++i)
	{
		int fu=findf(E[i].u),fv=findf(E[i].v);
		if(fu==fv) continue;
		fa[fu]=fv;if(!sp[fv] || !sp[fu]){sp[fv]+=sp[fu];continue;}
		sp[fv]+=sp[fu];ans=max(ans,E[i].w);
	}
	for(int i=1;i<=k;++i) printf("%d ",ans);

	return 0;
}

E. Missing Numbers

由于输入的数字最大只有 2 × 1 0 5 2\times 10^5 2×105,由 n 2 = ( n − 1 ) 2 + 2 n + 1 n^2=(n-1)^2+2n+1 n2=(n1)2+2n+1可观察出若前缀和 s = x 2 s=x^2 s=x2,则 x x x只有不到 1 0 6 10^6 106级别,枚举前缀 x x x贪心即可。

#include<bits/stdc++.h>
#define fi first
#define se second
#define mkp make_pair
#define pb push_back
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
const ll INF=(ll)2e18,MX=1e7,N=2e5+10;
int n;
ll a[N],b[N];

ll read()
{
	ll ret=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=0;c=getchar();}
	while(isdigit(c)) ret=ret*10+(c^48),c=getchar();
	return f?ret:-ret;
}

bool check(ll x){if(x<=0)return 0;ll t=(ll)sqrt(x);t*=t;return t==x;}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	n=read();
	for(int i=1;i<=n/2;++i) a[i]=read();
	ll now=1,sum=0,s=0;
	for(int i=1;i<=n/2;++i)
	{
		while(now<MX && s<=sum) s+=now,now+=2;
		if(now>=MX){puts("No");return 0;}
		while(now<MX && (!check(s-a[i]) || s<=sum+a[i])) s+=now,now+=2;
		if(now>=MX){puts("No");return 0;}
		b[i]=s-sum-a[i];sum=s;
	}
	puts("Yes");
	for(int i=1;i<=n/2;++i) printf("%lld %lld ",b[i],a[i]);

	return 0;
}

F.Tricky Interactor

单独写了

G.Mergesort Strikes Back

同样单独写了

H.Palindromic Magic

根本不会,左转官方题解

WXH的代码

#include <bits/stdc++.h>

using namespace std;

const int N = 234567;
const int LOG = 18;
const int ALPHA = 26;
const int base = 2333;
const int md0 = 1e9 + 7;
const int md1 = 1e9 + 9;

struct hash_t {
  int hash0, hash1;

  hash_t(int hash0 = 0, int hash1 = 0):hash0(hash0), hash1(hash1) {
  }

  hash_t operator + (const int &x) const {
    return hash_t((hash0 + x) % md0, (hash1 + x) % md1);
  };

  hash_t operator * (const int &x) const {
    return hash_t((long long)hash0 * x % md0, (long long)hash1 * x % md1);
  }

  hash_t operator + (const hash_t &x) const {
    return hash_t((hash0 + x.hash0) % md0, (hash1 + x.hash1) % md1);
  };

  hash_t operator - (const hash_t &x) const {
    return hash_t((hash0 + md0 - x.hash0) % md0, (hash1 + md1 - x.hash1) % md1);
  };

  hash_t operator * (const hash_t &x) const {
    return hash_t((long long)hash0 * x.hash0 % md0, (long long)hash1 * x.hash1 % md1);
  }

  long long get() {
    return (long long)hash0 * md1 + hash1;
  }
} ha[N], hb[N], power[N];

struct palindrome_tree_t {
  int n, total, p[N], pos[N], value[N], parent[N], go[N][ALPHA], ancestor[LOG][N];
  char s[N];

  palindrome_tree_t() {
    parent[0] = 1;
    value[1] = -1;
    total = 1;
    p[0] = 1;
  }

  int extend(int p, int w, int n) {
    while (s[n] != s[n - value[p] - 1]) {
      p = parent[p];
    }
    if (!go[p][w]) {
      int q = ++total, k = parent[p];
      while (s[n] != s[n - value[k] - 1]) {
        k = parent[k];
      }
      value[q] = value[p] + 2;
      parent[q] = go[k][w];
      go[p][w] = q;
      pos[q] = n;
    }
    return go[p][w];
  }

  void init() {
    for (int i = 1; i <= n; ++i) {
      p[i] = extend(p[i - 1], s[i] - 'a', i);
    }
    for (int i = 0; i <= total; ++i) {
      ancestor[0][i] = parent[i];
    }
    for (int i = 1; i < LOG; ++i) {
      for (int j = 0; j <= total; ++j) {
        ancestor[i][j] = ancestor[i - 1][ancestor[i - 1][j]];
      }
    }
  }

  int query(int r, int length) {
    r = p[r];
    if (value[r] <= length) {
      return value[r];
    }
    for (int i = LOG - 1; ~i; --i) {
      if (value[ancestor[i][r]] > length) {
        r = ancestor[i][r];
      }
    }
    return value[parent[r]];
  }

  bool check(int r, int length) {
    r = p[r];
    for (int i = LOG - 1; ~i; --i) {
      if (value[ancestor[i][r]] >= length) {
        r = ancestor[i][r];
      }
    }
    return value[r] == length;
  }
} A, B, RA, RB;

map<long long, int> fa, fb, ga, gb;
long long answer;
char a[N], b[N];
int n, m;

hash_t get_hash(hash_t *h, int l, int r) {
  return h[r] - h[l - 1] * power[r - l + 1];
}

int main() {
#ifdef wxh010910
  freopen("input.txt", "r", stdin);
#endif
  scanf("%s %s", a + 1, b + 1);
  n = strlen(a + 1);
  m = strlen(b + 1);
  A.n = RA.n = n;
  B.n = RB.n = m;
  for (int i = 1; i <= n; ++i) {
    A.s[i] = RA.s[n - i + 1] = a[i];
    ha[i] = ha[i - 1] * base + a[i];
  }
  for (int i = 1; i <= m; ++i) {
    B.s[i] = RB.s[m - i + 1] = b[i];
    hb[i] = hb[i - 1] * base + b[i];
  }
  power[0] = hash_t(1, 1);
  for (int i = 1; i <= max(n, m); ++i) {
    power[i] = power[i - 1] * base;
  }
  A.init();
  B.init();
  RA.init();
  RB.init();
  answer = (long long)(A.total - 1) * (B.total - 1);
  for (int i = 2; i <= A.total; ++i) {
    ++fa[get_hash(ha, A.pos[i] - A.value[i] + 1, A.pos[i]).get()];
    int p = A.parent[i];
    if (p < 2) {
      continue;
    }
    int l = A.pos[i] - (A.value[i] - A.value[p]) + 1, r = A.pos[i];
    if (A.value[i] <= A.value[p] << 1) {
      ++ga[get_hash(ha, l, r).get()];
    }
  }
  for (int i = 2; i <= B.total; ++i) {
    ++fb[get_hash(hb, B.pos[i] - B.value[i] + 1, B.pos[i]).get()];
    int p = B.parent[i];
    if (p < 2) {
      continue;
    }
    int l = B.pos[i] - B.value[i] + 1, r = B.pos[i] - B.value[p];
    if (B.value[i] <= B.value[p] << 1) {
      ++gb[get_hash(hb, l, r).get()];
    }
  }
  for (int i = 2; i <= A.total; ++i) {
    int p = A.parent[i];
    if (p < 2) {
      continue;
    }
    int l = A.pos[i] - (A.value[i] - A.value[p]) + 1, r = A.pos[i];
    long long value = get_hash(ha, l, r).get();
    if (gb.count(value)) {
      answer -= gb[value];
    }
    int longest_palindrome_suffix = A.query(r, r - l + 1);
    if (longest_palindrome_suffix == r - l + 1) {
      continue;
    }
    if (RA.check(n - l + 1, r - l + 1 - longest_palindrome_suffix)) {
      int length = r - l + 1 - longest_palindrome_suffix;
      if (fb.count(get_hash(ha, l, l + length - 1).get()) && fb.count((get_hash(ha, l, r) * power[length] + get_hash(ha, l, l + length - 1)).get())) {
        --answer;
      }
      continue;
    }
    int longest_palindrome_prefix = RA.query(n - l + 1, r - l + 1);
    if (A.check(r, r - l + 1 - longest_palindrome_prefix)) {
      int length = longest_palindrome_prefix;
      if (fb.count(get_hash(ha, l, l + length - 1).get()) && fb.count((get_hash(ha, l, r) * power[length] + get_hash(ha, l, l + length - 1)).get())) {
        --answer;
      }
      continue;
    }
  }
  for (int i = 2; i <= B.total; ++i) {
    int p = B.parent[i];
    if (p < 2) {
      continue;
    }
    int l = B.pos[i] - B.value[i] + 1, r = B.pos[i] - B.value[p];
    long long value = get_hash(hb, l, r).get();
    if (ga.count(value)) {
      answer -= ga[value];
    }
    int longest_palindrome_suffix = B.query(r, r - l + 1);
    if (longest_palindrome_suffix == r - l + 1) {
      continue;
    }
    if (RB.check(m - l + 1, r - l + 1 - longest_palindrome_suffix)) {
      int length = longest_palindrome_suffix;
      if (fa.count(get_hash(hb, r - length + 1, r).get()) && fa.count((get_hash(hb, r - length + 1, r) * power[r - l + 1] + get_hash(hb, l, r)).get())) {
        --answer;
      }
      continue;
    }
    int longest_palindrome_prefix = RB.query(m - l + 1, r - l + 1);
    if (B.check(r, r - l + 1 - longest_palindrome_prefix)) {
      int length = r - l + 1 - longest_palindrome_prefix;
      if (fa.count(get_hash(hb, r - length + 1, r).get()) && fa.count((get_hash(hb, r - length + 1, r) * power[r - l + 1] + get_hash(hb, l, r)).get())) {
        --answer;
      }
      continue;
    }
  }
  for (int i = 2; i <= A.total; ++i) {
    int p = A.parent[i];
    if (p < 2) {
      continue;
    }
    int l = A.pos[i] - (A.value[i] - A.value[p]) + 1, r = A.pos[i];
    if (A.value[i] > A.value[p] << 1) {
      ++ga[get_hash(ha, l, r).get()];
    }
  }
  for (int i = 2; i <= B.total; ++i) {
    int p = B.parent[i];
    if (p < 2) {
      continue;
    }
    int l = B.pos[i] - B.value[i] + 1, r = B.pos[i] - B.value[p];
    if (B.value[i] > B.value[p] << 1) {
      ++gb[get_hash(hb, l, r).get()];
    }
  }
  for (auto p : ga) {
    answer += (long long)p.second * gb[p.first];
  }
  printf("%lld\n", answer);
  return 0;
}

【总结】
这套题目的 F , G F,G F,G十分考思维,超棒的呢!
虽然我D,E,F都看错题然后光荣掉分了(还好没下紫,感谢那个hack了我D的童鞋)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值