自学内容网 自学内容网

【LGR-200-Div.4】洛谷入门赛 #27 A - H题解,包含(C++, Go语言)

前言:

        本文为【LGR-200-Div.4】洛谷入门赛 #27 A - H题解

        我只是一个只会各种暴力法的蒟蒻,这场比赛没有参加,是比赛完去写的,但是那个题目昨天晚上才能提交,导致拖久了一点

        最后面贴一个Go语言的,反正也没人看

        觉得有帮助或者写的不错可以点个赞

目录

题A:

题目大意和解题思路:

代码(C++):

题B:

题目大意和解题思路:

代码(C++):

题C:

题目大意和解题思路:

代码(C++):

题D:

题目大意和解题思路:

代码(C++):

题E:

题目大意和解题思路:

代码(C++):

题F:

题目大意和解题思路:

代码(C++):

题G1, G2:

题目大意和解题思路:

代码(C++):

题H:

题目大意和解题思路:​编辑

代码(C++):

A - H Go语言题解:


题A:

B4026 [语言月赛 202409] 灵感 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目大意和解题思路:

如果迅风在下午写下的文章的字数之和严格大于他在上午写下的文章的字数之和

简单的判断,注意仔细读题即可

代码(C++):

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    
    int a, b, c, d;
    std::cin >> a >> b >> c >> d;
    if (c + d > a + b) {
        std::cout << "Yes\n";
    } else {
        std::cout << "No\n";
    }
}

题B:

B4027 [语言月赛 202409] 重聚 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目大意和解题思路:

  • 小紫在分离时间 ≥t1​ 分钟时开启感应,如果她和小蓝距离不超过 d1​,那么可以感应到小蓝的位置。
  • 小蓝在分离时间 ≥t2​ 分钟时开启感应,如果她和小紫距离不超过 d2​,那么可以感应到小紫的位置。

当双胞胎的一个人能感应到另一个人的位置,就可以行动使得两人重聚。

现在小紫和小蓝已经分离了 t 分钟,当前距离为 d。她们都在原地等候。

请判断至少还需要几分钟,才能让双胞胎中的一个人感应到另一个人的位置?

首先可以判断d1,d2是否都不满足

然后可以定义两个最大值time1, time2,当d1满足,time1就赋值成t1 - t
注意如果t1 - t < 0 的话,那就只需要0分钟

代码(C++):

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    
    int t, d, t1, d1, t2, d2;
    std::cin >> t >> d >> t1 >> d1 >> t2 >> d2;
    int res = 0;
    if (d > d1 && d > d2) {
        res = -1;
    } else {
        int time1 = 101, time2 = 101;
        if (d <= d1) {
            time1 = t1 - t;
            if (time1 < 0) {
                time1 = 0;
            }
        }
        if (d <= d2) {
            time2 = t2 - t;
            if (time2 < 0) {
                time2 = 0;
            }
        }
        res = std::min(time1, time2);
    }
    std::cout << res << "\n";
}

题C:

B4028 [语言月赛 202409] 转盘 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目大意和解题思路:

阅读理解,从1开始遍历到n,表示1到n等奖,算出每一个奖的中奖概率i / sum跟m比较即可

注意题目n的范围,要算出总和sum的话,sum需要是long long类型

代码(C++):

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    
    long long n, sum = 0;
    double m;
    std::cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    int res = -1;
    for (int i = 1; i <= n; i++) {
        double v = double(i) / double(sum) * 100;
        if (v >= m) {
            res = i;
            break;
        }
    }
    std::cout << res << "\n";
}

题D:

B4029 [语言月赛 202409] 种子 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目大意和解题思路:

x天开始每天额外增加y
使用一个while循环,当成长值大于k的时候退出循环即可

代码(C++):

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    
    int x, y, w, k;
    std::cin >> x >> y >> w >> k;
    int day = 0, len = 0;
    while (true) {
        day++;
        if (day < x) {
            len += day / w;
        } else {
            len += day / w + y;
        }
        if (len >= k) {
            break;
        }
    }
    std::cout << day << "\n";
}

题E:

B4030 [语言月赛 202409] 距离 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目大意和解题思路:

