QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#132506 | #6562. First Last | Sorting# | AC ✓ | 1ms | 3688kb | C++20 | 4.7kb | 2023-07-30 02:48:52 | 2023-07-30 02:48:55 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector<int> vi;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
map < char , int > w ;
int cnt[ 3 ][ 3 ] , tot[ 3 ][ 3 ] ;
int n ;
string a[ 1007 ] ;
int tp ;
int dp[ 12 ][ 12 ] ;
int brute ( int x , int rem , int mask , int dir ) {
if ( dp[ rem ][ x ] != -1 ) { return dp[ rem ][ x ] ; }
if ( rem > 0 ) {
if ( brute ( ( x + dir + 3 ) % 3 , rem - 1 , mask , dir ) == 0 ) {
dp[ rem ][ x ] = 1 ;
return 1 ;
}
}
if ( cnt[ x ][ x ] > 0 && ( mask & ( 1 << x ) ) == 0 ) {
if ( brute ( x , rem , mask + ( 1 << x ) , dir ) == 0 ) {
dp[ rem ][ x ] = 1 ;
return 1 ;
}
}
dp[ rem ][ x ] = 0 ;
return 0 ;
}
int cyc ( int dir , int ori ) {
int snake = 0 ;
int mn = 1007 ;
for ( int i = 0 ; i < tp ; ++ i ) {
cnt[ i ][ i ] %= 2 ;
}
if ( dir == 1 ) {
mn = min ( { cnt[ 0 ][ 1 ] , cnt[ 1 ][ 2 ] , cnt[ 2 ][ 0 ] } ) ;
snake = 3 * mn ;
cnt[ 0 ][ 1 ] -= mn , cnt[ 1 ][ 2 ] -= mn , cnt[ 2 ][ 0 ] -= mn ;
if ( cnt[ ori ][ ( ori + 1 ) % 3 ] > 0 ) {
++ snake ;
if ( cnt[ ( ori + 1 ) % 3 ][ ( ori + 2 ) % 3 ] > 0 ) { ++ snake ; }
}
}
else {
mn = min ( { cnt[ 1 ][ 0 ] , cnt[ 2 ][ 1 ] , cnt[ 0 ][ 2 ] } ) ;
snake = 3 * mn ;
cnt[ 1 ][ 0 ] -= mn , cnt[ 2 ][ 1 ] -= mn , cnt[ 0 ][ 2 ] -= mn ;
if ( cnt[ ori ][ ( ori - 1 + 3 ) % 3 ] > 0 ) {
++ snake ;
if ( cnt[ ( ori - 1 + 3 ) % 3 ][ ( ori - 2 + 3 ) % 3 ] > 0 ) {
++ snake ;
}
}
}
int path_len = snake ;
snake += cnt[ 0 ][ 0 ] + cnt[ 1 ][ 1 ] + cnt[ 2 ][ 2 ] ;
if ( snake >= 0 ) { return ( ( snake % 2 ) == 1 ) ; }
for ( int i = 0 ; i <= path_len + 1 ; ++ i ) {
for ( int j = 0 ; j < 8 ; ++ j ) {
dp[ i ][ j ] = -1 ;
}
}
return brute ( ori , 0 , path_len , dir ) ;
}
int dfs ( int x , int id ) {
if ( dp[ x ][ id ] != -1 ) { return dp[ x ][ id ] ; }
for ( int i = 0 ; i < tp ; ++ i ) {
if ( x == i ) { continue ; }
if ( cnt[ x ][ i ] == 0 ) { continue ; }
if ( dfs ( i , ( cnt[ i ][ i ] > 0 ) ) == 0 ) {
dp[ x ][ id ] = 1 ;
return 1 ;
}
}
if ( id > 0 ) {
if ( dfs ( x , 0 ) == 0 ) {
dp[ x ][ id ] = 1 ;
return 1 ;
}
}
dp[ x ][ id ] = 0 ;
return 0 ;
}
int dag ( int ori ) {
for ( int i = 0 ; i < 4 ; ++ i ) {
dp[ i ][ 0 ] = dp[ i ][ 1 ] = -1 ;
}
for ( int i = 0 ; i < tp ; ++ i ) {
cnt[ i ][ i ] %= 2 ;
}
return dfs ( ori , ( cnt[ ori ][ ori ] > 0 ) ) ;
}
void solve ( ) {
cin >> n ;
tp = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
int sz = a[ i ].size ( ) ;
if ( w.find ( a[ i ][ 0 ] ) == w.end ( ) ) { w[ a[ i ][ 0 ] ] = tp ++ ; }
if ( w.find ( a[ i ][ sz - 1 ] ) == w.end ( ) ) { w[ a[ i ][ sz - 1 ] ] = tp ++ ; }
}
for ( int i = 1 ; i <= n ; ++ i ) {
int sz = a[ i ].size ( ) ;
++ tot[ w[ a[ i ][ 0 ] ] ][ w[ a[ i ][ sz - 1 ] ] ] ;
}
int ans = 0 ;
for ( int ii = 0 ; ii < tp ; ++ ii ) {
for ( int jj = 0 ; jj < tp ; ++ jj ) {
for ( int t = 0 ; t < tp ; ++ t ) {
for ( int z = 0 ; z < tp ; ++ z ) {
cnt[ t ][ z ] = tot[ t ][ z ] ;
if ( ii == t && jj == z ) { -- cnt[ t ][ z ] ; }
}
}
if ( cnt[ ii ][ jj ] < 0 ) { continue ; }
for ( int i = 0 ; i < tp ; ++ i ) {
for ( int j = i + 1 ; j < tp ; ++ j ) {
int aux = min ( cnt[ i ][ j ] , cnt[ j ][ i ] ) ;
cnt[ i ][ j ] -= aux , cnt[ j ][ i ] -= aux ;
}
}
if ( cnt[ 0 ][ 1 ] > 0 && cnt[ 1 ][ 2 ] > 0 && cnt[ 2 ][ 0 ] > 0 ) {
ans += cyc ( 1 , jj ) * tot[ ii ][ jj ] ;
}
else if ( cnt[ 1 ][ 0 ] > 0 && cnt[ 2 ][ 1 ] > 0 && cnt[ 0 ][ 2 ] > 0 ) {
ans += cyc ( -1 , jj ) * tot[ ii ][ jj ] ;
}
else {
ans += dag ( jj ) * tot[ ii ][ jj ] ;
}
}
}
cout << n - ans << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3688kb
input:
3 attic climb alpha
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
22 agora alpha antic aorta apnea arena aroma attic basic blurb china circa civic climb cobra cocoa comic comma conic crumb cubic cynic
output:
6
result:
ok single line: '6'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
3 ccabaabbba acbbbacccb ccccccacba
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3600kb
input:
11 mraa myga vtwm mala vvgm atvv vusm mznv avea atfv amgv
output:
7
result:
ok single line: '7'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
1000 vznwivhoprv pvzcjyruxv phykv vozinczup vbp vgjxfotuhzhobfp pbv vygphslv vpnqrfep vzrphpfggoqcgv vgdjmzuckyedpnp vatmaxfp ppnmmwtqmv paawwp pspoycv vcjthv ppcvagp pteiqdonkklp vav vcsxv priqwbalidav vinp phqeijnqkgbxv pwvmbivmgvouv vwzhndzmqpcwkmp pyzlv vtvblfov vrdv vzrfmjdnaruwp pfup pwzv vwyv...
output:
478
result:
ok single line: '478'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
1000 rlp pgscawiivpjndlp pxr ruzutr pqdhfniemqvvr rvar rp prarwp pkuinxempbmear rupxuanwlvawmr pp rzfvr rcsxoyirp pgpilljr pogeizvqlr rajolbgzswur rcap ryzqymwr rrpp rbxtr rfldblbesyhqr rjidcekyrir pqrycftr rftwgmmakykkmep pehkxkayuip pguxsgtuikvp rtxmynvoolrp pzmsljlpcr phcfdr plaop rydojaccp pzfr ...
output:
246
result:
ok single line: '246'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3688kb
input:
1000 ffn fgikjn fwtakdbjn fsjnfzzn feqmuasubirsf faypin fcsdhfjvncfzsn nbcmvxemkan ngoqtaaon nlovkxxrbn fhdqkltoxvif fxpnqbguftsrhff fyniukvsmlf fibwbqckjpojglf nwcvf fxgeivnakggcn nn fkghlusecf neuwdafrkzptwf fcxn ff nlkxtn fdlsijtpbcjjcbn frlcjjnpsn ntyabhf nvf fdlmlhebidazhn fjn fstxgnmfof fqhusf...
output:
496
result:
ok single line: '496'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3632kb
input:
15 adb bdc cda aeb bec cea afb bfc cfa agb bgc cga ahb bhc cha
output:
15
result:
ok single line: '15'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
1 alpha
output:
1
result:
ok single line: '1'
Test #10:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
3 alpha beta gamma
output:
1
result:
ok single line: '1'
Test #11:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
2 alpha beta
output:
1
result:
ok single line: '1'
Test #12:
score: 0
Accepted
time: 1ms
memory: 3632kb
input:
20 ccbccaabaa aaccbbbbab bcbcbccabc bbbabcbbcc ccaaccabca acabbbccab baacbcbccc bcbbabbbbc bccbbbbabc bacaccbcac cbbabbbaca ccaaacbaaa acbbbaccab bcaacaaacc aabbaabcbb bbbabbbbcc aabcaaabcb aabbbaaacb acacaababb abacccccbb
output:
8
result:
ok single line: '8'
Test #13:
score: 0
Accepted
time: 1ms
memory: 3684kb
input:
20 acccccbcbb bccbccaaac aabbcbbccb ababcabccb abcaabbcab ccabbabcca bbccaaacbc bbabacbccc babacbaacc bcabaacbac acbabaabbb caaccbacaa bccccbcbcc abbbbbbcbb bbcbaabaac abccaabccb cacacaccca bcbbcbcacc bcccaabcbc cabaaaccca
output:
9
result:
ok single line: '9'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3524kb
input:
20 bcbbbababc baccababbc bcccbaacbc ccccacbaca aaaaccabbb cbbbacbaaa aaabbaabcb aacccbbacb bcbccbcacc caabacbcba bcbcbbcbbc cacccabcaa abbababacb aacaabbccb abbcacaccb caaccaacca bcbacacaac baccbcccbc abbcaccabb acbacccccb
output:
13
result:
ok single line: '13'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
20 babccbbbbc accacaabcb acacabaabb bacbccacac cacacaacaa cacaaaaaca bccbccacac acbaccabbb ccbacaccba acbaaababb ccacabcaaa bbabbcabcc bacbccccac bcacaacacc ccabcbbbaa aaababbbab bbbbbabbac abbcaabbbb bababbabac aaabcbbacb
output:
12
result:
ok single line: '12'
Test #16:
score: 0
Accepted
time: 1ms
memory: 3600kb
input:
20 aacbaacccb cbcbcacaaa cacbacbaca aabacccacb bbbcbacbcc cababcacaa bbcaaacbbc ccccacaaca cabcbcccaa bbcbccabbc ccababccca aaacbaabbb baacabacbc accbbcbbab cccbccabba cbccaaaaca cbcabaccca caaacbccca acaaabccab bbbaaabbac
output:
10
result:
ok single line: '10'
Test #17:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
20 aaacaacbcb cccccccaca abbabbcacb accaacbabb bacbaccbac cbcbbcccba bacbcabcac abaabcabab acaaacaaab bacbbaccac baababbbbc caaaaabaca abacaccacb abbbcbccbb cbcbcbbcaa cabbcaabba cbccbaccaa ccbbcabbaa ababababcb abcbcbcabb
output:
9
result:
ok single line: '9'
Test #18:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
20 bcaccbabbc caaaaccaca acbaacabab aaabbabbcb ccccaccaca bcacbaacbc cbccacabca ccbabbaaaa ababcbaaab caabacabca bbbcaabcac cccccababa aabbabcccb abaaaccbbb bcabaabbcc bcbbbaacac acaacbbccb bacabbabcc bbcbccbbac aacbabccbb
output:
7
result:
ok single line: '7'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3684kb
input:
20 cbacccbaba caccbccaba abcaccacab cbcccabaca acccbccacb bccacbbabc bbacabacac abbcccbbab acaaaabbcb cbbbaabaca babbbccacc bbabccbcbc cabaababaa caacbabcba aaaaaacccb ccaaacacaa baccaabcac aaaacbcbab baacaaaabc ccabcacaba
output:
8
result:
ok single line: '8'
Test #20:
score: 0
Accepted
time: 1ms
memory: 3576kb
input:
20 bbaccaccbc cabcccabba bcabbbbaac caaacccaca cabccbabaa abccaaacab ababbccabb acccaaabab bababbcacc acabbacacb accccbcaab cabaaccbca cccacbbcca acabbaccab baacccacbc bacacabbcc aabaaaacab cbbabcccba aaaacbaccb cccbaaaaca
output:
12
result:
ok single line: '12'
Test #21:
score: 0
Accepted
time: 1ms
memory: 3536kb
input:
20 aabacacacb abaccaccbb acaccccbcb cbacbcccca cccccabbaa bbbcbabccc cabbccccba babcbcabac baccbaacbc ccbbccbbba aacaabbbab bccaccacac abbcacaccb cbcaabacba babbcbaacc ccabbbbbaa cbcbbbaaba bcaabcbaac bcbbbcbbbc babccbbbac
output:
13
result:
ok single line: '13'
Test #22:
score: 0
Accepted
time: 1ms
memory: 3536kb
input:
17 hydrogen nitrogen helium neon magnesium niobium molybdenum newdymnium holmium hafnium neptunium mendelevium nobelium hassium meitnerium nihonium moscovium
output:
6
result:
ok single line: '6'
Test #23:
score: 0
Accepted
time: 1ms
memory: 3524kb
input:
10 cbabbbcabb bacbacbcba bcacacacba bbabbbbbac acaccccbac bbbbababca bacabccaac aacbcaabcb bacbabaaaa acabcabccb
output:
3
result:
ok single line: '3'
Test #24:
score: 0
Accepted
time: 1ms
memory: 3536kb
input:
100 ccbcaccbab bbcbcbbbca acbbbbbcbb bacabcbcba aaaabcbaac babaaabbba bbbabbbabc bccaaacbac acacaccbaa acbacbcaac baccabcccb bbabcccbca acbabaaaac bcbbcabacc ccbbacbcbc aaaabcbcaa aacbbbaaaa bbabbcacac baabacbbcc abaccbabab abcbbabacb cbbbaccbcb bbabbcabca bcbaacacaa aabccaacca bcabaaacca acbcbacbbc...
output:
32
result:
ok single line: '32'
Test #25:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
1000 pfalakkgegzhogw wkkuxghywruwxfe wyejymxprfudmww pzfhykyprexdzjp euglzawfswzsicw wukfsntcitmqqsp ptedktsjhdybegp woiewjinnixhftw pwysrvatyqcgxcp pyslupqwmpqluzw ezwtyuflrtgjdle pyecuqzysscnqvp pualunhhvihcohe eemgrxaolvkxmup egvaujnrfthojvw ekfhcvufcqcrhcw ecqzrgboomfjcve whxlumclfpgcanw pfoxyrw...
output:
225
result:
ok single line: '225'
Test #26:
score: 0
Accepted
time: 1ms
memory: 3516kb
input:
30 rfiuianksyzpkqz zkntbrwccnjcnzh hvyhatniqoeptvh zzwxgtvocagvdlz hkvtotqsilzpexr rmqjlxhiyuvtwrh ziynrswzgzpnfnz rntoducfmkqpaoz rgzweicxeqajpzr zjznfxgpfgshkbr rmtmhwrzkenopsz hyodaehasivoabz hkgxlvuxonzlvgr hcjelcmekbohgxr zkdlkppyvchvimh rehlcscmhbnjexr rhhhexryikreroz htuwcazhktbrvqr hvnicynkr...
output:
11
result:
ok single line: '11'
Test #27:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
300 bzpjihkgvkrwzav bssiintigbaccyv asuzprjzymuwkfv blajmgohbmihtwb bwocwcqekqwuwab azkbjbrjdnwgzhb aqdgacdlcofzjzb vvqgvpdtsphhqbb vafommzgswakrub bizlmhzwvokxezb btwrjkdkxiaciaa bgyfvakbcgcaima vtkthaktjltsvtv vmyxovrkpphmnna bgwhfiinpdjigza vaghbpbabwfsnub atztqndgqetgbzv agtepkzcnnrlfzb ayktyzbv...
output:
196
result:
ok single line: '196'
Test #28:
score: 0
Accepted
time: 1ms
memory: 3596kb
input:
1000 drucrd dwbdnwzffwd dkcoffhuvxgzd dtd dbud dhdjamctkfkufpd dpolagked dqpyleydrcbjd diunguld dbpxmzd dd dllmgjsd dnncoucapnojd dbydsqd did dgjd dmd dkzkjmdkd derbbcjvtgxbgd dbpznlrtud dxrdhad dqhyxdd dgqd dawzjid domzfuycd dlyed dkunoduhamd dlpwoaqgjuwazd dcd dxnvgrvxnd drxptrhxd duqwvybjgahd djh...
output:
0
result:
ok single line: '0'
Test #29:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
18 aab bac caa abb bbc cba acb bcc cca adb bdc cda aeb bec cea afb bfc cfa
output:
0
result:
ok single line: '0'
Test #30:
score: 0
Accepted
time: 1ms
memory: 3524kb
input:
1000 qqaligwjiggjcq iagxuiizhglmi xuikcxsrirevfi qgcyddrpx iriauckx xaq iczi xqhfnkhgx ihjmwpzmaplimcq qxhadplhx qylxcygq xi xansoq injixpfhfasvbq qiwgbci xjgmxhtxvdq iihmigcihi iedrdwzgq iyi xhqlttzyni qcx irbnlx qmcxdihujwlai iqaq qyhquti qcqzoifzglwxzdq ikjajeafi ifgydi xembxgcluzqq qyesjwiix qkx...
output:
242
result:
ok single line: '242'
Test #31:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
100 yhothsuntodvjbp yovbflgbufdystp plxhiphtvtvccxn yteszfwnocvquep yimksvuvxrmquyn yzxolkjfnwdwcop yvnzximnjfteqap padrxvamipryowy pxgkjkpddfbrarn ykqtkdxxwzknrvp pxewmovejzktyzn pnnjanibbinhwzn ylzdwvrozhyyyen yfyfqvvuycfaxpp nechefkhfvumkry yvtzpokomrtusyn pwpbgmtkpcnhmtn niyymfgohdgdhuy yedcxgnr...
output:
67
result:
ok single line: '67'
Test #32:
score: 0
Accepted
time: 1ms
memory: 3524kb
input:
1000 yaswiedrkfymnzk kzcaleacmpuazhv kfefxdppxusoqxv vifosydgwnqpssk ymtrrddmufmncfv vrupdvcsmepqtwk vjuvfllbidvbcqy veldskdioyyzock kdmjepctwkiixrv ypicbokwooycocv ybbsmccvqynbvwk yzgsbzbhteyajfk yyynqadclcjkcvv kmwrrremazrcdyv yfuauavqsoxnknv yizrfjukexccyvk yuodugxjnuymgwv vvdbmcmdcejdjhk vsesegg...
output:
360
result:
ok single line: '360'
Test #33:
score: 0
Accepted
time: 1ms
memory: 3688kb
input:
30 nduqkltyrjchrir npqnyjhvtczrxhk rvkhqwigvnjasln nqxnnyxpmhbzhrr kcrjplhrdkyyvpn nuxuodpvfvyovsk nzcbwoxvmuajnuk rcrqzqtykuzgiek rvzjffzzwapzvck rgntauijiodjcwn nqntaykdmrpiojr kmbynsfeerniqpr rszawhpanrairqk nhjsfdswnnaqumr kijysjjlrbwigin njebshbbcadcbwk ncwluvybyxhbuvr rlxuzfjuxgziehk rcbtttxlq...
output:
11
result:
ok single line: '11'
Test #34:
score: 0
Accepted
time: 1ms
memory: 3684kb
input:
300 ldqparrlixcrfhn lslzwfkysvvvaqt lklydecymfdsymn nxxolcshfdiofxt lkwqujbckqmigin nfqvomhsapqexsl njnqzhjetlnecwt twgomlfjreikycl lujnrxtaognjjbn lciyasqkkkcgrln ncwhdddgamnbgft naprkvnqkxqjdjl ttltucizicrybgl trdnzylpxeujmql nmyusbfdjyaoaft lsdwoewrtnrxhkn nfapnyvwftbnwyl lvahwwkijbffrxn tzmuasjd...
output:
107
result:
ok single line: '107'