/* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; char s[110000]; LL n,k; LL N[110000]; // N[i] = nawiasowosc od indeksu i do jakiegos akurat wybranego i2 LL C[110000]; LL stac[110000]; LL stackCount; void stackInit() { stackCount=0; } inline void push(LL num) { stackCount++; stac[stackCount]=num; } inline LL pop() { LL res = stac[stackCount]; stackCount--; return res; } void calculateN(LL i2) { stackInit(); FOR(i,1,i2+1) C[i]=0; LL total=0; LL dobreNaPrawo=0; LL j; FORD(i,i2,1) { if (s[i]==')') { push(i); //cout << "push: " << i << "\n"; } else { if (stackCount>0) { j=pop(); // mamy poprawne wyrazenie nawiasowe [i..j] dobreNaPrawo=C[j+1]; C[i]=1+dobreNaPrawo; //cout << "poprawne: " << i << ".." << j << " C[" << i << "]=" << C[i] << "\n"; total+=C[i]; } } N[i]=total; } } void makeRandom() { FOR(i,1,n) if (rand()%2==1) s[i]='('; else s[i]=')'; } LL* X[101000]; // X[i][q] - podzial [1..i] na q przedzialow const LL infi1=110000; const LL infi=infi1*infi1; LL calculateIt() { FOR(i,0,n) X[i]=new LL[k+1]; LL naw, nawMin; FOR(i,1,n)FOR(q,1,k) X[i][q]=infi; X[1][1]=0; FOR(i,2,n) { calculateN(i); X[i][1]=N[1]; // jeden przedzial //cout << "X[" << i << "][1]=" << X[i][1] << "\n"; FOR(q,2,k) { //cout << " licze dla q=" << q << "\n"; if (q>i) break; // nie liczymy podzialu na wiecej przedzialow niz znakow // liczymy X[i][q] // probujemy ostatni przedzial na wszystkie mozliwe sposoby nawMin=infi; // probujemy dzielic na q przedzialow: FORD(i1,i,q) { //cout << " probuje podzial na: 1.." << i1-1 << " " << i1 << ".." << i << " naw=" << N[i1]+X[i1-1][q-1] << "\n"; naw=N[i1]+X[i1-1][q-1]; if (naw<nawMin) nawMin=naw; } X[i][q]=nawMin; //cout << "X[" << i << "][" << q << "]=" << nawMin << "\n"; } } return X[n][k]; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); string s0; cin >> n; cin >> k; cin >> s0; FOR(i,1,n) s[i]=s0[i-1]; if (n*k>60000000) cout << "0\n"; else cout << calculateIt() << "\n"; return 0; }
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 143 144 145 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; char s[110000]; LL n,k; LL N[110000]; // N[i] = nawiasowosc od indeksu i do jakiegos akurat wybranego i2 LL C[110000]; LL stac[110000]; LL stackCount; void stackInit() { stackCount=0; } inline void push(LL num) { stackCount++; stac[stackCount]=num; } inline LL pop() { LL res = stac[stackCount]; stackCount--; return res; } void calculateN(LL i2) { stackInit(); FOR(i,1,i2+1) C[i]=0; LL total=0; LL dobreNaPrawo=0; LL j; FORD(i,i2,1) { if (s[i]==')') { push(i); //cout << "push: " << i << "\n"; } else { if (stackCount>0) { j=pop(); // mamy poprawne wyrazenie nawiasowe [i..j] dobreNaPrawo=C[j+1]; C[i]=1+dobreNaPrawo; //cout << "poprawne: " << i << ".." << j << " C[" << i << "]=" << C[i] << "\n"; total+=C[i]; } } N[i]=total; } } void makeRandom() { FOR(i,1,n) if (rand()%2==1) s[i]='('; else s[i]=')'; } LL* X[101000]; // X[i][q] - podzial [1..i] na q przedzialow const LL infi1=110000; const LL infi=infi1*infi1; LL calculateIt() { FOR(i,0,n) X[i]=new LL[k+1]; LL naw, nawMin; FOR(i,1,n)FOR(q,1,k) X[i][q]=infi; X[1][1]=0; FOR(i,2,n) { calculateN(i); X[i][1]=N[1]; // jeden przedzial //cout << "X[" << i << "][1]=" << X[i][1] << "\n"; FOR(q,2,k) { //cout << " licze dla q=" << q << "\n"; if (q>i) break; // nie liczymy podzialu na wiecej przedzialow niz znakow // liczymy X[i][q] // probujemy ostatni przedzial na wszystkie mozliwe sposoby nawMin=infi; // probujemy dzielic na q przedzialow: FORD(i1,i,q) { //cout << " probuje podzial na: 1.." << i1-1 << " " << i1 << ".." << i << " naw=" << N[i1]+X[i1-1][q-1] << "\n"; naw=N[i1]+X[i1-1][q-1]; if (naw<nawMin) nawMin=naw; } X[i][q]=nawMin; //cout << "X[" << i << "][" << q << "]=" << nawMin << "\n"; } } return X[n][k]; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); string s0; cin >> n; cin >> k; cin >> s0; FOR(i,1,n) s[i]=s0[i-1]; if (n*k>60000000) cout << "0\n"; else cout << calculateIt() << "\n"; return 0; } |