BOJ 2014新生暑假个人排位赛09 整合

来源:互联网 发布:arclive有没有mac 编辑:程序博客网 时间:2024/05/24 07:35

A. diffsum


思路是从小到大排序后维护前缀和, 由于当前项比前面的所有项都大, 这样即可以O(1)求出当前值前方的绝对值

最后复杂度是O(N)



#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cctype>#include <cmath>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>#include <climits> #define MAXN 100005#define eps 1e-5#define MOD 1000000009 #define test #define For(i,m,n) for(int i=(m);i<(n);i++)#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)#define rep(i,m,n) for(int i=(m);i<=(n);i++)#define LL long long /*author birdstorm*/using namespace std;const double pi=acos(-1.0); template<class T>inline bool read(T &n){    T x = 0, tmp = 1; char c = getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();    if(c == EOF) return false;    if(c == '-') c = getchar(), tmp = -1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();    n = x*tmp;    return true;} template <class T>inline void write(T n) {    if(n < 0) {        putchar('-');        n = -n;    }    int len = 0,data[20];    while(n) {        data[len++] = n%10;        n /= 10;    }    if(!len) data[len++] = 0;    while(len--) putchar(data[len]+48);}LL a[MAXN];LL sum[MAXN];int main(){    //freopen("input.txt","r",stdin);    int n, m, t;    LL ans=0;    while(~scanf("%d",&n)){            ans=0;    For(i,0,n){        read(a[i]);    }    sort(a,a+n);    sum[0]=a[0];    For(i,1,n){        sum[i]=sum[i-1]+a[i];        ans=ans-sum[i-1]+(a[i]*(LL)i);    }    printf("%lld\n",ans);    }    return 0;}



B. 小妹妹送很多快递



和上次的题一样, 只是加入了一个计算...发现当前边连接了两个不同的联通块时,要计算他们的距离和可以O(1)算出

顺便依然需要特判0的情况, 居然在一个坑里跳了两次....



#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cctype>#include <cmath>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>#include <climits> #define MAXN 100005#define eps 1e-5#define MOD 1000000009#define INF 1000000009 #define test #define For(i,m,n) for(int i=(m);i<(n);i++)#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)#define rep(i,m,n) for(int i=(m);i<=(n);i++)#define LL long long /*author birdstorm*/using namespace std;const double pi=acos(-1.0); template<class T>inline bool read(T &n){    T x=0, tmp=1; char c=getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c=getchar();    if(c == EOF) return false;    if(c == '-') c=getchar(), tmp=-1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c=getchar();    n=x*tmp;    return true;} template <class T>inline void write(T n) {    if(n < 0) {        putchar('-');        n=-n;    }    int len=0,data[20];    while(n) {        data[len++]=n%10;        n /= 10;    }    if(!len) data[len++]=0;    while(len--) putchar(data[len]+48);}  pair<pair<int,int>,int> edge[200005];LL a[MAXN];int vis[MAXN];struct Disjoint{    vector<int> father,rank,num;    Disjoint(int n):father(n),rank(n),num(n){        For(i,0,n) father[i]=i;    }    int find(int v){        return father[v]=father[v]==v?v:find(father[v]);    }    void merge(int x,int y){        int a=find(x), b=find(y);        if(a==b) return;        if(rank[a]<rank[b]){            father[a]=b;            num[b]+=num[a];        }        else{            father[b]=a;            num[a]+=num[b];            if(rank[b]==rank[a]) ++rank[a];        }    }    void clear(int n){        For(i,0,n) father[i]=i, rank[i]=0, num[i]=1;    }    LL calc(int n){        return (LL)num[n];    }}; bool cmp(pair<pair<int,int>,int> a, pair<pair<int,int>,int> b){    return a.second<b.second;} int main(){    //freopen("input.txt","r",stdin);    //freopen("output.txt","w",stdout);    int u, v, w;    Disjoint D(MAXN);    int n, m;    int t;    scanf("%d",&t);    while(t--){        read(n),read(m);        int tot=0;        D.clear(n+1);        For(i,0,m){            read(u),read(v),read(w);            edge[tot++]=make_pair(make_pair(u,v),w);        }        sort(edge,edge+tot,cmp);        //For(i,0,tot) printf("%d %d %d--\n",edge[i].first.first,edge[i].first.second,edge[i].second);        bool found=false;        LL ans=0;        For(i,0,tot){            int u=edge[i].first.first;            int v=edge[i].first.second;            int w=edge[i].second;            int a1=D.find(u);            int a2=D.find(v);            if(a1!=a2){                if(w==0) w=1;                ans+=2LL*D.calc(a1)*D.calc(a2)*(LL)w;            }            D.merge(u,v);        }        printf("%lld\n",ans);    }    return 0;}



C. 新来的小妹妹



题意: 求字符串的最长重复子串长度

鉴于数据规模和随机数据, 给我用qsort水过去了= = 

正确的姿势是后缀数组(nlogn) 或者哈希(n^2)



