2016年12月8日 星期四

USACO 2013 November Contest, Gold Problem 1. Empty Stalls

http://usaco.org/index.php?page=viewproblem2&cpid=346

Tips : something like union-find tree

#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

const int MAX_N = 3e6 + 6;

struct DisjointSet {
    int p[MAX_N];
    void init(int n) {
        for (int x=0;n>=x;x++) p[x] = x;
    }
    int Find(int x) {
        return p[x]==x?x:p[x]=Find(p[x]);
    }
    void Union(int x,int y) {
        p[Find(x)]=Find(y);
    }
} djs;

int nxt[MAX_N];

void go(int x,int n) {
    nxt[djs.Find(x)]=(nxt[djs.Find((nxt[djs.Find(x)]+1)%n)]);    
    djs.Union(djs.Find(x),(nxt[djs.Find(x)])%n);
}

void pp(int n) {
    for (int x=0;n>x;x++) cout<<nxt[djs.Find(x)]<<' ';
    cout<<endl;
}

int main () {
    if (fopen("empty.in","r")) {
        freopen("empty.in","r",stdin);
        freopen("empty.out","w",stdout);
    }
    int n,k;
    while (scanf("%d %d",&n,&k) != EOF) {
        djs.init(n);
        for (int x=0;n>x;x++) {
            nxt[x] = x;
        }
        for (int x=0;k>x;x++) {
            long long int a,b,c,d;
            scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
            for (int i=0;a>i;i++) {
                for (int j=1;b>=j;j++) {
//                    cout<<"put at "<<(c*j+d)%n<<endl;
                    go((c*j+d)%n,n);
//                    pp(n);
                }
            }
        }
        printf("%d\n",nxt[djs.Find(0)]);
    }
}

沒有留言:

張貼留言