HPU 积分赛

文章目录

  • A
  • B
  • C
  • E
  • F
  • G
  • H
  • I
  • J
  • K
  • L

A

处理出最多可划分的序列个数,如果是奇数就输出“Yes”,否则输出“No”
其实,只要偶数不在两边并且序列个数为奇就一定是“Yes”,否则就是“No”,不过当时没想到,这里就给出当时写的代码吧

#include
using namespace std;
#define ll long long
int a[110];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for (int i = 0;i < n;i ++)
        cin>>a[i];
    if (!(a[0] & 1) || !(a[n-1] & 1))
    {
        cout<<"No"<<'\n';
        return 0;
    }
    int len = 0,ans = 0;
    for (int i = 0;i < n;i ++)
    {
        ++ len;
        if (a[i] & 1 && len & 1)
        {
            ++ans;
            len = 0;
        }
        else
            ++i,++len;
    }
    cout<<((ans & 1) ? "Yes" : "No")<<'\n';
    return 0;
}

B

当两个列表有重合时,可以输出一个一位的,否则输出大小即可

#include
using namespace std;
#define ll long long
int num1[10],num2[10];
int main()
{
    ios::sync_with_stdio(false);
    int n,m,a = 10,b = 10,x;
    cin>>n>>m;
    for (int i = 0;i < n;i ++)
    {
        cin>>x;
        a = min(a,x);
        num1[x] = 1;
    }
    for (int i = 0;i < m;i ++)
    {
        cin>>x;
        b = min(b,x);
        num2[x] = 1;
    }
    for (int i = 1;i < 10;i ++)
    {
        if (num1[i] && num2[i])
        {
            cout<<i<<'\n';
            return 0;
        }
    }
    cout<<min(a,b)<<max(a,b)<<'\n';
    return 0;
}

C

傻逼题,题意没有说两人事先可以商量,只要全输出1.00即可,做过一次仍然wa+3,我是真的菜/大哭

#include
using namespace std;
#define ll long long
int main()
{
    //ios::sync_with_stdio(false);
    for (int i = 0;i < 4;i ++)
        printf("1.00\n");
    return 0;
}

E

k为1时,输出最小值
k为2,输出两边最小
k大于2,输出最大值

#include
using namespace std;
#define ll long long
const int maxn = 1e5 + 10;
int a[maxn];
int main()
{
    ios::sync_with_stdio(false);
    int n,k;
    cin>>n>>k;
    for (int i = 0;i < n;i ++)
        cin>>a[i];
    int xx = a[0],yy = a[n-1];
    sort(a,a + n);
    if (k == 1) cout<<a[0]<<'\n';
    else if (k == 2)
            cout<<max(xx,yy)<<'\n';
    else cout<<a[n-1]<<'\n';
    return 0;
}

F

前缀pre维护p * ai最大值,后缀sa维护r * ai最大值,枚举q * ai
答案即为ans=max(ans,pre[i]+q * ai+sa[i])

#include
using namespace std;
#define ll long long
const int maxn = 1e5 + 10;
ll a[maxn],pre[maxn],sa[maxn];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    ll p,q,r;
    cin>>p>>q>>r;
    for (int i = 1;i <= n;i ++)
        cin>>a[i];
    ll ans = - (1LL*1<<62);
    for (int i = 0;i <= n+1;i ++)
        pre[i]=sa[i]=ans;
    for (int i = 1;i <= n;i ++)
        pre[i] = max(p*a[i],pre[i-1]);
    for (int i = n;i;i --)
        sa[i] = max(sa[i+1],r*a[i]);
    for (int i = 1;i <= n;i ++)
        ans = max(ans,pre[i]+q*a[i]+sa[i]);
    cout<<ans<<'\n';
    return 0;
}

G

签到

#include
using namespace std;
#define ll long long
const int maxn = 550;
int a[maxn],b[maxn];
int main()
{
    ios::sync_with_stdio(false);
    int t,kase = 1001;
    cin>>t;
    while (t --)
    {
        int n,m;
        cin>>n>>m;
        for (int i = 0;i < n;i ++)
            cin>>a[i];
        for (int i = 0 ;i < m;i ++)
            cin>>b[i];
        sort(a,a + n);
        sort(b,b + m);
        cout<<"Problem "<<kase++<<':'<<'\n';
        cout<<"Shortest judge solution: "<<a[0]<<" bytes."<<'\n';
        cout<<"Shortest team solution: ";
        if (m == 0) cout<<"N/A";
        else cout<<b[0];
        cout<<" bytes."<<'\n';
    }
    return 0;
}

H

思维,只要模m后的余数相等,差就一定是m的倍数,把相同的压入vector即可

