【AtCoder】CODE FESTIVAL 2016 qual B

CODE FESTIVAL 2016 qual B

A - Signboard

……

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	   c = getchar();
    }
    while(c >= '0' && c <= '9') {
    	res = res * 10 +c - '0';
    	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    	out(x / 10);
    }
    putchar('0' + x % 10);
}
string s = "CODEFESTIVAL2016",t;
void Solve() {
    cin >> t;
    int ans = 0;
    for(int i = 0 ; i < s.length() ; ++i) {
        if(s[i] != t[i]) ++ans;
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

B - Qualification simulator

……

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	   c = getchar();
    }
    while(c >= '0' && c <= '9') {
    	res = res * 10 +c - '0';
    	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    	out(x / 10);
    }
    putchar('0' + x % 10);
}
int N,A,B;
char s[MAXN];
void Solve() {
    read(N);read(A);read(B);
    scanf("%s",s + 1);
    int r = 1,all = 0;
    for(int i = 1 ; i <= N ; ++i) {
        if(s[i] == 'a') {
            if(all < A + B) {puts("Yes");++all;}
            else puts("No");
        }
        else if(s[i] == 'b') {
            if(all < A + B && r <= B) {puts("Yes");++all;++r;}
            else puts("No");
        }
        else {
            puts("No");
        }
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

C - Gr-idian MST

找到p和q中还没用的最小的一个

如果用的是p,q已经用了k个,这个p值能用的就是\(H - k + 1\)这么多

用的是q同理

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	   c = getchar();
    }
    while(c >= '0' && c <= '9') {
    	res = res * 10 +c - '0';
    	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    	out(x / 10);
    }
    putchar('0' + x % 10);
}
int W,H;
int64 p[MAXN],q[MAXN],ans;
int id[2][MAXN],c[2];
void Solve() {
    read(W);read(H);
    for(int i = 0 ; i < W ; ++i) {
        id[0][i] = i;
        read(p[i]);
    }
    for(int i = 0 ; i < H ; ++i) {
        id[1][i] = i;
        read(q[i]);
    }
    sort(id[0],id[0] + W,[](int a,int b){return p[a] < p[b];});
    sort(id[1],id[1] + H,[](int a,int b){return q[a] < q[b];});
    int p1 = 0,p2 = 0;
    for(int i = 1 ; i <= W + H ; ++i) {
        if(p2 >= H || (p1 < W && p[id[0][p1]] < q[id[1][p2]])) {
            ans += 1LL * (H - c[1] + 1) * p[id[0][p1]];
            ++p1;++c[0];
        }
        else {
            ans += 1LL * (W - c[0] + 1) * q[id[1][p2]];
            ++p2;++c[1];
        }
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

D - Greedy customers

考虑到我们希望每个人用尽量小的东西填满它,且这个东西不能和之前出现过的大小相等

我们先用1把第一个人变成1,然后后面如果出现一个2,则我们扔的东西都要大于2

如果出现大于2的数,我们总有办法使这个数买了最多\(\lfloor \frac{x}{3} \rfloor\)的状态下

如果出现了3之后同理,如此贪心下去即可

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	   c = getchar();
    }
    while(c >= '0' && c <= '9') {
    	res = res * 10 +c - '0';
    	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    	out(x / 10);
    }
    putchar('0' + x % 10);
}
int N,a[MAXN];
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
        read(a[i]);
    }
    int pre = 1;
    int64 ans = a[1] - 1;a[1] = 1;
    for(int i = 2 ; i <= N ; ++i) {
        if(a[i] == pre + 1) pre++;
        else {
            ans += (a[i] - 1) / (pre + 1);
        }
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

E - Lexicographical disorder

这个我分析了一下,压缩后的树高应该是根号级别的,于是愉快的开始卡常,最后就剩三个点死活也过不去了

翻了一下题解,只要对于trie上每个点统计在某个字母小于另一个字母的情况下,某个点的儿子中的一个儿子是否小于另一个儿子

因为这种关系只有\(26 * 26\)所以存的下而且很快就过了。。

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	   c = getchar();
    }
    while(c >= '0' && c <= '9') {
    	res = res * 10 +c - '0';
    	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    	out(x / 10);
    }
    putchar('0' + x % 10);
}
struct node {
    int to;string s;
};
vector<node> to[MAXN * 4];
int N,Q,Ncnt;
int ed[MAXN * 4],ans[MAXN],pri[30],siz[MAXN * 4],pla[MAXN];
int dp[MAXN * 4][26][26];
string str[MAXN],order;
void insert(int u,int id,int pos) {
    if(pos == str[id].length()) {ed[u] = id;pla[id] = u;return;}
    for(auto &t : to[u]) {
        if(t.s[0] == str[id][pos]) {
            int l = -1;
            while(l + 1 < str[id].length() - pos && l + 1 < t.s.length()) {
                if(str[id][pos + l + 1] == t.s[l + 1]) ++l;
                else break;
            }
            if(l + 1 == t.s.length()) {
                insert(t.to,id,pos + t.s.length());
                return;
            }
            int nw = ++Ncnt;
            to[nw].pb((node){t.to,t.s.substr(l + 1)});
            t.to = nw;t.s = t.s.substr(0,l + 1);
            if(l + 1 == str[id].length() - pos) {
                ed[nw] = id;pla[id] = nw;
            }
            else {
                to[nw].pb((node){++Ncnt,str[id].substr(pos + l + 1)});
                ed[Ncnt] = id;
                pla[id] = Ncnt;
            }
            return;
        }
    }
    to[u].pb((node){++Ncnt,str[id].substr(pos)});
    ed[Ncnt] = id;pla[id] = Ncnt;return;
}
void pre_Process(int u,int all) {
    if(ed[u]) {ans[ed[u]] = all;siz[u]++;++all;}
    for(auto t : to[u]) {
        pre_Process(t.to,all);
        siz[u] += siz[t.to];
    }

}
void Calc(int u) {
    for(auto t : to[u]) {
        memcpy(dp[t.to],dp[u],sizeof(dp[t.to]));
        for(auto k : to[u]) {
            if(t.to == k.to) continue;
            dp[t.to][k.s[0] - 'a'][t.s[0] - 'a'] += siz[k.to];
        }
    }
    for(auto t : to[u]) {
        Calc(t.to);
    }
}
void Solve() {
    read(N);
    Ncnt = 1;
    for(int i = 1 ; i <= N ; ++i) {
        cin >> str[i];
        insert(1,i,0);
    }
    pre_Process(1,0);
    Calc(1);
    read(Q);
    int k;
    for(int i = 1 ; i <= Q ; ++i) {
        read(k);cin >> order;
        int res = 0;
        for(int j = 0 ; j < 26 ; ++j) {
            for(int t = j + 1 ; t < 26 ; ++t) {
                res += dp[pla[k]][order[j] - 'a'][order[t] - 'a'];
            }
        }
        out(res + ans[k] + 1);enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}
posted @ 2019-05-18 15:23  sigongzi  阅读(180)  评论(0编辑  收藏  举报