杭电杯第三场
大约 2 分钟
杭电杯第三场
个人题解,欢迎指正
题目变得好难,我也一直在推A题,还没推出来,最后一个多小时来看P1009,还好写出来了
P1003
思路:
签到题
Code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define lowbit(x) ((x)&(-x))
#define fi first
#define se second
#define pb push_back
#define cf(_) int _;cin >> _;while(_--)
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
while (c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
x *= f;
}
int main()
{
int T;
cin >> T;
getchar();
while(T--)
{
string s;
getline(cin,s);
string ans;
bool flag = false;
// cout << s << endl;
for(int i = 0;i < s.size();i++)
{
if(!i) ans.pb(s[i]);
else
{
// cout << s[i] ;
if(s[i] == ' ') {
flag = true;
continue;
}
if(flag)
{
// cout << s[i];
ans.pb(s[i]);
flag = false;
}
}
}
for(int i = 0;i < ans.size();i++)
{
cout << char(ans[i] - 32);
}
cout << endl;
}
return 0;
}
P1009
思路:
将排序后开始遍历,让开始的为第一个,然后将小于的数都加入一个优先队列中,同时将更新为当前队列中最小的,然后将数据弹出,要么队列为空,要么剩余带的快递数量为。
Code:
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define WA return 0;
#define ptn putchar('\n')
using namespace std;
inline ll read() { ll x = 0, z = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-')z = -1; c = getchar(); }while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); }return z * x; }
inline void writ(ll x) { if (x < 0) { putchar('-'); x = (~x) + 1; }if (x > 9)writ(x / 10); putchar(x - x / 10 * 10 + 48); }
#define pii pair<int,int>
const ll INF = 1e18+7;
void Qingtuan() {
int n = read(), k = read();
priority_queue<pii,vector<pii>,greater<pii>>pq; //改为数组排序也可
pii t;
for (int i = 1; i <= n; i++)
{
t.first = read(); t.second = read();
pq.push(t);
}
priority_queue<int,vector<int>,greater<int>>p;
int res=0;
int r;
int m;
while (!p.empty()||!pq.empty())
{
m=k;
if(p.empty()){
t=pq.top();
r=t.second;
}else{
r=p.top();
}
while (!pq.empty())
{
t=pq.top();
if(t.first<=r){
pq.pop();p.push(t.second);
r=min(r,t.second);
}else{
break;
}
}
while (!p.empty()&&m)
{
p.pop();m--;
}
res++;
}
writ(res); ptn;
}
int main() {
//cin.tie(nullptr)->sync_with_stdio(false);
//freopen("data.in", "r", stdin);freopen("data1.out", "w", stdout);
int T = read(); while (T--)
Qingtuan();
WA
}