洛谷训练新手村之“BOSS战入门综合练习1”题解

摘要:
其次,订单从小到大。贪婪的想法:优先吃体力消耗较少的苹果。解决方案思路:通过第一个数字,判断最后两个数字是否为三位数,以及这三个数字是否从1到9。因为直接枚举可以打开许多循环,所以我可以采用类似于状态压缩的思路。每个状态对应一个三进制数,然后三进制数的每个位对应10位。这更容易解决。

P1478 陶陶摘苹果(升级版)

题目链接:https://www.luogu.com.cn/problem/P1478
题目大意:陶陶有s点体力值,每个苹果消耗体力值,问s体力值最多能摘多少苹果。
解题思路:首先过滤掉摘不到的苹果。其次从小到大排序, 贪心 思想:优先选择消耗体力值小的苹果。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5050;
int n, m, s, a, b, x[maxn], y[maxn], ans;
int main() {
    cin >> n >> s >> a >> b;
    a += b;
    while (n --) {
        cin >> x[m] >> y[m];
        if (x[m] <= a) m ++;
    }
    sort(y, y+m);
    for (int i = 0; i < m; i ++) {
        if (y[i] <= s) {
            s -= y[i];
            ans ++;
        }
        else break;
    }
    cout << ans << endl;
    return 0;
}

P1618 三连击(升级版)

题目链接:https://www.luogu.com.cn/problem/P1618
题目大意:求出所有壁纸为A:B:C的三位数。
解题思路:遍历第一个数,判断后两个数是否都是三位数且刚好这三个数从1到9。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int A, B, C;
bool t[10];
bool check(int a) {
    if (a % A) return false;
    int b = a/A*B, c = a/A*C;
    if (b >= 1000 || c >= 1000) return false;
    int d = a * 1000000 + b * 1000 + c;
    memset(t, 0, sizeof(t));
    while (d) {
        t[d%10] = true;
        d /= 10;
    }
    for (int i = 1; i < 10; i ++) if (!t[i]) return false;
    return true;
}
int main() {
    cin >> A >> B >> C;
    bool flag = false;
    for (int a = 123; a < 333; a ++) {
        if (check(a)) {
            flag = true;
            cout << a << " " << a/A*B << " " << a/A*C << endl;
        }
    }
    if (!flag) puts("No!!!");
    return 0;
}

P1579 哥德巴赫猜想(升级版)

题目链接:https://www.luogu.com.cn/problem/P1579
题目大意:给你一个数,找三个素数使得它们的和是这个数。
解题思路:数据量偏大,所以采用素数筛法,然后再枚举。
实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 20020;
bool pp[maxn];
int n, a, p[maxn];
void init() {
    pp[0] = pp[1] = true;
    for (int i = 2; i < maxn; i ++)
        if (!pp[i]) {
            p[n++] = i;
            for (int j = i; j < maxn/i; j ++) pp[i*j] = true;
        }
}
int main() {
    init();
    cin >> a;
    for (int i = 0; i < n; i ++)
        for (int j = i; j < n; j ++) {
            if (p[i] + p[j] >= a) continue;
            if (!pp[a- p[i] - p[j] ]) {
                cout << p[i] << " " << p[j] << " " << a-p[i]-p[j] << endl;
                return 0;
            }
        }
    return 0;
}

P2089 烤鸡

题目链接:
题目大意:
猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10种配料(芥末、孜然等),每种配料可以放1—3克,任意烤鸡的美味程度为所有配料质量之和
现在,Hanke想要知道,如果给你一个美味程度,请输出这10种配料的所有搭配方案

解题思路:枚举出所有的状态方案。因为直接枚举得开好多循环,所以我可以采用类似 状态压缩 的思想,每个状态对应一个三进制数,然后三进制数的每一位对应10个数字,这样比较好解决一些。
实现代码如下:

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

int a[10], n, res;

int main() {
    cin >> n;
    if (n < 10 || n > 30) {
        puts("0");
        return 0;
    }
    n -= 10;
    for (int i = 0; i < 59049; i ++) {
        int tmp = i, cnt = 0;
        for (int j = 9; j >= 0; j --) {
            a[j] = tmp % 3;
            tmp /= 3;
            cnt += a[j];
        }
        if (cnt == n) res ++;
    }
    cout << res << endl;
    for (int i = 0; i < 59049; i ++) {
        int tmp = i, cnt = 0;
        for (int j = 9; j >= 0; j --) {
            a[j] = tmp % 3;
            tmp /= 3;
            cnt += a[j];
        }
        if (cnt != n) continue;
        for (int j = 0; j < 10; j ++) {
            if (j) putchar(' ');
            cout << a[j]+1;
        }
        cout << endl;
    }
    return 0;
}

免责声明:文章转载自《洛谷训练新手村之“BOSS战入门综合练习1”题解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇三方库漏洞检测与CI/CDLinq之关键字基本查询下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Kubernetes监控etcd集群(自带metrics接口)

Kubernetes用operator部署prometheus上面采用Kubernetes部署prometheus 我们可以使用prometheus来监控自带metrics接口的应用。 etcd是Kubernetes的数据库,自带接口,我们可以用etcd作为实例来看看怎么操作。 一、监控etcd集群 1.1、查看接口信息 二进制和kubeadm安装方式...

深入理解openstack网络架构(1)

原文地址: https://blogs.oracle.com/ronen/entry/diving_into_openstack_network_architecture 译文转载自:http://blog.csdn.net/halcyonbaby/article/details/41524447 前言 openstack网络功能强大同时也相对更复杂。本...

laravel框架实现商品订单加入到rabbitMQ队列

一、 创建一个任务类 php artisan make:job QueuedTest  //执行之后app/Jobs目录下生成一个QueuedTest.php 二、创建控制器 php artisan make:controller QueuedController 执行之后生成app/Http/Controllers/QueuedControlle...

K8S常用命令

引自: https://www.cnblogs.com/dalianpai/p/12370635.html #查看所有namespace的pods运行情况 kubectl get pods --all-namespaces #查看具体pods,记得后边跟namespace名字哦 kubectl get pods kubernetes-dashboard...

docker --- (入门必读)

容器 容器就是一个视图隔离、资源可限制、独立文件系统的进程集合。所谓“视图隔离”就是能够看到部分进程以及具有独立的主机名等;控制资源使用率则是可以对于内存大小以及 CPU 使用个数等进行限制。容器就是一个进程集合,它将系统的其他资源隔离开来,具有自己独立的资源视图。 docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及...

System.Web.Mvc 找到的程序集清单定义与程序集引用不匹配

System.IO.FileLoadException: 未能加载文件或程序集“System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)文...