首页 > 代码库 > CF round416 div2 补题

CF round416 div2 补题

A. Vladik and Courtesy

水题略过

#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long int LL;
int main()
{
 LL a,b;
 scanf("%I64d%I64d",&a,&b);
 LL num=1;
 while(true)
 	{
 	 if(a<num){printf("Vladik\n");return 0;}
 	 a-=num;
 	 //b+=num;
 	 num++;
 	 if(b<num){printf("Valera\n");return 0;}
 	 b-=num;
 	 //a+=num;
 	 num++;
	}
 return 0;
} 

  B. Vladik and Complicated Book

给定一个序列, 询问子区间【l,r】的第k小数是不是原位置的数。

显然可以用主席树维护,不过这道题数据放水了,用暴力一点的方法应该也能过。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100000 + 5;

int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 20], sum[N * 20],numm[N];

int n, k, tot, sz, ql, qr, x, q, T;

void Build(int& o, int l, int r){
    if(l>r)return ;
	o = ++ tot;
    sum[o] = 0;
    if(l == r) return;
    int m = (l + r) >> 1;
    Build(ls[o], l, m);
    Build(rs[o], m + 1, r);
}

void update(int& o, int l, int r, int last, int p){
    o = ++ tot;
    ls[o] = ls[last];
    rs[o] = rs[last];
    sum[o] = sum[last] + 1;
    if(l == r) return;
    int m = (l + r) >> 1;
    if(p <= b[m])  update(ls[o], l, m, ls[last], p);
    else update(rs[o], m + 1, r, rs[last], p);
}

int query(int ss, int tt, int l, int r, int k){
    if(l == r) return l;
    int m = (l + r) >> 1;
    int cnt = sum[ls[tt]] - sum[ls[ss]];
    if(k <= cnt) return query(ls[ss], ls[tt], l, m, k);
    else return query(rs[ss], rs[tt], m + 1, r, k - cnt);
}

void work(){
    scanf("%d%d%d", &ql, &qr, &x);
    int xx=x;
    x=(xx-ql+1);
    int ans = query(rt[ql - 1], rt[qr], 1, sz, x);
    if(b[ans]==numm[xx])printf("Yes\n");
    	else printf("No\n");
}

int main(){//freopen("t.txt","r",stdin);
   T=1; 
    while(T--){
        scanf("%d%d", &n, &q);
        for(int i = 1; i <= n; i ++) scanf("%d", a + i), numm[i]=b[i] = a[i];
        sort(b + 1, b + n + 1);
        sz = unique(b + 1, b + n + 1) - (b + 1);
        tot = 0;
        Build(rt[0],1, sz);
        //for(int i = 0; i <= 4 * n; i ++)printf("%d,rt =  %d,ls =  %d, rs = %d, sum = %d\n", i, rt[i], ls[i], rs[i], sum[i]);
        //for(int i = 1; i <= n; i ++)a[i] = lower_bound(b + 1, b + sz + 1, a[i]) - b;
        for(int i = 1; i <= n; i ++)update(rt[i], 1, sz, rt[i - 1], a[i]);
       // for(int i = 0; i <= 5 * n; i ++)printf("%d,rt =  %d,ls =  %d, rs = %d, sum = %d\n", i, rt[i], ls[i], rs[i], sum[i]);
        while(q --)work();
    }
    return 0;
}

  C. Vladik and Memorable Trip

O(n^2)的线性dp 不要想太多。。(我都想到DAG去了。。。)

代码比较简单,可以直接看懂。

#include <bits/stdc++.h>
using namespace std;

vector <int> ps [5010], cs[5010];
int s[5010] , e[5010];
bool vis[5010];
long long dp [5010];
int main()
{
	int n;
	scanf("%d",&n);
	vector <int> a (n);
	for (int i = 0; i < n; i++) {
		scanf("%d",&a[i]);
		if (!vis[a[i]]) s[a[i]] = i;
		vis[a[i]] = 1;
		e[a[i]] = i;
	}
	for (int i = 0; i < n; i++) vis[a[i]] = 0;
	for (int i = 0; i < n; i++) {
		int mx = i, rx = 0, lj = i;
		for (int j = i; j < n; j++) {
			if (s[a[j]] < i) break;
			lj = j;
			mx = max(mx,e[a[j]]);
			if (!vis[a[j]]) rx ^= a[j];
			vis[a[j]] = 1;
			
			if (mx <= j) {
				ps[i].push_back(j);
				cs[i].push_back(rx);
			}
		}
		for (int k = lj; k >= i; k--) vis[a[k]] = 0;
	}
	for (int i = n-1; i >= 0; i--) {
		dp[i] = dp[i+1];
		for (int j = 0; j < ps[i].size(); j++) {
			dp[i] = max(dp[i],dp[ps[i][j]+1]+cs[i][j]);
		}
	}
	printf("%I64d\n",dp[0]);
}

  D. Vladik and Favorite Game

 

 

E. Vladik and Entertaining Flags

CF round416 div2 补题