Submission #67884


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <bits/stdc++.h>
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
template <class A, class B> inline void chmod(A &a, const B b) { a = ((a%b)+b)%b; return; }
#define fi first
#define se second
const int mx[]={0, 1, 0, -1}, my[]={1, 0, -1, 0};
const int mod=1e9+7;
const int INF=1e9;
template <typename X>
struct Segment{
using FX=function<X(X,X)>; //X●X -> X となる関数の型
int n;
FX fx;
const X ex;
vector<X> dat;
Segment(int size,FX fx_,X ex_) : fx(fx_),ex(ex_){
n=1;
while(size>n){
n*=2;
}
dat.resize(n*2,ex_);
return;
}
void set(int i,X x){ dat[i+n-1]=x;}
void build(){
for(int k=n-2;k>=0;k--) dat[k]=fx(dat[2*k+1],dat[2*k+2]);
return;
}
void update(int i,X x){
i+=n-1;
dat[i]=x;
while(i>0){
i=(i-1)/2;
dat[i]=fx(dat[i*2+1],dat[i*2+2]);
}
return;
}
X get(int a,int b){ return (get_sub(a,b+1,0,0,n));}
X get_sub(int a,int b,int k,int l,int r){
if(r<=a || b<=l) return ex;
if(a<=l && r<=b) return dat[k];
X vl=get_sub(a,b,k*2+1,l,(l+r)/2);
X vr=get_sub(a,b,k*2+2,(l+r)/2,r);
return fx(vl,vr);
}
X operator[](const int &k) const{
return dat[k+n-1];
}
X find_rightest(int a, int b, X x) { return find_rightest_sub(a, b, x, 0, 0, n); }
X find_leftest(int a, int b, X x) { return find_leftest_sub(a, b, x, 0, 0, n); }
X find_rightest_sub(int a, int b, X x, int k, int l, int r) {
if (dat[k] > x || r <= a || b <= l) { // 自分の値がxより大きい or [a,b)が[l,r)の範囲外
return a - 1;
} else if (k >= n - 1) { // 自分が葉ならその位置をreturn
return (k - (n - 1));
} else {
int vr = find_rightest_sub(a, b, x, 2 * k + 2, (l + r) / 2, r);
if (vr != a - 1) { // 右の部分木を見て a-1 以外ならreturn
return vr;
} else { // 左の部分木を見て値をreturn
return find_rightest_sub(a, b, x, 2 * k + 1, l, (l + r) / 2);
}
}
}
X find_leftest_sub(int a, int b, X x, int k, int l, int r) {
if (dat[k] > x || r <= a || b <= l) { // 自分の値がxより大きい or [a,b)が[l,r)の範囲外
return b;
} else if (k >= n - 1) { // 自分が葉ならその位置をreturn
return (k - (n - 1));
} else {
int vl = find_leftest_sub(a, b, x, 2 * k + 1, l, (l + r) / 2);
if (vl != b) { // 左の部分木を見て b 以外ならreturn
return vl;
} else { // 右の部分木を見て値をreturn
return find_leftest_sub(a, b, x, 2 * k + 2, (l + r) / 2, r);
}
}
}
};
int main() {
string s;
int k;
int slen, cost, pos, sum;
priority_queue<int, vector<int>, greater<int>> alpha[26];
vector<int> isUsed;
auto fx1=[](int x1,int x2) -> int {return x1+x2;};
vector<char> str;
cin >> s >> k;
slen = s.size();
isUsed.assign(slen, 0);
Segment<int> rust(slen, fx1, 0);
for(int i=0; i<slen; i++ ) {
rust.update(i, 1);
alpha[s[i]-'a'].push(i);
}
for (int i=0; i<26; i++ ) {
alpha[i].push(INF);
}
sum=0;
for (int i=0; i<slen && sum<=k; i++ ) {
for (int j=0; j<26; j++ ) {
pos = alpha[j].top();
if (pos!=INF) {
cost = rust.get(0, pos)-1;
if (cost+sum<=k) {
isUsed[pos]=1;
alpha[j].pop();
rust.update(pos, 0);
sum+=cost;
str.push_back('a'+j);
break;
}
}
}
}
for (int i=0; i<slen; i++ ) {
if (isUsed[i]==0) {
str.push_back(s[i]);
}
}
for (auto e : str) {
cout << e;
}
cout << endl;
return (0);
}

ステータス

項目 データ
問題 0963 - 人気のユーザ名
ユーザー名 うなぎぜりぃ
投稿日時 2021-08-02 12:14:34
言語 C++17
状態 Accepted
得点 14
ソースコード長 4089 Byte
最大実行時間 257 ms
最大メモリ使用量 5908 KB

セット

セット 得点 Cases
1 ALL 14 / 14 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in1.txt AC 24 ms 604 KB
1
in2.txt AC 19 ms 956 KB
1
in3.txt AC 26 ms 1712 KB
1
in4.txt AC 23 ms 2096 KB
1
in5.txt AC 17 ms 2060 KB
1
in6.txt AC 18 ms 2024 KB
1
in7.txt AC 20 ms 1992 KB
1
in8.txt AC 24 ms 1960 KB
1
in9.txt AC 21 ms 1928 KB
1
in10.txt AC 21 ms 440 KB
1
in11.txt AC 20 ms 404 KB
1
in12.txt AC 19 ms 496 KB
1
in13.txt AC 28 ms 336 KB
1
in14.txt AC 23 ms 556 KB
1
in15.txt AC 18 ms 516 KB
1
in16.txt AC 17 ms 488 KB
1
in17.txt AC 140 ms 2892 KB
1
in18.txt AC 132 ms 2964 KB
1
in19.txt AC 131 ms 2920 KB
1
in20.txt AC 113 ms 3228 KB
1
in21.txt AC 87 ms 5272 KB
1
in22.txt AC 254 ms 5548 KB
1
in23.txt AC 250 ms 5736 KB
1
in24.txt AC 257 ms 5908 KB
1
in25.txt AC 16 ms 1644 KB
1
in26.txt AC 27 ms 1744 KB
1
in27.txt AC 22 ms 1704 KB
1
in28.txt AC 29 ms 1652 KB
1
in29.txt AC 21 ms 1756 KB
1
in30.txt AC 23 ms 1812 KB
1
in31.txt AC 172 ms 5744 KB
1
in32.txt AC 142 ms 4800 KB
1