福州大学第十一届程序设计竞赛

  • Post author:
  • Post category:其他


Problem A 大王叫我来巡山呐

题意:第一天是星期一,求n天中有多少个星期六或者星期天

水题,直接求

#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

int main()
{
    int tmp,ans,n;
    while(~scanf("%d",&n)){
        ans=n/7*2+n%7/6;
        printf("%d\n",ans);
    }
    return 0;
}



Problem B 防守阵地 I




题意:在n的数中取m个连续的,使得1*a[k]+2*a[k+1]+…+m*a[k+m]最大


分析:先求sum[i]为从i开始的m个数的和,先求最开始m个数的答案,然后向后推,ans[i]=ans[i-1]-sum[i]+a[i+m]*m,比较求最大







#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1000005
using namespace std;
int sum[N],a[N];
int main()
{
    int n,m,i,j,ans,tmp;
    while(~scanf("%d%d",&n,&m)){
        for(i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        j=1;
        sum[j]=0;
        ans=0;
        for(i=1;i<=m;i++){
            sum[j]+=a[i];
            ans+=a[i]*i;
        }
        j++;
        for(i=m+1;i<=n;i++,j++){
            sum[j]=sum[j-1]+a[i]-a[j-1];
        }
        tmp=ans;
        for(i=m+1;i<=n;i++){
            tmp=tmp+m*a[i]-sum[i-m];
            if(tmp>ans)
            ans=tmp;
        }
        printf("%d\n",ans);
    }
    return 0;
}




Problem C shadow

从根节点开始dfs求每个结点的父节点,从士兵结点开始往根节点遍历,标记遍历到的点,求标记点的叛军总和,存边不要用vector,会RE

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
#include<vector>
using namespace std;
int cnt,vis[N],mark[N],pre[N],num[N],a[N],head[N];
struct edge{
    int v,next;
}e[N*2];
void addedge(int u,int v){
    e[cnt].v=v;e[cnt].next=head[u];head[u]=cnt++;
    e[cnt].v=u;e[cnt].next=head[v],head[v]=cnt++;
}
void dfs(int u){
    int i,j;
    for(i=head[u];i!=-1;i=e[i].next){
        j=e[i].v;
        if(!vis[j]){
            vis[j]=1;
            pre[j]=u;
            dfs(j);
        }
    }
}
int main()
{
    int n,k,i,j,ans,x,y;
    while(~scanf("%d%d",&n,&k)){
        for(i=1;i<=n;i++){
            vis[i]=0;
            pre[i]=0;
            mark[i]=0;
            head[i]=-1;
            a[i]=0;
        }
        cnt=0;
        memset(vis,0,sizeof(vis));
        memset(mark,0,sizeof(mark));
        for(i=1;i<=n;i++){
            scanf("%d",&num[i]);
        }
        while(k--){
            scanf("%d",&x);
            a[x]=1;
        }
        for(i=1;i<n;i++){
            scanf("%d%d",&x,&y);
            addedge(x,y);
        }
        vis[1]=1;
        dfs(1);
        for(i=1;i<=n;i++){
            if(a[i]){
                j=pre[i];
                while(j!=1&&!mark[j]){
                    mark[j]=1;
                    j=pre[j];
                }
            }
        }
        ans=0;
        for(i=1;i<=n;i++){
            if(mark[i])
            ans+=num[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}

Problem E 防守阵地 II

线段树,还不会,再看看用树状数组能不能做

Problem G Nostop

矩阵快速幂

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define INF -1
#define LL __int64
#define N 55
using namespace std;
LL n;
struct Matrix{
    LL m[N][N];
};
Matrix mul(Matrix a,Matrix b){
    Matrix ret;
    LL i,j,k;
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            ret.m[i][j]=INF;
            for(k=1;k<=n;k++){
                if(a.m[i][k]==INF||b.m[k][j]==INF)
                continue;
                if(ret.m[i][j]==INF){
                    ret.m[i][j]=a.m[i][k]+b.m[k][j];
                }else if(ret.m[i][j]>a.m[i][k]+b.m[k][j]){
                    ret.m[i][j]=a.m[i][k]+b.m[k][j];
                }
            }
        }
    }
    return ret;
}
void solve(Matrix a,LL k){
    LL i,j;
    Matrix tmp;
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            tmp.m[i][j]=a.m[i][j];
        }
    }
    k--;
    while(k){
        if(k&1)
        tmp=mul(tmp,a);
        a=mul(a,a);
        k>>=1;
    }
    printf("%I64d\n",tmp.m[1][n]);

}
int main()
{
    LL i,j,t,h,k,u,v,w;
    scanf("%I64d",&t);
    while(t--){
        scanf("%I64d%I64d%I64d",&n,&h,&k);
        Matrix a;
        for(i=1;i<=n;i++){
            for(j=1;j<=n;j++)
            a.m[i][j]=INF;
        }
        while(h--){
            scanf("%I64d%I64d%I64d",&u,&v,&w);
            if(w<a.m[u][v]||a.m[u][v]==INF)
            a.m[u][v]=w;
        }
        solve(a,k);
    }
    return 0;
}
/*
100
3 3 1000004
1 2 1
2 3 1
3 1 1
*/



版权声明:本文为a634771197原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。