CCF-CSP 202303(持续更新)

1.田地丈量

 

 

蓝色的矩形长算出来是负的,不能算进去

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
int main()
{
    int n, a, b;
    cin >> n >> a >> b;
    LL res = 0;
    while (n--) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        if(min(x2, a) - max(x1, 0)>=0&& min(y2, b) - max(0, y1)>=0)
        res +=(LL) (min(x2, a) - max(x1, 0)) * (min(y2, b) - max(0, y1));
    }
    cout << res << endl;
    return 0;
}

 2.垦田计划

 

二分答案

求开垦n块区域最少耗时,用二分来寻找答案,找到符合的最小答案

类似于每日一题:二分答案,里面有详尽的解释,具体见每日一题:二分答案_m0_74087709的博客-CSDN博客

15分代码: 

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5 + 10;
int t[N], c[N];    
int n, m, k;
bool check(int x) {
    int res = 0;
    for (int i = 1; i <= n; i++) res += (t[i] - x) * c[i];
    if (res <= m) return true;
    return false;
}
int main()
{

    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) cin >> t[i] >> c[i];
    int max1 = 0;
    for (int i = 1; i <= n; i++) max1 = max(max1, t[i]);
    int l = k, r = max1;
    while (l < r) {
        int mid = (l + r) / 2;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    cout << l << endl;
    return 0;
}

不知道哪里出错了,然后看了别人的代码,发现有一种情况没有考虑,就是当t[i]<x时,不用考虑资源消耗,而我恰恰将该资源消耗算了,导致总资源消耗加了负值,导致错误

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#define int long long 
using namespace std;
const int N = 1e5 + 10;
int t[N], c[N];    
int n, m, k;
bool check(int x) {
    int res = 0;
    for (int i = 1; i <= n; i++) {
        if (t[i] < x) continue;
        res += (t[i] - x) * c[i];
        if (res > m) return false;
    }
    return true;
}
signed main()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) cin >> t[i] >> c[i];
    int max1 = 0;
    for (int i = 1; i <= n; i++) max1 = max(max1, t[i]);
    int l = k, r = max1;
    while (l < r) {
        int mid = (l + r) / 2;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    cout << l << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值