由于是从下标1开始的,所以可以使用一个长度为n + 1的二维数组来记录每一个情况
然后循环,每次变换之后,都找出当前二维数组的最大值即可

代码(C++):

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    
    int n, m;
    std::cin >> n >> m;
    std::vector<std::vector<int>> A(n + 1, std::vector<int> (n + 1, 0));
    while (m--) {
        int op, a, b, c;
        std::cin >> op >> a >> b >> c;

        if (op == 1) {
            A[a][b] += c;
        } else {
            A[a][b] -= c;
        }

        int res = A[0][0];
        for (auto row : A) {
            for (int x : row) {
                res = std::max(x, res);
            }
        }
        std::cout << res << "\n";
    }
}

题F:

B4031 [语言月赛 202409] 始终 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目大意和解题思路:

遍历两次,当前的字符相等计数器加1即可

代码(C++):

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    
    std::string s;
    std::cin >> s;
    int cnt = 0, n = s.size();
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            if (s[i] == s[j]) {
                cnt++;
            }
        }
    }
    std::cout << cnt << "\n";
}

题G1, G2:

B4032 [语言月赛 202409] 数字 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目大意和解题思路:

简单的贪心加数学

这题当然可以暴力模拟写,创建一个长度为n的数组,要保证x尽可能小,数组第一位为1,然后最后一位不断+1
在模拟的过程中我们可以发现:

要使得除以p的余数尽可能小,那么可以尽可能让余数为0,也就是各个位数之和刚好等于p,但是根据给出的p和n的范围,存在即使全是9,余数也不会是0

此时,n * 9 < p
那么可以让此时的余数为1,也就是10^(n -1)次方

由上面的模拟可以得出:

当n * 9 < p的时候,答案就为10 ^ (n - 1)次方

否则,开始暴力模拟,我这里用数学方法优化,首先让p - 1(也就是第一位)
如果p大于等于9,就从最后一位(各位)开始补充9,并且p -= 9
然后从十位数...依次进行操作
当p 不足9的时候,剩余的p加到当前位置即可

 

代码(C++):

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    
    int n, p;
    std::cin >> n >> p;
    if (n * 9 < p) {
        //数据量大的话,这里可以用字符串
        std::cout <<  static_cast<long long>(std::pow(10, n - 1)) << "\n";
    } else {
        std::vector<int> A(n);
        A[0] = 1;
        int idx = n - 1;
        p--;
        while (p) {
            if (p >= 9) {
                A[idx] = 9;
                p -= 9;
                idx--;
            } else {
                A[idx] += p;
                p = 0;
            }
        }
        std::string res = "";
        for (auto x : A) {
            res += std::to_string(x);
        }
        std::cout << res << "\n";
    }
}

题H:

B4033 [语言月赛 202409] 考试 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目大意和解题思路:

我觉得这一题没有上一题难度高

先求出一个差值数组,然后对数组进行排序,然后遍历数组

如果当前为0,那么答案加1,x++, y不变
如果当前不为0,那么答案加上当前的的值加1,然后x++, y--

注意,当x 与 y的差值为1的时候,只需让当前平局即可,答案只需要加上当前的值,然后就是 y --
 

代码(C++):

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    
    int n;
    std::cin >> n;
    std::vector<int> A(n);
    std::vector<int> B(n);
    for (int i = 0; i < n; i++) {
        std::cin >> A[i];
    }
    for (int i = 0; i < n; i++) {
        std::cin >> B[i];
    }
    std::vector<int> imp;
    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
        int v = A[i] - B[i];
        if (v > 0) {
            x++;
        } else if (v < 0) {
            y++;
            imp.push_back(-v);
        } else {
            imp.push_back(0);
        }
    }
    if (x > y) {
        std::cout << "0\n";
        return 0;
    }
    int res = 0, idx = 0;
    std::sort(imp.begin(), imp.end());
    while (x <= y) {
        if (imp[idx] == 0) {
            res++; x++; idx++;
        } else {
            if (x == y) {
                res += imp[idx]; idx++; y--;
            } else {
                res += imp[idx] + 1; idx++; y--; x++;
            }
        }
    }
    std::cout << res << "\n";
}

