2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017) 部分题/平衡树/最小环/思路bfs

交题地址

难度按照顺序递增

J - Judging Moose
队友敲的

#include 
#include 
using namespace std;

int main()
{
   int l, r;
   while(~scanf("%d %d", &l, &r)){
    if(l == r && l != 0){
        printf("Even ");
    }
    else if(l != r && (l != 0 || r != 0)){
        printf("Odd ");
    }
    else{
        puts("Not a moose");
        continue;
    }
    int mid = (l + r);
    if(l == mid / 2 && r == mid / 2){
        printf("%d\n", mid);
    }
    else printf("%d\n",max(l, r) * 2);
   }
}

B - Best Relay Team
队友敲的

#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 500 + 10;
const double inf = 1 << 30;
int n;
struct runer{
    string name;
    double f;
    double s;
}runers[maxn];
int cmp(runer a, runer b){
    if(a.s - b.s < 0) return 1;
    else return 0;
}
int main()
{
    //freopen("input.txt", "r", stdin);
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; i++){
            cin >> runers[i].name;
            scanf("%lf%lf", &runers[i].f, &runers[i].s);
        }
        sort(runers + 0, runers + n, cmp);
//         for(int i = 0; i < n; i++){
//           cout << runers[i].name <<" "<
            scanf("%lf%lf", &runers[i].f, &runers[i].s);
//        }
        double minimum = inf;
        int res[5], cnt;
        for(int i = 0; i < n; i++){
            double temp = 0;
            int tmplist[5];
            cnt = 0;
            temp += runers[i].f;
            tmplist[cnt++] = i;

            for(int j = 0; j < n; j++){
                    if(i != j){
                        temp += runers[j].s;
                        tmplist[cnt++] = j;
                        if(cnt == 4) break;
                    }
            }
            //cout << "%" << i <<" "<< temp <
            if(temp - minimum < 0){
                minimum = temp;
                for(int j = 0; j < 4; j++) res[j] = tmplist[j];
            }
        }
        printf("%.2lf\n",minimum);
        for(int i = 0; i < 4; i++){
            //cout <
            cout << runers[res[i]].name << endl;

        }
    }
    return 0;
}

I - Import Spaghetti
在题目所给的有向图中找一个最小的环,并按照环的顺序逆向输出(任意起点皆可)
每个点bfs。。。居然在很智障的地方写re了(因为输入数组开小了哈哈哈)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 610;
const int inf = 0x3f3f3f3f;
vector<int> mp[maxn];
map<string,int> change;
char in[100010];
bool vis[maxn];
int num[maxn],res,n;
int pre[510],dis[510];
bool bfs(int root){
    memset(dis, inf, sizeof(dis));
    bool flag = true;
    queue<int> que;
    que.push(root);
    while(!que.empty()){
        int q = que.front();
        if(q==root){
            if(!flag){
                res = dis[root];
                return true;
            }else{
                flag = false;
            }
        }
        que.pop();
        for(int i=0;iint now;
            if(q==root){
                now = 1;
            }else{
                now = dis[q] + 1;
            }
            if(dis[mp[q][i]] > now){
                que.push(mp[q][i]);
                dis[mp[q][i]] = now;
            }
        }
    }
    return false;
}
int main(){
    string name[maxn];
    //freopen("29-496.4.in", "r", stdin);
    int i,j,a,k,killme;
    string str,now;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        cin>>name[i];
        change[name[i]] = i;
    }
    for(i=1;i<=n;i++){
        cin>>str>>a;
        int to = change[str];
        for(j=1;j<=a;j++){
            cin>>str;
            getchar();
            gets(in);
            int p1 = 0;
            int len = strlen(in);
            for(k=0;kif(in[k] == ','){
                    in[k] = '\0';
                    now = in + p1;
                    mp[change[now]].push_back(to);
                    k += 2;
                    p1 = k;
                    continue;
                }
            }
            now = in + p1;
            mp[change[now]].push_back(to);
        }
    }
    res = inf;
    int pos = -1, maxx = inf;
    for(i=1;i<=n;i++){
        memset(vis, false, sizeof(vis));
        bool flag = bfs(i);
        if(res < maxx && flag){
            maxx = res;
            pos = i;
        }
    }
    if(res == inf){
        cout<<"SHIP IT\n";
        return 0;
    }
    queue<int> que;
    que.push(pos);
    memset(dis, inf, sizeof(dis));
    while(!que.empty()){
        int q = que.front();
        que.pop();
        for(i=0;iint now;
            if(q == pos){
                now = 1;
            }else{
                now = dis[q] + 1;
            }
            if(dis[mp[q][i]] > now){
                pre[mp[q][i]] = q;
                que.push(mp[q][i]);
                dis[mp[q][i]] = now;
            }
            if(mp[q][i] == pos){
                pre[pos] = q;
                goto here;
            }
        }
    }
here:
    int x = pos, p =0;
    while(pre[x] != pos){
        cout<" ";
        x = pre[x];
    }
    cout<return 0;
}

G - Galactic Collegiate Programming Contest
比赛时想会不会是平衡树,但是并不会写
套了wenwenla的平衡树板子一下就过了
附上wenwenla板子 github地址:https://github.com/wenwenla/ACM

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1e5+10;
struct unit{
    int prom, time;

    bool operator==(unit rhs) const {
        return prom == rhs.prom && time == rhs.time;
    }
}save[maxn];
bool compare(unit a,unit b){
    if(a.prom == b.prom){
        return a.time < b.time;
    }else{
        return a.prom > b.prom;
    }
}
template<typename T, class _Comp = less>
struct Treap {
    const static int NODECNT = maxn;
    struct Node {
        int ch[2], p, sz;
        T v;
        void make(int _l, int _r, int _p, const T& _v) {
            ch[0] = _l; ch[1] = _r; p = _p; v = _v; sz = 1;
        }
    } node[NODECNT];
    int m_rt, mp[NODECNT], mp_idx, node_idx;

