202303-1(田地丈量)
题目链接:
计算机软件能力认证考试系统http://118.190.20.162/view.page?gpid=T165题意:
思路:
没什么难度,直接模拟就行。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<queue>
#include<unordered_map>
#include<set>
#include<stack>
#include<cmath>
#include<unordered_set>
#define PII pair<int, int>
#define ll long long
using namespace std;
const int N = 110;
int n, a, b, ans;
int x1, y1, x2, y2;
void solve()
{
cin >> n >> a >> b;
for (int i = 0; i < n; ++ i)
{
cin >> x1 >> y1 >> x2 >> y2;
x1 = max(x1, 0), x1 = min(x1, a);
y1 = max(y1, 0), y1 = min(y1, b);
if(x1 < a || y1 < b)
{
x2 = max(x2, 0), x2 = min(x2, a);
y2 = max(y2, 0), y2 = min(y2, b);
ans += (x2 - x1) * (y2 - y1);
}
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// int T;
// cin >> T;
//
// while(T--)
// {
solve();
// }
return 0;
}
202303-2(垦田计划)(二分)
题目链接:
计算机软件能力认证考试系统http://118.190.20.162/view.page?gpid=T164题意:
思路(70分思路):
一开始没有想到二分,只是想到了优先队列,所以就先使用了处理优先队列的办法,就是将所有的 ti 和 ci都用pair 存到优先队列(大根堆)里,然后我们就可以对这个大根堆进行处理,也就是每次都将需要时间最大的拿出来,给他 ci 资源使它所用时间减少1,以此类推,直到用完 m 为止,此时 t 的最大值就是题目所求的结果。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<unordered_set>
#define PII pair<int, int>
#define ll long long
using namespace std;
const int N = 110;
priority_queue<PII > pq;
int n, m, k;
int t, c;
void solve()
{
cin >> n >> m >> k;
for (int i = 0; i < n; ++ i)
{
cin >> t >> c;
pq.push({t, c});
}
while(m > 0)
{
auto it = pq.top();
pq.pop();
t = it.first, c = it.second;
if(m - c >= 0)
{
t --;
if(t < k) break;
pq.push({t, c});
}
m -= c;
}
int ans = pq.top().first;
if(ans >= k) cout << ans << endl;
else cout << k << endl;
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// int T;
// cin >> T;
//
// while(T--)
// {
solve();
// }
return 0;
}
满分思路(二分):
交了第一发之后直接超时,我一看,妈呀,m最大1e9,可不超时吗,然后我就想怎么能不超时,我们可以看到,t 的最大值不超过 1e5,那么我们如果用二分枚举的话,就绝对不会出现这种状态,我们只需要从k 到 t 的最大值用二分枚举,然后写一个check就行。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<unordered_set>
#define PII pair<int, int>
#define ll long long
using namespace std;
const int N = 110;
vector<PII > vc;
int n, m, k;
int t, c;
int mt = -1;
bool check(int x)
{
int sum = m;
for (int i = 0; i < n; ++ i)
{
int t = vc[i].first, c = vc[i].second;
if(t > x) sum -= c * (t - x);
}
if(sum >= 0) return true;
else return false;
}
void solve()
{
cin >> n >> m >> k;
for (int i = 0; i < n; ++ i)
{
cin >> t >> c;
mt = max(mt, t);
vc.push_back({t, c});
}
int l = k, r = mt;
while(l < r)
{
int mid = l + r >> 1;
if(check(mid)) r = mid;
else l = mid + 1;
}
cout << l << endl;
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// int T;
// cin >> T;
//
// while(T--)
// {
solve();
// }
return 0;
}