A - H Go语言题解:

package main

import (
"bufio"
. "fmt"
"math"
"os"
"sort"
"strconv"
)

var (
in  *bufio.Reader
out *bufio.Writer
)

func main() {
in = bufio.NewReader(os.Stdin)
out = bufio.NewWriter(os.Stdout)
defer out.Flush()
solve_G1_G2()
}

func solve_A() {
var a, b, c, d int
Fscan(in, &a, &b, &c, &d)
var res string
if a+b < c+d {
res = "Yes"
} else {
res = "No"
}
Fprintln(out, res)
}

func solve_B() {
var t, d, t1, d1, t2, d2 int
Fscan(in, &t, &d, &t1, &d1, &t2, &d2)
res := 0
if d > d1 && d > d2 {
res = -1
} else {
time1, time2 := 101, 101
if d1 >= d {
time1 = t1 - t
if time1 < 0 {
time1 = 0
}
}
if d2 >= d {
time2 = t2 - t
if time2 < 0 {
time2 = 0
}
}
res = min(time1, time2)
}
Fprintln(out, res)
}

func solve_C() {
n, m := 0, float64(0)
Fscan(in, &n, &m)
m /= 100
sum := 0
for i := 1; i <= n; i++ {
sum += i
}
res := -1
for i := 1; i <= n; i++ {
p := float64(i) / float64(sum)
if p >= m {
res = i
break
}
}
Fprintln(out, res)
}

func solve_D() {
var x, y, w, k int
Fscan(in, &x, &y, &w, &k)
l, day := 0, 0
for true {
day++
if day < x {
l += day / w
} else {
l += (day / w) + y
}
if l >= k {
break
}
}
Fprintln(out, day)
}

func solve_E() {
n, m := 0, 0
Fscan(in, &n, &m)
A := make([][]int, n+1)
for i := range A {
A[i] = make([]int, n+1)
}
for ; m > 0; m-- {
var op, a, b, c int
Fscan(in, &op, &a, &b, &c)
if op == 1 {
A[a][b] += c
} else {
A[a][b] -= c
}
res := 0
for _, row := range A {
for _, x := range row {
res = max(res, x)
}
}
Fprintln(out, res)
}
}

func solve_F() {
s := ""
Fscan(in, &s)
n := len(s)
res := 0
for i := 0; i < n; i++ {
c := s[i]
for j := i; j < n; j++ {
if c == s[j] {
res++
}
}
}
Fprintln(out, res)
}

func solve_G1_G2() {
n, p := 0, 0
Fscan(in, &n, &p)
if n*9 < p {
//数据量大的话,这里可以用字符串
Fprintln(out, int64(math.Pow(10, float64(n)-1)))
return
} else {
A := make([]int, n)
A[0] = 1
p--
idx := n - 1
for p > 0 {
if p >= 9 {
A[idx] = 9
p -= 9
idx--
} else {
A[idx] += p
p = 0
}
}
res := ""
for _, x := range A {
res += strconv.Itoa(x)
}
Fprintln(out, res)
}
}

func solveH() {
n := 0
Fscan(in, &n)
A := make([]int, n)
B := make([]int, n)
for i := range A {
Fscan(in, &A[i])
}
for i := range B {
Fscan(in, &B[i])
}
diff := make([]int, n)
for i := 0; i < n; i++ {
diff[i] = A[i] - B[i]
}
x, y := 0, 0
imp := make([]int, 0)
for _, val := range diff {
if val > 0 {
x++
} else if val < 0 {
y++
imp = append(imp, -val)
} else {
imp = append(imp, 0)
}
}
if x > y {
Fprintln(out, 0)
return
}
res, idx := 0, 0
sort.Ints(imp)
for true {
if imp[idx] == 0 {
res++
x++
idx++
} else {
if x == y {
res += imp[idx]
idx++
y--
} else {
res += imp[idx] + 1
idx++
x++
y--
}
}
if x > y {
break
}
}
Fprintln(out, res)
}


原文地址:https://blog.csdn.net/2401_83669813/article/details/142289077

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!