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;
}