#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("seat.in", "r", stdin);
freopen("seat.out", "w", stdout);
int n, k;
cin >> n >> k;
set<int> s;
for (int i = 1; i <= k; i++) {
s.insert(i);
}
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (s.empty()) {
cout << 0 << " ";
continue;
}
auto it = s.lower_bound(x);
int l = -1, r = -1;
if (it != s.end()) r = *it;
if (it != s.begin()) l = *prev(it);
int p;
if (l == -1) p = r;
else if (r == -1) p = l;
else if (x - l <= r - x) p = l;
else p = r;
cout << p << " ";
s.erase(p);
}
cout << "\n";
return 0;
}
#include <iostream>
using namespace std;
int n, m, a[200010];
bool check(long long c) {
int cnt = 1, maxn = 0;
long long sum = 0;
bool flag = false;
for (int i = 1; i <= n; i++) {
sum += a[i];
maxn = max(maxn, a[i]);
if (sum <= c) {
continue;
}
if (!flag) {
int t = (maxn + 1) >> 1;
if (sum - t <= c) {
sum -= t;
flag = true;
continue;
}
}
if (++cnt > m) {
return false;
}
sum = maxn = 0;
flag = false;
i--;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
long long l = 0, r = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
l = max(l, (long long)a[i] >> 1);
r += a[i];
}
while (l <= r) {
long long mid = (l + r) >> 1;
if (check(mid)) {
r = mid - 1;
}
else {
l = mid + 1;
}
}
cout << r + 1;
return 0;
}
C 稳定区间
By meowjiao
初步观察
O(n3) 暴力枚举每个区间肯定会 TLE,考虑优化。
进一步观察
考虑固定区间右端点 r,向左移动区间左端点 l。
我们发现,在 l 向左移动的过程中,区间长度 r−l+1 变大,而区间最大值增大或不变。
因此,当 l 向左移动时,t 只可能增大,不可能不变或减小。
这意味着,在 r 固定时,如果某个 l 不合法,那么更小的 l 也一定不合法,只能让 l 增大。
因此,稳定区间具有单调性,可以考虑双指针。
最终求解
考虑双指针的更新策略。
我们每次让 r 右移一个单位。如果此时 t 超过了 s,那么很显然 l 就需要右移,直到 t 小于等于 s。
那么如何维护区间最大值呢?很显然,可以用滑动窗口解决。
注意计算 t 时开 long long。
标准程序
#include <iostream>
#include <deque>
using namespace std;
int a[200010];
deque<int> q;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long s;
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int l = 0, ans = 0;
for (int r = 1; r <= n; r++) {
while (!q.empty() && a[q.back()] <= a[r]) {
q.pop_back();
}
q.push_back(r);
while (!q.empty() && (long long)a[q.front()] * (r - l + 1) > s) {
if (q.front() == l) {
q.pop_front();
}
l++;
}
ans = max(ans, r - l + 1);
}
cout << ans;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> a(n), d(n - 1);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i + 1 < n; i++) d[i] = a[i + 1] - a[i];
int l = 0;
while (l < n - 1) {
int r = l;
while (r + 1 < n - 1 && ((d[r] % 2 + 2) % 2) == ((d[r + 1] % 2 + 2) % 2)) r++;
sort(d.begin() + l, d.begin() + r + 1);
l = r + 1;
}
每个上界都有实际分配方案达到,所以答案就是这三个数的最大值。时间复杂度 O(nlogn),空间 O(n)。所有和使用 long long。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;cin>>n;vector<long long>a(n);long long S=0,L=0,H=0;for(auto &x:a){cin>>x;S+=x;}sort(a.begin(),a.end());
for(int i=0;i<n/3;++i){L+=a[i];H+=a[n-1-i];}cout<<max({S-3*L,3*H-S,H-L})<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;long long T,K;cin>>n>>T>>K;long long cur=1,ans=0;
for(int i=0;i<n;++i){long long d,b;cin>>d>>b;cur=max(cur,d);long long e=min(T,d+K-1);if(cur<=e){long long c=min(b,e-cur+1);ans+=c;cur+=c;}}
cout<<ans<<'\n';
}
C. 围村收费
算法与正确性
使用映射按车辆编号汇总记录。每辆车的进入时间初始为 00:00,离开时间初始为 24:00;出现 IN 时覆盖进入时间,出现 OUT 时覆盖离开时间。这同时处理了只有进入、只有离开和两条记录完整的情况,也不依赖输入顺序。
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n,x;cin>>n>>x;map<string,pair<int,int>>mp;
for(int i=0;i<n;++i){string id,t,op;cin>>id>>t>>op;int tm=stoi(t.substr(0,2))*60+stoi(t.substr(3,2));if(!mp.count(id))mp[id]={0,1440};if(op=="IN")mp[id].first=tm;else mp[id].second=tm;}
long long ans=0;for(auto p:mp){int t=p.second.second-p.second.first;int h=(max(0,t-120)+59)/60;ans+=min(24,h*x);}cout<<ans<<'\n';
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100005;
const ll inf = (1LL << 60);
int n;
ll a[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
ll sum = 0;
vector<ll> v[3];
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
v[a[i] % 3].push_back(a[i]);
}
for (int i = 0; i < 3; i++) {
sort(v[i].begin(), v[i].end());
}
int r = sum % 3;
if (r == 0) {
cout << n << '\n';
cout << sum << '\n';
return 0;
}
ll del = inf;
int cnt = 0;
if (r == 1) {
if ((int)v[1].size() >= 1) {
del = v[1][0];
若队首获胜,连续胜场加一;若挑战者获胜,他成为新队首,连续胜场从 1 开始。只要达到 k 就立即输出。
若扫描完所有选手仍未结束,当前队首已经是全局最大战斗力中最先出场者。他此后不可能失败,必然最终连续获胜 k 次,因此可以直接输出,不需要模拟可能高达 109 轮的比赛。
每名选手只在首次来到队首附近时处理一次,时间 O(n),空间 O(n)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;long long k;cin>>n>>k;vector<long long>a(n);for(auto &x:a)cin>>x;
int win=0;long long streak=0;for(int i=1;i<n;++i){if(a[win]>=a[i])++streak;else{win=i;streak=1;}if(streak>=k){cout<<win+1<<'\n';return 0;}}
cout<<win+1<<'\n';
}
C. 奇偶油箱
算法与正确性
设 dp[s][p] 表示处理完前若干个油箱、总油量恰好为 s、最后一个油箱油量奇偶性为 p 的方案数。
枚举当前油箱装入 x∈[0,ai] 升。除了第一个油箱外,只有 x 的奇偶性与前一个油箱不同才能转移:把原状态的方案数加到 ndp[s+x][xmod2]。同时要求 s+x≤m。
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin>>T;while(T--){long long a[3],b[3];for(auto &x:a)cin>>x;for(auto &x:b)cin>>x;long long have=0,need=0;
for(int i=0;i<3;++i){long long d=a[i]-b[i];if(d>=0)have+=d/2;else need-=d;}cout<<(have>=need?"Yes":"No")<<'\n';}}
#include <bits/stdc++.h>
using namespace std;
bool ok(long long n,long long m,long long M){
long long s=0;
while(m&&s+1<M&&s<n){s+=s+1;--m;}
return s>=n||(__int128)s+(__int128)m*M>=n;
}
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin>>T;
while(T--){long long n,m;cin>>n>>m;if(!ok(n,m,n)){cout<<-1<<'\n';continue;}
long long l=1,r=n;while(l<r){long long mid=(l+r)/2;if(ok(n,m,mid))r=mid;else l=mid+1;}
cout<<l<<'\n';}
}
E. 因数加权和
算法与正确性
把“枚举每个数的所有因数”改成“枚举因数,再枚举它的倍数”。因数 d 对答案的贡献为
d+2d+⋯+⌊n/d⌋d=d2q(q+1),q=⌊n/d⌋.
于是答案为 ∑d=1ndq(q+1)/2。所有因数都被计入一次,因此这个换序与原式完全相同。
从左端点 l 出发,令 q=⌊n/l⌋。商仍为 q 的最右端点是 r=⌊n/q⌋,这一整段的贡献可以合并为
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);
long long n;cin>>n;__int128 ans=0;
for(long long l=1,r;l<=n;l=r+1){long long q=n/l;r=n/q;
ans+=(__int128)(l+r)*(r-l+1)/2*((__int128)q*(q+1)/2);}
cout<<(long long)ans<<'\n';
}