結果
| 問題 |
No.59 鉄道の旅
|
| コンテスト | |
| ユーザー |
kei
|
| 提出日時 | 2018-04-22 01:19:30 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 20 ms / 5,000 ms |
| コード長 | 3,616 bytes |
| コンパイル時間 | 1,597 ms |
| コンパイル使用メモリ | 171,732 KB |
| 実行使用メモリ | 11,312 KB |
| 最終ジャッジ日時 | 2024-06-27 05:32:19 |
| 合計ジャッジ時間 | 2,772 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 12 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
/*
<url:https://yukicoder.me/problems/no/59>
問題文============================================================
列車が出発駅から終点駅まで向かう。
その間に中間駅がN個ある。
列車は必ずすべての中間駅に停車する。
列車は出発駅では荷物を1つも積んでいない。
各中間駅では荷物を1つ積むか降ろすことが決まっている。
荷物には重さWが設定されている。
各中間駅では積むか降ろす荷物の重さが指定される。
・Wが正の数の場合は、重さ|W|の荷物を詰む。
・Wが負の数の場合は、重さ|W|の荷物を降ろす。
ここでひとつ変わったルールがある。
・荷物を積むまえにすでにその荷物の重さ以上の荷物がK個以上列車に積まれている場合にはその荷物を積めない。
この変わったルールのために荷物を積めない場合がある。
ルールに反しなければ必ず荷物を積む。
また、荷物を降ろす場合に指定した重さの荷物が積まれていない場合も考えられる。
この場合は指定された荷物が無いのだから荷物を降ろす必要は無い。
もし指定された重さの荷物があれば必ず1つ降ろす。
最終的に何個の荷物を最終駅まで運ぶことになるだろうか?
=================================================================
解説=============================================================
================================================================
*/
struct BIT {
int N;
vector<ll> bit;
BIT(int N):N(N) {
/* BITは[1..N]で扱う */
bit.resize(N + 1, 0);
}
void add(int x, int val){
while (x <= N) {
bit[x] += val;
x += x & -x;
}
}
int sum(int x){
int ret = 0;
while (x) {
ret += bit[x];
x &= (x - 1);
}
return (ret);
}
};
#define MAX_W 1000000
BIT bit(MAX_W);
ll solve(){
int res = 0;
int N,K; cin >> N >> K;
vector<int> W(N); for(auto& in:W) cin >> in;
for(int i = 0; i < N;i++){
if(W[i] > 0){
ll Sum = bit.sum(MAX_W);
if(W[i]!=1) Sum -= bit.sum(W[i]-1);
if(Sum >= K) continue;
bit.add(W[i],1);
}else{
W[i] = -W[i];
ll now = bit.sum(W[i]);
if(W[i]!=1) now -= bit.sum(W[i]-1);
if(now) bit.add(W[i],-1);
}
}
res = bit.sum(MAX_W);
return res;
}
int main(void) {
cin.tie(0); ios_base::sync_with_stdio(false);
cout << solve() << endl;
return 0;
}
kei