#include
using namespace std;
#define ll long long
const int maxn = 1e5 + 10;
vector<int> ve[maxn];
int main()
{
    ios::sync_with_stdio(false);
    int n,k,m,x;
    cin>>n>>k>>m;
    for (int i = 0;i < n;i ++)
    {
        cin>>x;
        ve[x%m].push_back(x);
    }
    for (int i = 0;i < m;i ++)
    {
        if (ve[i].size() >= k)
        {
            int f = 0;
            cout<<"Yes"<<'\n';
            for (int j = 0;j < k;j ++)
            {
                if (f) cout<<' ';
                f = 1;
                cout<<ve[i][j];
            }
            cout<<'\n';
            return 0;
        }
    }
    cout<<"No"<<'\n';
    return 0;
}

I

应该是个dp记录一下状态吧,待补

J

贪心,比较一下溢出的和正好的哪个花费最小即可,m代表幂,pre代表前缀最小单价

#include
using namespace std;
#define ll long long
ll cost[100],dp[100];
int pre[100],m[100];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    ll L;
    cin>>n>>L;
    cost[0] = 9223372036854775807;
    pre[0] = 0;
    for (int i = 1;i <= n;i ++)
    {
        cin>>cost[i];
        m[i] = 1 << (i-1);
    }
    m[0] = m[n];
    for (int i = 1;i <= n;i ++)
    {
        if (cost[i]*(m[n]/m[i]) < cost[pre[i-1]] * (m[n]/m[pre[i-1]]))
            pre[i] = i;
        else pre[i] = pre[i-1];
    }
    for (int i = 0;i <= 50;i ++)
        dp[i] = cost[0];
    ll ans = cost[0];
    for (int i = 1;i <= n;i ++)
    {
        int j=i;
        ll x = L,w = 0;
        while (j > 0 && x > 0)
        {
            dp[i] = min(dp[i],w+((x-1)/m[pre[j]]+1)*cost[pre[j]]);
            w += x / m[pre[j]] * cost[pre[j]];
            x -= x / m[pre[j]] * m[pre[j]];
            j = pre[j]-1;
        }
        ans = min(dp[i],ans);
    }
    cout<<ans<<'\n';
    return 0;
}

K

矩快板子题,f(n) = f(n-1) + 2*f(n-2) + n3,矩阵快速幂加速一下即可

#include
using namespace std;
#define ll long long
const ll mod = 123456789;
const int N = 6;
struct Matrix
{
    int n;
    ll d[6][6];
    void init(int n)
    {
        this -> n = n;
        memset(d,0,sizeof(d));
    }
    Matrix operator *(Matrix &b)
    {
        Matrix ans;
        ans.init(n);
        for (int i = 0;i < n;i ++)
            for (int j = 0;j < n;j ++)
                for (int k = 0;k < n;k ++)
                    ans.d[i][j]=(ans.d[i][j]+d[i][k]*b.d[k][j])%mod;
        return ans;
    }
};
ll a[N][N] = {
        {1,1,0,0,0,0},
        {2,0,0,0,0,0},
        {1,0,1,0,0,0},
        {3,0,3,1,0,0},
        {3,0,3,2,1,0},
        {1,0,1,1,1,1}
        };
ll b[6] = {2,1,8,4,2,1};
Matrix quick(Matrix a,ll b)
{
    Matrix res;
    res.init(a.n);
    for (int i = 0;i < res.n;i ++)
        res.d[i][i] = 1;
    while (b)
    {
        if (b & 1) res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
int main()
{
    ll t,n;
    scanf("%lld",&t);
    while (t --)
    {
        scanf("%lld",&n);
        if (n < 2)
        {
            printf("%lld\n",n);
            continue;
        }
        Matrix x,ans;
        x.init(6),ans.init(6);
        for (int i = 0;i < N;i ++)
            for (int j = 0;j < N;j ++)
                x.d[i][j] = a[i][j];
        for (int i = 0;i<N;i++) ans.d[0][i]=b[i];
        x = quick(x,n - 1);
        ans = ans * x;
        printf("%lld\n",ans.d[0][1]);
    }
    return 0;
}

L

签到,瞎搞一下即可

#include
using namespace std;
#define ll long long
struct node
{
    int s,d;
}a[1010];
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for (int i = 0;i < n;i ++)
        cin>>a[i].s>>a[i].d;
    int ans = a[0].s,tmp;
    for (int i = 1;i < n;i ++)
    {
        if (a[i].s > ans) ans = a[i].s;
        else
        {
            tmp = ((ans - a[i].s - 1) / a[i].d + 1) * a[i].d + a[i].s;
            if (tmp <= ans) tmp += a[i].d;
            ans = tmp;
        }
    }
    cout<<ans<<'\n';
    return 0;
}

你可能感兴趣的:(HPU 积分赛)