Probleme Oni 7


https://www.pbinfo.ro/probleme/4398/palindrom4

#include <bits/stdc++.h>
using namespace std;

ifstream fin("palindrom.in");
ofstream fout("palindrom.out");

bool palindrom(string s)
{
    int n=s.size();
    for(int i=0;i<n;i++)
    {
        if(s[i]!=s[n-i-1]) return 0;
    }
    return 1;
}

int verifc(string s)
{
    int cnt=0;
    string test;
    if(palindrom(s)==0)
    {
        do
        {
            cnt++;
            string c;
            int n=s.size();
            int nr=cnt-1;
            for(int i=1; i<=cnt; i++)
            {
                c.push_back(s[nr]);
                nr--;
            }
            test=s+c;
        } while(palindrom(test)==0);
    }
    return cnt;
}

int main()
{
    int c,cnt=0;
    fin>>c;
    if(c==1)
    {
        int n;
        fin>>n;
        for(int i=1;i<=n;i++)
        {
            string s;
            fin>>s;
            cnt+=verifc(s);
        }
        fout<<cnt;
    }
    return 0;
}



cin >> n;


bool palindrom(char s[102], int mij) {
	n = strlen(s);
	for(int i =mij; i <n;++i)
		if(s[i] != s[n-i-1])
			return false;
	return true;
}

int solve(char s[51]) {
	
	n = strlen(s);
if(palindrom(s))
	return 0;

char t[102];
for(int i = 0; i <n; ++i)
	t[i] = s[i];
int nr = 0;

abcc => abccba 
abded => abdedba

c1c2c3…Cx|palindrom
abc => abcba
abcaa
abcded
abcxdedx


—-------

Sa se determine lungimea celui mai lung palindrom din sir?


…..

pare => abba 4 => bb => 0
impare => abcba 5 => bcb => c

fgdededgf => 9 => gdededg => deded  => ede => d

xxxxxxxfgdededgf
 
3 5 7 9 

int st = 1, dr = n;




	abded 
while(st <= dr) {
	int mij = (st+dr)/2;
	if(palindrom(s,n-mij+1)) {
		sol = mij;
		dr = mij-1;
		
}
else 	{
	st = mij+1;
}
}

sol = 3

cout << n-sol;



while(st <= dr) {
	int mij = (st+dr)/2;
	if(mij % 2 == 0)
		mij = mij-1;
	if(palindrom(s,mij)) {
		sol = mij;
		dr = mij-1;
		
}
else 	{
	st = mij+1;
}
}


while(st <= dr) {
	int mij = (st+dr)/2;
	if(mij % 2 == 1)
		mij = mij-1;
	if(palindrom(s,mij)) {
		sol = mij;
		dr = mij-1;
		
}
else 	{
	st = mij+1;
}
}


st = 1 dr = 7

mij = 

1 3 5 7 

c1c2palindrom

	for(int i = 0; i < n; ++i) {
	for(j = i; j>= 0; —j,nr++)
		t[n + nr] = s[j];
if(palindrom(t))
return i+1;	
}

}
s s[1] s[2] 

char s[51];
for(int i = 1; i <= n; ++i) {
	cin >> s;
	total += solve(s);
}

O(50000*50^2) = O(50*1000*50^2) = 1000 * 125.000

bool palindrom(string s,int st, int dr)
{
   for(int i=0;i<dr-st+1;i++)
   {
      if(s[st+i]!=s[dr-i-1]) return false;
   }
    return true;
}

int sol(string s)
{
   int st=1,dr=s.size();
   while(st<=dr)
   {
	int lung_mij=(st+dr)/2;
	if(lung_mij % 2 == 0)
		lung_mij—;
	if(palindrom(s,n-lung_mij+1,n)==true)
	{	
    sol = max(sol,lung_mij); 
    st = mij+1
   	 }
	else dr = mij-1;

   }
int st=1,dr=s.size();

  while(st<=dr)
   {
	int lung_mij=(st+dr)/2;
	if(lung_mij % 2 == 1)
		lung_mij—;
	if(palindrom(s,n-lung_mij+1,n)==true)
	{	
    sol = max(sol,lung_mij); 
    st = mij+1
   	 }
	else dr = mij-1;

   }


	return s.size() - sol;
	
}


11 -> 9 - > 7 - >5 -> 3 -> 1

11->10->9->8

adcefgfecda

abba
#a#b#b#a#
ded
#d#e#d#

log2(50) =
2^5 = 32
2^6 = 64
5 
int main()
{
   int c,cnt=0;
   string s;
   fin>>n;
   if(c==1)
   {
      for(int i=1;i<=n;i++)
	{
	   fin>>s;
	   cnt+=sol(s);
	}
   }
}



{
   int c,cnt=0;
   string s;
   fin>>n;
   if(c==2)
   {
      for(int i=1;i<=n;i++)
	{
	   fin>>s;
	   v[i] = solve(s);
	}
   }


for(int dr = 1; dr <=n; ++dr) {
	s += v[dr];
	if(s <= S)
	{
		l++;
		lmax = max(lmax, l);
	}
	else
		    {
				while(s > S)
				{
					s -= v[st];
				     st++;
					l–;
				}
		    }
}
	
}

https://www.pbinfo.ro/probleme/4400/primprim

#include <fstream>
#include <climits>
using namespace std;

ifstream fin("primprim.in");
ofstream fout("primprim.out");

const int nmax=1000001;
int f[60];
int c,n,v[nmax],cost[nmax],emin[nmax],emax[nmax],cnt=0,vcs[nmax],sp[nmax];
bool e[nmax];

void eratostene(){
    e[0]=1,e[1]=1;
    for(int i=2;i<=nmax;i++){
        if(e[i]==0){
            for(int j=i+i;j<=nmax;j+=i){
                e[j]=1;
            }
        }
    }
    for(int i=2;i<=nmax;i++){
        if(e[i]==0){
            cnt=0;
            emin[i]=cnt;
        }else if(e[i]==1){
            cnt++;
            emin[i]=cnt;
        }
    }
    cnt=0;
    for(int i=nmax;i>=2;i--){
        if(e[i]==0){
            cnt=0;
            emax[i]=cnt;
        }else if(e[i]==1){
            cnt++;
            emax[i]=cnt;
        }
    }
    for(int i=2;i<=nmax;i++)cost[i]=min(emin[i],emax[i]);
}





int main()
{
    eratostene();
    cost[1]=1;
    fin>>c>>n;

    if(c==1){
        int sum=0;
        for(int i=1;i<=n;i++){
            fin>>v[i];
            int ap = cost[v[i]];
            sum+=ap;
        }
        fout<<sum;
    }else if(c==2){
        for(int i=1;i<=n;i++){
            fin>>v[i];
            vcs[i]=cost[v[i]];
            f[vcs[i]]++;
            
        }
        int q,j,x,p;
        fin>>q;
      
        for(int i=1;i<=q;i++){
            fin>>j>>x>>p;
			f[cost[v[j]]]--;
			v[j] = x;
			f[cost[v[j]]]++;
			int numere = 0, diferenta = 0;
			int suma = 0;
			while(numere < p) {
				if(numere + f[diferenta] <= p) {
				   numere += f[diferenta];
				   suma += diferenta*f[diferenta];
				}
				else {
					
					suma += (p-numere)*diferenta;
					numere += p-numere;
					
				}
				++diferenta;
			}
			fout << suma << "\n";
        }
 
    }
    return 0;
}

https://www.pbinfo.ro/probleme/4786/teren1

// Your code here