2016年中国大学生程序设计竞赛(杭州) 部分题解

【题解可以看这里,我提供了部分题的代码】点击打开链接

【A】

【代码】

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int a[500000];
LL sum[500000];

int main()
{
    int T, ks = 1;
    scanf("%d",&T);
    while(T--)
    {
        int n, m;
        scanf("%d%d",&n,&m);
        sum[0] = 0;
        LL ans = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&a[i]);
            sum[i] = sum[i-1] + 1LL*a[i];
            ans += a[i];
        }
        if(ans % m)
        {
            printf("Case #%d: -1\n",ks++);
            continue;
        }
        int t = 0;
        ans  = ans / m;
        for(int i = 1; i < n; i++)
        {
            if(sum[i] % ans == 0) t -= 2;
        }
        t = t + n - 1 + m - 1;
        printf("Case #%d: %d\n",ks++,t);
    }
    return 0;
}

【B】
【代码】
//
//Created by just_sort 2016/10/28
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, LL> pp;
#define MP(x,y) make_pair(x,y)
const int maxn = 200005;
const int N = 1005;
const int M = 2010;
const int inf = 0x3f3f3f3f;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//head
int indgree[maxn],pre[maxn],lowlink[N],sccno[N],dfs_clock,scc_cnt,maxc[maxn],ans;
struct node{
    LL x, y, r;
    int c;
}a[N];
vector <int> E[N];
vector <int> G[N];//new graph.
stack <int> s;
void init(){
    memset(indgree, 0, sizeof(indgree));
    ans = 0;
}
void dfs(int u)
{
    pre[u] = lowlink[u] = ++dfs_clock;
    s.push(u);
    for(int i = 0; i < E[u].size(); i++)
    {
        int v = E[u][i];
        if(!pre[v])
        {
            dfs(v);
            lowlink[u] = min(lowlink[u], lowlink[v]);
        }
        else if(!sccno[v])
            lowlink[u] = min(lowlink[u], pre[v]);
    }
    if(lowlink[u] == pre[u])
    {
        scc_cnt++;
        for(;;)
        {
            int x = s.top(); s.pop();
            sccno[x] = scc_cnt;
            maxc[scc_cnt] = min(maxc[scc_cnt], a[x].c);
            if(x == u) break;
        }
    }
}

void suodian(int n)
{
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(pre, 0, sizeof(pre));
    while(!s.empty()) s.pop();
    for(int i = 1; i <= n; i++)
    {
        if(!pre[i])
        {
            dfs(i);
        }
    }
}

bool check(int i, int j)
{
    double d = sqrt(1.0*(a[i].x-a[j].x)*(a[i].x-a[j].x) + 1.0*(a[i].y-a[j].y)*(a[i].y-a[j].y));
    if(1.0*a[i].r >= d) return 1;
    else return 0;
}

int main()
{
    int T,n,ks = 1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        init();
        for(int i = 0; i <= n; i++) E[i].clear();
        for(int i = 0; i <= n; i++) G[i].clear();
        for(int i = 0; i <= n; i++) maxc[i] = inf;
        for(int i = 1; i <= n; i++) scanf("%lld%lld%lld%d",&a[i].x,&a[i].y,&a[i].r,&a[i].c);
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                if(i == j) continue;
                if(check(i,j)) E[i].push_back(j);
            }
        }
        suodian(n);
        for(int i = 1; i <= n; i++){
            for(int j = 0; j < E[i].size(); j++){
                int v = E[i][j];
                if(sccno[i] != sccno[v]){
                    indgree[sccno[v]]++;
                    G[sccno[i]].push_back(sccno[v]);
                }
            }
        }
        for(int i = 1; i <= scc_cnt; i++)
        {
            if(!indgree[i])
            {
                ans += maxc[i];
            }
        }
        printf("Case #%d: %d\n",ks++, ans);
    }
    return 0;
}


【C】

注意卡精度!

【代码】

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int n, a[100010];
const double eps = 1e-8;

int main()
{
    int T,ks = 1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
        double v = a[n] - a[n-1];
        int  ans = 1;
        double x;
        for(int i = n - 1; i; i--)
        {
            x = a[i] - a[i-1];
            if(x <= v + eps)
            {
                v = x;
                ans++;
            }
            else
            {
                ans += (int(double(x - eps)/v) + 1);
                v = (double(x)/(int(double(x-eps)/v)+1));
            }
        }
        printf("Case #%d: %d\n",ks++,ans);
    }
}


【F】

【代码】

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const LL inf = 1e20;
char s[22];
int a[22];

int main()
{
    int T,ks=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        int len = strlen(s);
        memset(a, 0, sizeof(a));
        for(int i = 0; i < len; i++){
            a[i+1] = s[i] - '0';
        }
        LL ans = -inf;
        LL t1 = 0;
        LL t2 = 0;
        LL t3 = 0;
        LL t4 = 0;
        LL t5 = 0;
        for(int i = 2;  (i+3)<= len; i++){
            for(int j = i+1; (j+2) <= len; j++){
                int k = j + 1;
                int l = k + 1;
                t1 = t2 = t3 = t4 = t5 = 0;
                for(int t = 1; t < i; t++) t1 = t1*10 + 1LL*a[t];
                for(int t = i; t < j; t++) t2 = t2*10 + 1LL*a[t];
                for(int t = j; t < k; t++) t3 = t3*10 + 1LL*a[t];
                for(int t = k; t < l; t++) t4 = t4*10 + 1LL*a[t];
                for(int t = l; t <= len; t++) t5 = t5*10 + 1LL*a[t];
                //cout<<t1<<" "<<t2<<" "<<t3<<" "<<t4<<" "<<t5<<endl;
                ans = max(ans, t1 + t2 - 1LL*(t3*t4)/t5);
            }
        }
        printf("Case #%d: %I64d\n",ks++,ans);
    }
}


【K】

【代码】

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int maxn = 2000;
vector<int>g[maxn];
int from[maxn];
bool used[maxn];

bool match(int x)
{
    for(int i = 0; i < (int)g[x].size(); i++){
        if(!used[g[x][i]]){
            used[g[x][i]] = true;
            if(from[g[x][i]] == -1 || match(from[g[x][i]]))
            {
                from[g[x][i]] = x;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int T, ks = 1;
    scanf("%d",&T);
    while(T--)
    {
        int s, n;
        scanf("%d%d",&s,&n);
        if(s < n) swap(s, n);
        if(n > 500)
        {
            printf("Case #%d: No\n",ks++);
            continue;
        }
        for(int i = 0; i < maxn; i++) g[i].clear();
        memset(from, -1, sizeof(from));
        int tot = 0;
        for(int i = 1; i <= n; i++){
            int t = i + s;
            for(int j = 1; j <= n; j++){
                if(t % j == 0){
                    g[i].push_back(j);
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            memset(used,0,sizeof(used));
            if(match(i)){
                tot++;
            }
        }
        if(tot == n){
            printf("Case #%d: Yes\n",ks++);
        }
        else
        {
            printf("Case #%d: No\n",ks++);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值