QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#372958 | #3665. Watch Later | zezoo050# | AC ✓ | 1318ms | 7920kb | C++20 | 1.2kb | 2024-03-31 21:47:14 | 2024-03-31 21:47:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 20) + 10;
int dp[N];
int n, k;
vector<int> v;
vector<int> frq[21];
int rec(int msk) {
if (msk == 0) {
return 0;
}
auto &ans = dp[msk];
if (~ans)
return ans;
ans = 1e9;
vector<int> cnt(k), last(k, -2);
int last0 = -1;
for (int i = 0; i < n; i++) {
if (((1 << v[i]) & msk) == 0)
last0 = i;
else {
cnt[v[i]] += (last0 > last[v[i]]);
last[v[i]] = i;
}
}
for (int i = 0; i < k; i++) {
if (!(msk & (1 << i)))
continue;
ans = min(ans, rec(msk ^ (1 << i)) + cnt[i]);
}
return ans;
}
void tc() {
memset(dp, -1, sizeof dp);
cin >> n >> k;
map<char, int> mp;
string s;
cin >> s;
for (auto &i: s) {
if (mp.find(i) == mp.end())
mp[i] = mp.size();
v.push_back(mp[i]);
}
cout << rec((1 << k) - 1) << '\n';
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
tc();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 7684kb
input:
4 2 abba
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 0ms
memory: 7700kb
input:
4 2 rtrt
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 2ms
memory: 7684kb
input:
10 3 abcbacabac
output:
6
result:
ok single line: '6'
Test #4:
score: 0
Accepted
time: 0ms
memory: 7680kb
input:
9 3 abcbcacab
output:
6
result:
ok single line: '6'
Test #5:
score: 0
Accepted
time: 2ms
memory: 7632kb
input:
9 3 dbccdbbcd
output:
5
result:
ok single line: '5'
Test #6:
score: 0
Accepted
time: 0ms
memory: 7632kb
input:
10 3 acbcabacab
output:
6
result:
ok single line: '6'
Test #7:
score: 0
Accepted
time: 1220ms
memory: 7680kb
input:
400 20 efgfmadpiijgaamlemgqhdnpihlkagkmjkigtmbhafjnedhebthrmlklmbojbkosqhtrslisecfisltebghpnctnjfdbecanrrtileddokkhoocgkaqnctoogdsrqtafehonofcbktacaaercsslcgaroeatplntboornbgkrnchlrgplrbnjnfjqmtcbekcqtlepcqfiaqqffftkpgsdlhahoddrppibscjhtnfncpglhknnicsqqrkmjnfpomeikpckgqlpidftitjpmjqgeamsgjpfsssqgmpq...
output:
308
result:
ok single line: '308'
Test #8:
score: 0
Accepted
time: 1298ms
memory: 7632kb
input:
400 20 otoatqdklbjrgeogbqknqtdrjimhjnodcgmkbjgobqaiisstokecnqrkrettplnnhfegldifqtabgbfsttahakkflhikrebpqhlnjnnmacnambslrmdkirdhcjfagsomidrhjrejaegketercespnssprcpthqldcpqgcilftparljlngmrdmatoshbfhjjsadebljklfmfjqcqkkltqossooiaqehgtosmqaspdkkhfikdhebmprtjhpqcfmpbdndbmkjgrdforerrannibgacqfkigpmthcdjpi...
output:
312
result:
ok single line: '312'
Test #9:
score: 0
Accepted
time: 1297ms
memory: 7732kb
input:
400 20 grfstbcpbeqqnplbeinsleiffhlmaeqmjfnemclngttnsndfollqrfsjcaajesbjspcegjthpolkiptqhcrposnsdklfsksmarqhdncjdlkhrrjrtipbetaorpmcdgkmfcaqnngjapdjsclbmpjcibqnookfaqicrmgsnnoanspdsmgtarcqoehindmmcehaijjgpbaflbkbkoltpbaqaifhjdtmidtakaqilhbjjnnfqnhhqjcqtiohqagjrhdekesftcetdkgmgklidgoemlmiefrfebfokdcrk...
output:
314
result:
ok single line: '314'
Test #10:
score: 0
Accepted
time: 1318ms
memory: 7712kb
input:
400 20 jbjdcsnehpthnmqtbkjsaspnemkohfkgfskdrqqiapgsijcsdadksplhaelanirjodskjpntlconcjihgjegqelmhralrstfskmrsgpompddeonjqekfchmshclpfhssehfnkrmedfrmncsppaeimtcciggprcqchqdfciggotjbpintihbbtaksnnocsklfbmgiccbgopkpdptaerqtjrhenocaqdprgjgogtqlrdimollehipmsbqabiqjfnibgtjltqfhemmlfjkoslhikjsncefmthcdbhogt...
output:
310
result:
ok single line: '310'
Test #11:
score: 0
Accepted
time: 1266ms
memory: 7644kb
input:
400 20 sbballopdokiilpcolsbkmqogsmsnlpnehlscmbnekseniggjgqqhfnlcngakiptpmflrjrbdihltrktrtbbjeaptkphmasfcficoinrracjfqaalrtochjdaeaejgolqrdlhenaidjekjdooosgsqhgjptdhediondbphrptlcrffmepqbjohgitsptemqrsqcmcgpffshfkqammhrckgibnffisfjkepjrbseetddlhgpfqdchdmtcirhfcbdbhkfjosofmiaknjthborrknlokptlbetqknkqg...
output:
309
result:
ok single line: '309'
Test #12:
score: 0
Accepted
time: 1238ms
memory: 7716kb
input:
400 20 dsfqhngaajtlpdbhfbmfqemchqrbpjokodgjdjtsotgecrenmtcnbjbbpgonbrgjionqltdoshcdsiktbkialkisptktnpkslihfaarskrlnfittbdgaifqjgqraqfaclnofthbsafpadsesmjbcrheonkkfnjogdoieppifdpgksmagpinqjlkprqmhmisfmremefhfshiccmeeqlskmebnjkfdhpgclhdeeltnnalbqshhieeipnkpoimbgejeboahmadqqbtldcnfqfcmkpcrilmistatabjgr...
output:
308
result:
ok single line: '308'
Test #13:
score: 0
Accepted
time: 1315ms
memory: 7640kb
input:
400 20 bdagjhnmhjekhsmacdapjkpirienarbljtnfprnegtsspoipqdanbslepjlqhihsefotgojsqckbmbhmdfgfslrolaldjjhgnmkoomehncriifmgebtpopkqcpddnhtscrjkarnrbftgfctacmbjgtsdigsrinlfagkphgmtfthncgqfnbeeidpqnlssoksocrqnacscklqdmiobrjimhsbhaancnmlfiepaiokdrkaekqfgfstbphdcigktklmcmrqppjsqfolcjcjlnrndntpijjfkdqilqtokb...
output:
313
result:
ok single line: '313'
Test #14:
score: 0
Accepted
time: 1278ms
memory: 7884kb
input:
400 20 ddsjfdtthnqeenlmjdoremmhqplolgrpklnrpjtbqpprlffiqphgkfddigkokaorqnogclgbdpnqsbtngjohhqrndbolktgfapifdtakothcqgmlkboqienisgbcddbkannnamredpcgmopjekkpiisohdrciidcfbmqpbqibfsbrfoskcthjhastepokcrcmhqtarsafbgraaeiseljlhlmgsriqemrdmeosmjnppminglsqckllthjribhfreanfsfsfkqmjbkcbkdtemprpksaligiderjcamt...
output:
308
result:
ok single line: '308'
Test #15:
score: 0
Accepted
time: 1255ms
memory: 7920kb
input:
400 20 slamsdgqsgigjadjdhmpmltpqpppjacjedconpkcskmopkamsfmshkicrefgotmnrhkofnleptkbtdchgdfciancdtfllafihkiqpbkkfgnpaslefmnioegqpmphphojkaciscaebnnaejirkdotbhknqtbmethnrbjmiggqjfopbnomdelpgpjtqdkbrimoglrbmrqdtenitfkhnakohnkhmseccooflrehrlcbsblnqfqftaispdshlqihrfmacgjeojdrbcaibesbopnfstdfeoabktghloqsm...
output:
314
result:
ok single line: '314'
Test #16:
score: 0
Accepted
time: 1283ms
memory: 7640kb
input:
400 20 fllnojkpaneaebnikktlmtdpcntsioegtrndbmqjctorbassdrlcfqrqhiisjndgcqghkffdmrrskgmknsgkotqlbmajpajassjriaojdcphookphkpnghankrsggdcfiapcbkjhmofsofqsoborncdcaiepgsbtoiclaljceiqelbclbqgdelfotkhltsdghtrhctoppeidhhabkrmplpninmisrrqergbkpmrbabokgmgbtpmltetoejkftfrcitfefshmtodpemdfkjfglsperljofegdiqqhj...
output:
308
result:
ok single line: '308'