    void maintain(int x) {
        node[x].sz = 1;
        node[x].sz += node[x].ch[0] == -1 ? 0 : node[node[x].ch[0]].sz;
        node[x].sz += node[x].ch[1] == -1 ? 0 : node[node[x].ch[1]].sz;
    }
    _Comp cmp;

    explicit Treap(const _Comp& c) : cmp(c) { unsigned seed = 19971023; srand(seed); clear(); }

    Treap() : cmp(_Comp()) { unsigned seed = 19971023; srand(seed); clear(); }

    void clear() { m_rt = -1; mp_idx = -1; node_idx = 0; }

    void ins(const T& val) { _ins(m_rt, val); }
    void _ins(int& rt, const T& val) {
        if(rt == -1) {
            if(mp_idx == -1) { node[rt = node_idx++].make(-1, -1, rand(), val); }
            else { node[rt = mp[mp_idx--]].make(-1, -1, rand(), val); }
        } else {
            int type = cmp(node[rt].v, val);
            _ins(node[rt].ch[type], val);
            maintain(rt);
            if(node[rt].p < node[node[rt].ch[type]].p) rotate(rt, type);
        }
    }

    void del(const T& val) { _del(m_rt, val); }
    void _del(int& rt, const T& val) {
        assert(rt != -1);
        if(node[rt].v == val) {
            if(node[rt].ch[0] == -1) {
                mp[++mp_idx] = rt;
                rt = node[rt].ch[1];
            } else if(node[rt].ch[1] == -1) {
                mp[++mp_idx] = rt;
                rt = node[rt].ch[0];
            } else {
                int next = node[node[rt].ch[0]].p < node[node[rt].ch[1]].p;
                rotate(rt, next);
                _del(node[rt].ch[next ^ 1], val);
                maintain(rt);
            }
        } else {
            _del(node[rt].ch[cmp(node[rt].v, val)], val);
            maintain(rt);
        }
    }

    int find(const T& val) {
        int rt = m_rt;
        while(rt != -1) {
            if(node[rt].v == val) return rt;
            rt = node[rt].ch[cmp(node[rt].v, val)];
        }
        return -1;
    }

    void rotate(int& rt, int type) {
        int tmp = node[rt].ch[type];
        node[rt].ch[type] = node[tmp].ch[type ^ 1];
        node[tmp].ch[type ^ 1] = rt;
        maintain(rt); maintain(tmp);
        rt = tmp;
    }

    int kth(int k) {
        assert(k >= 1 && k <= size());
        int rt = m_rt, res = -1;
        while(rt != -1) {
            int le = node[rt].ch[0] == -1 ? 0 : node[node[rt].ch[0]].sz;
            if(le == k - 1) {
                res = node[rt].v;
                break;
            } else if(le > k - 1) {
                rt = node[rt].ch[0];
            } else {
                k -= le + 1;
                rt = node[rt].ch[1];
            }
        }
        return res;
    }

    int rank(const T& val) {
        int rt = m_rt, cnt = 0;
        while(rt != -1) {
            int le = node[rt].ch[0] == -1 ? 0 : node[node[rt].ch[0]].sz;
            if(cmp(node[rt].v, val)) {
                cnt += le + 1;
                rt = node[rt].ch[1];
            } else {
                rt = node[rt].ch[0];
            }
        }
        return cnt + 1;
    }

    int size() { return node[m_rt].sz; }
};
Treapbool(*)(unit, unit)> tr (compare);
int main(){
    tr.clear();
    int n,m,i,j,a,b;
    while(~scanf("%d%d",&n,&m)){
        for(i=1;i<=n;i++){
            save[i].prom = 0;
            save[i].time = 0;
            tr.ins(save[i]);
        }
        for(i=1;i<=m;i++){
            scanf("%d%d",&a, &b);
            tr.del(save[a]);
            ++save[a].prom;
            save[a].time += b;
            tr.ins(save[a]);
            printf("%d\n",tr.rank(save[1]));
        }
    }

    return 0;
}

D - Distinctive Character

先讲一下设定,对于两个只含有0、1的串,它们的每一位如果相同则它们相似度+1
题目会给我们很多(1e5)这样的串,然后让我们给出一个01串,使得我们给出的串和题目输入的所有串的相似度最大值最小
炒鸡绕的,首先建图(最大1e6个点),把每个题目输入的串都当作起点,它们的相邻点是仅有一个位置和自身位置不同的串。bfs一遍,这些源串起点能到达的最远点即是问题所求。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1e5+10;
const int inf = 0x3f3f3f3f;
char in[maxn];
int save[maxn];
queue<int> que;
int dis[(1<<20)+10];
int main(){
    int n,k,i,j;
    while (~scanf("%d%d",&n,&k)) {
        memset(save, 0, sizeof(save));
        memset(dis, inf, sizeof(dis));
        for(i=1;i<=n;i++){
            scanf("%s",in);
            for(j=0;jif(in[j]-'0'){
                    save[i] += (1<0;
            que.push(save[i]);
        }
        int farthest = -1,res = 0;
        while(!que.empty()){
            int q = que.front();
            que.pop();
            for(i=0;iint nnext = q ^ (1<if(dis[nnext] > dis[q]+1){
                    dis[nnext] = dis[q]+1;
                    que.push(nnext);
                    if(dis[nnext]>farthest){
                        res = nnext;
                        farthest = dis[nnext];
                    }
                }
            }
        }
        for(i=0;iif(res&(1<printf("1");
            }else{
                printf("0");
            }
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(BFS,二叉树)