hiho[Offer收割]编程练习赛1

链接:http://hihocoder.com/contest/hihointerview3/problems

A:九宫,中文题。

分析:3*3的幻方,爆搜即可。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int MAX=151;
const int MOD1=1000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const ll INF=10000000010;
typedef unsigned long long ull;
int g,a[4][4],b[4][4],h[4],c[4],q[10],x,y;
int nexl(int l,int r) {
    if (r<3) return l;
    return l+1;
}
int nexr(int l,int r) {
    if (r<3) return r+1;
    return 1;
}
int judge() {
    for (int i=1;i<10;i++)
    if (!q[i]) return 0;
    for (int i=1;i<=3;i++)
    if (h[i]!=15||c[i]!=15) return 0;
    if (x!=15||y!=15) return 0;
    return 1;
}
void dfs(int l,int r) {
    if (l==4&&r==1) {
        if (judge()) {
            g++;
            for (int i=1;i<=3;i++)
                for (int j=1;j<=3;j++) b[i][j]=a[i][j];
        }
        return ;
    }
    if (a[l][r]) { dfs(nexl(l,r),nexr(l,r));return ; }
    for (int i=1;i<10;i++)
    if (!q[i]) {
        q[i]=1;h[l]+=i;c[r]+=i;
        if (l==r) x+=i;
        if (l+r==4) y+=i;
        a[l][r]=i;
        dfs(nexl(l,r),nexr(l,r));
        q[i]=0;h[l]-=i;c[r]-=i;
        if (l==r) x-=i;
        if (l+r==4) y-=i;
        a[l][r]=0;
    }
}
int main()
{
    int i,j;
    memset(q,0,sizeof(q));
    for (i=1;i<=3;i++)
        for (j=1;j<=3;j++) {
            scanf("%d", &a[i][j]);
            q[a[i][j]]=1;
            h[i]+=a[i][j];c[j]+=a[i][j];
            if (i==j) x+=a[i][j];
            if (i+j==4) y+=a[i][j];
        }
    g=0;dfs(1,1);
    if (g==1) {
        for (i=1;i<=3;i++) {
            for (j=1;j<=3;j++) printf("%d ", b[i][j]);
            printf("\n");
        }
    } else printf("Too Many\n");
    return 0;
}


B:优化延迟,中文题。。

分析:二分+优先队列。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int MAX=151;
const int MOD1=1000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const ll INF=10000000010;
typedef unsigned long long ull;
int a[N],b[N];
int cmd(int x,int y) {
    return x>y;
}
priority_queue<int>Q;
ll judge(int x,int n) {
    int i,k=1,w;
    ll ret=0;
    while (!Q.empty()) Q.pop();
    for (i=1;i<=x;i++) Q.push(a[i]);
    for (i=x+1;i<=n;i++) {
        w=Q.top();Q.pop();
        ret+=(ll)w*k;k++;
        Q.push(a[i]);
    }
    while (!Q.empty()) {
        w=Q.top();Q.pop();
        ret+=(ll)w*k;k++;
    }
    return ret;
}
int main()
{
    int i,n,l,r,mid;
    ll q,ans;
    scanf("%d%lld", &n, &q);
    for (i=1;i<=n;i++) {
        scanf("%d", &a[i]);b[i]=a[i];
    }
    sort(b+1,b+n+1,cmd);
    ans=0;
    for (i=1;i<=n;i++) ans+=(ll)b[i]*i;
    if (ans>q) { printf("-1\n");return 0; }
    l=0;r=n;mid=(l+r)/2;
    while (l+1<r)
    if (judge(mid,n)<=q) { r=mid;mid=(l+r)/2; }
    else { l=mid;mid=(l+r)/2; }
    printf("%d\n", r);
    return 0;
}


C:建造基地,中文题。。

分析:对每一层直接暴力背包即可,注意处理溢出k的情况。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=20010;
const int MAX=151;
const int MOD1=100000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll INF=1000000000010;
const ll MOD=1000000007;
typedef unsigned long long ull;
int n,m,k,t,a[N],b[N];
ll ans,dp[N];
int build(int x) {
    if (x>n) return 1;
    int i,j;
    ll mi=INF;
    dp[0]=0;
    for (i=1;i<=2*k;i++) dp[i]=INF;
    if (x!=1) {
        for (i=1;i<=m;i++) b[i]/=t;
    }
    for (i=1;i<=m;i++)
    if (b[i]) {
        for (j=b[i];j<=2*k;j++) dp[j]=min(dp[j],dp[j-b[i]]+a[i]);
    }
    for (i=1;i<=m;i++)
    if (b[i]>=k) mi=min(mi,(ll)a[i]);
    for (i=k;i<=2*k;i++) mi=min(mi,dp[i]);
    ans+=mi;
    if (mi==INF) return 0;
    return build(x+1);
}
int main()
{
    int i,ca;
    scanf("%d", &ca);
    while (ca--) {
        scanf("%d%d%d%d", &n, &m, &k, &t);
        for (i=1;i<=m;i++) scanf("%d", &a[i]);
        for (i=1;i<=m;i++) scanf("%d", &b[i]);
        ans=0;
        if (build(1)) printf("%lld\n", ans);
        else printf("No Answer\n");
    }
    return 0;
}

你可能感兴趣的:(hiho[Offer收割]编程练习赛1)