#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cctype>#include <cmath>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>#include <climits> #define MAXN 4005#define eps 1e-5#define MOD 1000000009#define INF 1000000009 #define test #define For(i,m,n) for(int i=(m);i<(n);i++)#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)#define rep(i,m,n) for(int i=(m);i<=(n);i++)#define LL long long /*author birdstorm*/using namespace std;const double pi=acos(-1.0); template<class T>inline bool read(T &n){    T x=0, tmp=1; char c=getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c=getchar();    if(c == EOF) return false;    if(c == '-') c=getchar(), tmp=-1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c=getchar();    n=x*tmp;    return true;} template <class T>inline void write(T n) {    if(n < 0) {        putchar('-');        n=-n;    }    int len=0,data[20];    while(n) {        data[len++]=n%10;        n /= 10;    }    if(!len) data[len++]=0;    while(len--) putchar(data[len]+48);}int s[MAXN];char str[MAXN], *a[MAXN];int cmp(const void *a, const void *b){    return strcmp(*(char* const*)a,*(char* const*)b);} int work(char *a, char *b, int cnt){    while(*a==*b){        *a++, *b++;        cnt++;        if(!*a) break;    }    return cnt;}int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%s",str);        int n=strlen(str);        For(i,0,n) a[i]=&str[i];        qsort(a,n,sizeof(char*),cmp);        int m=-1;        For(i,0,n-1){            int aa=work(a[i],a[i+1],0);            if(aa>m) m=aa;        }        printf("%d\n",m);    }    return 0;}



D. 学妹去搬砖



dp, 小白书上有原题.......




E. 小妹妹快递公司的新址


树形dp, 改编自14年多校第一场1003

与那题不同, 这里求的是"不平衡"的子树个数

设dp[u][i]为以u为根,有i个顶点的子树个数

可以藉此写出状态转移.



#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cctype>#include <cmath>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>#include <climits> #define MAXN 205#define eps 1e-5 #define INF 0x3f3f3f3f #define test #define For(i,m,n) for(int i=(m);i<(n);i++)#define vecfor(iter,a) for(vector<int>::iterator iter=a.begin();iter!=a.end();iter++)#define rep(i,m,n) for(int i=(m);i<=(n);i++)#define LL long long /*author birdstorm*/using namespace std;const double pi=acos(-1.0);const LL MOD=10007;template<class T>inline bool read(T &n){    T x=0, tmp=1; char c=getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c=getchar();    if(c == EOF) return false;    if(c == '-') c=getchar(), tmp=-1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c=getchar();    n=x*tmp;    return true;} template <class T>inline void write(T n) {    if(n < 0) {        putchar('-');        n=-n;    }    int len=0,data[20];    while(n) {        data[len++]=n%10;        n /= 10;    }    if(!len) data[len++]=0;    while(len--) putchar(data[len]+48);} int n, m, t;struct edge{    int to,next;}e[MAXN<<1];bool vis[MAXN];int tot, head[MAXN];int total[MAXN];int dp[MAXN];int dp2[MAXN][MAXN];int child[MAXN]; void add(int u,int v){    e[tot].to=v; e[tot].next=head[u]; head[u]=tot++;    e[tot].to=u; e[tot].next=head[v]; head[v]=tot++;} void dfs(int u, int father){    dp[u]=0;    total[u]=1;    for(int i=head[u];i!=-1;i=e[i].next){        int v=e[i].to;        if(v==father) continue;        dfs(v,u);        dp[u]=max(dp[u],total[v]);        total[u]+=total[v];    }} void dfs2(int u,int father){    dp2[u][0]=dp2[u][1]=1; child[u]=1;    for(int i=head[u];i!=-1;i=e[i].next){        int v=e[i].to;        if(v==father) continue;        dfs2(v,u); child[u]+=child[v];    }    for(int i=head[u];i!=-1;i=e[i].next){        int v=e[i].to;        if(v==father) continue;        for(int i=child[u];i>=2;i--){            for(int j=1;j<i&&j<=child[v];++j)                dp2[u][i]=(dp2[u][i]+dp2[v][j]*dp2[u][i-j])%MOD;        }    }} int main(){    //freopen("output.txt","w",stdout);    int u, v, w;    read(t);    while(t--){        read(n);        memset(head,-1,sizeof head);        memset(total,-1,sizeof total);        memset(dp,0,sizeof dp);        tot=0;        For(i,0,n-1){            read(u),read(v);            add(u,v);        }        dfs(1,-1);        int minn=INF, ans;        for(int i=1;i<=n;i++){            int t=max(dp[i],n-total[i]);            if(minn>t){                minn=t;                ans=i;            }        }        memset(dp2,0,sizeof dp2);        dfs2(ans,-1);        int ret=0;        for(int k=head[ans];k!=-1;k=e[k].next){            for(int i=0;i<=n;++i) dp[i]=dp2[ans][i];            int v=e[k].to;            for(int i=1;i<=n;++i) for(int j=1;j<i;++j)                dp[i]=((dp[i]-dp[i-j]*dp2[v][j])%MOD+MOD)%MOD;            dp[0]=1;            for(int i=1;i<n;++i) dp[i]=(dp[i-1]+dp[i+1])%MOD;            for(int i=1;i<=n;++i) ret=((ret+dp2[v][i]*dp[i-1])%MOD+MOD)%MOD;        }         printf("%d\n",ret);    }    return 0;}



0 0