L1-8 静静的推荐(AC版)
起初,通过k次筛选,来依次访问n个学生,只能得14分,最后两个样例超时。 参考大佬思路,代码如下。
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N = 1e5 + 10;
bool vis[N];
typedef pair<int,int> pii;
pii a[N];
map<int,int> mp;
int main(){
int n,k,s;
int cnt= 0, ans = 0;
cin >> n >> k >> s;
for(int i = 0; i < n; i++){
cin >> a[i].x >> a[i].y;
if(a[i].x >= 175 && a[i].y >= s){
cnt++;
}
else{
mp[a[i].x]++;
}
}
for(int i = 175; i <= 290; i++){
if(mp[i] <= k) ans += mp[i];
else{
ans += k;
}
}
ans += cnt;
cout << ans << endl;
return 0;
}
L2-1 插松枝 Score 25
栈+队列+vector+模拟
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
queue<int> q;
stack<int> hez;
vector<int> ans[N];
int main(){
int n,m,k;
cin >> n >> m >> k;
for(int i = 0; i < n; i++){
int x;
cin >> x;
q.push(x);
}
int kk = 0;
while(q.size() || hez.size()){
if(hez.size() == 0 && q.size() && ans[kk].size() < k){
int t = q.front();
int len = ans[kk].size();
if(len > 0 && t > ans[kk][len-1]){
if(hez.size() < m){
hez.push(t);
q.pop();
}
else{
kk++;
}
}
else{
ans[kk].push_back(t);
q.pop();
}
}
else if(q.size() && ans[kk].size() < k){
int t = hez.top();
int len = ans[kk].size();
if(len > 0 && t > ans[kk][len-1]){
int tt = q.front();
if(tt > ans[kk][len - 1]){
if(hez.size() < m){
hez.push(tt);
q.pop();
}
else{
kk++;
}
}
else{
ans[kk].push_back(tt);
q.pop();
}
}
else{
ans[kk].push_back(t);
hez.pop();
}
}
else if(q.size() == 0 && hez.size() && ans[kk].size() < k){
if(hez.size()){
int t = hez.top();
int len = ans[kk].size();
if(len > 0 && t > ans[kk][len - 1]){
kk++;
}
else{
ans[kk].push_back(t);
hez.pop();
}
}
}
else if(ans[kk].size() == k){
kk++;
}
}
int mm = 0;
if(ans[kk].size() == 0){
mm = kk-1;
}
else{
mm = kk;
}
for(int i = 0; i <= mm; i++){
for(int j = 0; j < ans[i].size(); j++){
int len = ans[i].size();
if(j != len - 1){
cout <<ans[i][j] << " ";
}
else{
cout << ans[i][j] << endl;
}
}
}
return 0;
}
L2-2 老板的作息表 Score 25
阅读理解+模拟
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int>pii;
typedef long long ll;
const int N = 1e5 + 10;
bool vis[N];
pii a[N],ans[N];
int main(){
int n;
cin >> n;
getchar();
string s;
int eed = 24 * 3600;
for(int i = 0; i < n; i++){
getline(cin,s);
int len = s.size();
int st = ((s[0] - '0')*10 + (s[1] - '0')) * 3600;
st += ((s[3] - '0')*10 + (s[4] - '0')) * 60;
st += ((s[6] - '0')*10 + (s[7] - '0')) ;
int ed = ((s[11] - '0')*10 + (s[12] - '0')) * 3600;
ed += ((s[14] - '0')*10 + (s[15] - '0')) * 60;
ed += ((s[len-2] - '0')*10 + (s[len-1] - '0'));
a[i] = {st,ed};
}
sort(a,a + n);
int stt = 0;
int k = 0;
for(int i = 0; i < n; i++){
if(stt == a[i].x){
stt = a[i].y;
}
else{
ans[k++] = {stt,a[i].x};
stt = a[i].y;
}
}
if(stt != eed){
if(a[0].x == 0)
ans[k++] = {stt,eed-1};
else{
ans[k++] = {stt,eed};
}
}
for(int i = 0; i < k; i++){
int hh = ans[i].x / 3600;
if(hh < 10){
cout <<"0";
}
cout << hh<<":";
int mm = ans[i].x/60%60;
if(mm < 10) cout<<"0";
cout<<mm<<":";
int ss = ans[i].x%60;
if(ss<10) cout<<"0";
cout<<ss<<" - ";
int hh2 = ans[i].y / 3600;
if(hh2 < 10){
cout <<"0";
}
cout << hh2<<":";
int mm2 = ans[i].y/60%60;
if(mm2 < 10) cout<<"0";
cout<<mm2<<":";
int ss2 = ans[i].y%60;
if(ss2<10) cout<<"0";
cout<<ss2<<endl;
}
return 0;
}
|