-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathsiruri.cpp
69 lines (68 loc) · 1.39 KB
/
siruri.cpp
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
# include <fstream>
# include <cstring>
# define LB(p) ((-p) & p)
# define NR 1000005
# define mod 41357
# define alpha 26
using namespace std;
ifstream f("siruri.in");
ofstream g("siruri.out");
int i,j,n,m,tip,S,x,ci,cs,poz;
int AIB[30], B[200005];
char s[NR];
int suma (int poz)
{
int S=0;
for (int i=poz; i>=1; i-=LB(i))
S+=AIB[i];
return (S%mod);
}
void actualizare (int poz, int S)
{
for (int i=poz; i<=alpha; i+=LB(i))
AIB[i]=(AIB[i]+S)%mod;
}
int cb (int x)
{
int st=ci, dr=cs, mij;
while (st<=dr)
{
mij=(st+dr)/2;
if (x<B[mij]) dr=mij-1;
else st=mij+1;
}
return dr;
}
int main ()
{
f>>tip>>n; f.get();
f.getline(s+1, NR);
if (tip==1)// nr de subsiruri strict crescatoare
{
for (i=1; i<=n; ++i)
{
x=s[i]-'a'+1;
S=suma(x); //nr de subsuri anterioare
actualizare (x, S+1);
}
S=suma (alpha);
g<<S<<"\n";
}
else //tip==2 // partitionarea in subsiruri
{
ci=200000; cs=200000;
B[cs]=s[1]-'a'+1;
for (i=2; i<=n; ++i)
{
x=s[i]-'a'+1;
if (x<B[ci]) B[--ci]=x;
else {
poz=cb(x);
B[poz]=x;
}
if (B[cs]==alpha && B[cs-1]==alpha) --cs;
}
g<<200000-ci+1<<"\n";
}
return 0;
}