Skip to content

DAA ELAB SRM Solutions – Design and Analysis of Algorithms codes answers 2023 Part 1

    srmnotesadda

    Table of Contents

    In the following figure, you can see a rectangular :-

    #include <bits/stdc++.h> using namespace std; void solve(){

    cout<<"return(l-2*x)*(b-2*x)*x;";

     

    }

     

    int main()

     

    {

     

    int tc;

     

    double a, b, c, res, l, w, x;

     

    scanf(" %d", &tc);

     

    while(tc–) {

     

    scanf(" %lf %lf", &l, &w); a = 12.0;

    b = -4.0 * (l+w); c = l*w;

    x = (-b – sqrt (b*b – 4.0*a*c)) / (2.0*a); res = (l – 2*x) * (w – 2*x) * x;

    printf ("%.9f\n", res);

     

    }

     

    return 0;

     

    }

     

    Mr. somu has another problem for Agi today.

     

    #include<bits/stdc++.h> using namespace std; int main()

    {

     

    int t; cin>>t; while(t–){

    int b,n,r; cin>>b>>n>>r; int z=1;

    for(int i=1;i<=n;i++){ z=z*i;

    }

     

    int res; res=pow(b,z); cout<<res%r<<endl;

    }

     

    return 0; cout<<"if(n%2==1)";

    }

     

    Major Kathiravan has a chart of distinct :-

     

    #include<bits/stdc++.h> #define f(n) for(int i=0;i<n;i++) using namespace std;

    int main()

     

    {

     

    int n; cin>>n; int arr[n];

    int res=10000; f(n){

    cin>>arr[i];

     

    }

     

    f(n){

     

    for(int j=i+1;j<n;j++){ if(arr[i]>arr[j]){

    res=min(res,arr[i]-arr[j]);

     

    }

     

    }

     

    }

     

    cout<<res;

     

    return 0; cout<<"while";

     

    }

     

     

    The allies are trying to get a message

     

     

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

    void solve(){ cout<<"break;"; } int main(){

    string s1,s2,s3,s4; double r;

    double h; cin>>s1>>r>>s2>>s3>>s4; if(s2=="FEET")

    r=r/3.28;

     

    //cout<<r<<endl; if(s2=="KILOMETERS") r=r*1000; if(s2=="YARDS") r=r*0.9144; if(s2=="INCHES") r=r*0.0254; if(s2=="MILES") r=r*1609.34; if(s4=="HOUR") r=r/3600; if(s4=="MINUTE") r=r/60; if(s2=="CENTIMETERS") r=r/100;

    h=r*r/(2*9.805);

     

    cout<<s1<<" will launch the message "<<fixed<<setprecision(2)<<h<<" meters high, ";

     

    if(h>50) cout<<"OUCH!";

     

    else if(h<25) cout<<"SPLAT!"; else cout<<"SUCCESS!";

    return 0; }

     

     

    Given 'm' positive integers denoting an upgrading map :-

     

     

    #include <bits/stdc++.h> using namespace std; #define f(n) for(i=0;i<n;i++)

    #define g(n) for(i = 1; i < n; i++) #define k(n) for(i=n-2;i>=0;i–) int maxWater(int arr[], int n)

    {

     

    int left[n],i; int right[n]; int water = 0;

    left[0] = arr[0]; g(n)

    left[i] = max(left[i – 1], arr[i]); right[n – 1] = arr[n – 1];

    k(n)

     

    right[i] = max(right[i + 1], arr[i]); for(i = 1; i < n-1; i++)

     

    {

     

    int var=min(left[i-1],right[i+1]); if(var > arr[i])

    {

     

    water += var – arr[i];

     

    }

     

    }

     

    return water;

     

    }

     

    int main()

     

    {

     

    int n,i; cin>>n; int arr[n]; f(n){

    cin>>arr[i];

     

    }

     

    cout << maxWater(arr, n); return 0;

    }

     

     

    The Mask ate a block of dynamite to save :-

     

     

    #include <bits/stdc++.h>

     

    using namespace std; int main()

    {

     

    float a,c,d; string b; cin>>a>>b>>c; float res;

    int z=b.size(); if(z==1) d=b[0]-48;

    else

     

    d=(float)(b[0]-48)/(b[2]-48); res=a*d*0.45*7.5; if(res>c){

    cout<<res<<" the Mask should not eat it!";

     

    }

     

    else

     

    cout<<fixed<<setprecision(2)<<res<<" the Mask can eat it!"; return 0;

    cout<<"for";

     

    }

     

     

    There is a Gangaroo initially placed at the :-

     

     

    #include <stdio.h> int main(){

    int x,y,s,t,i,j,count=0;

     

    scanf("%d", &x);

     

    scanf("%d", &y);

     

    scanf("%d", &s);

     

    scanf("%d", &t); for(i=x;i<=x+s;i++){

    for(j=y;j<=y+s;j++){ if(i+j<=t) count++;

    }

     

    }

     

    printf("%d",count); return 0; printf("if(s>=t)if(s<=t/2)");

    }

     

     

    Inspector Gadget just installed new springs :-

     

    #include<bits/stdc++.h> using namespace std; int main()

    {

     

    string s1,s2,s3,s4,s5,s6; cin>>s1>>s2>>s3>>s4>>s5>>s6; float a,b,c;

    if(s2=="?"){

     

    b=stof(s4); c=stof(s6);

    //cout<<c;

     

    cout<<"F "<<fixed<<setprecision(2)<<(-b)*c;

     

    }

     

    else if(s4=="?"){ a=stof(s2); c=stof(s6);

    cout<<"K "<<fixed<<setprecision(2)<<a/(-c);

     

    }

     

    else

     

    {

     

    a=stof(s2); b=stof(s4);

    cout<<"X "<<fixed<<setprecision(2)<<a/(-b);

     

    }

     

    return 0;

     

    }

     

    A double-square number is an integer Y which can be :-

     

    #include <bits/stdc++.h> using namespace std; int sumSquare(int n)

    {

     

    int res=0;

     

    for (long i = 0; i * i <= n; i++)

     

    for (long j = i; j * j <= n; j++)

     

    if ((i * i + j * j == n) ) {

     

    res++;

     

    }

     

    return res;

     

    }

     

    int main()

     

    {

     

    int t; cin>>t; int i=1; while(t–){

    int n; cin>>n;

    cout<<"Line #"<<i<<": "<<sumSquare(n)<<endl; i++;

     

    }

     

    return 0;

     

    cout<<"for(i=0;i<=sqrt(y);i++) for(j=0;j<=i;j++)";

     

    }

     

     

    Given two integers: 'b' and 'a'

    #include <iostream> using namespace std; int main()

    {

     

    int t;

     

    long long m; long long n; long long ans; scanf("%d", &t);

    for (int cs = 1; cs <= t; cs++) {

     

    scanf("%lld %lld", &n, &m); ans = (n * m ) / 2; printf("%lld\n",ans);

    }

     

     

    }

    Great news! You get to go to Japan :-

    #include<iostream> using namespace std; int main()

    {

     

    int items; int a,j,cnt=0;

    cin>>a>>items; int c[items]; string s[items];

    for(j=0;j<items;j++){ cin>>s[j]>>c[j];

    if(c[j]<a){

     

    cout<<"I can afford "<<s[j]<<endl; a=a-c[j];

    }

     

    else{

     

    cnt++;

     

    cout<<"I can't afford "<<s[j]<<endl;

     

    }

     

    //cout<<cnt;

     

    }

     

    if(cnt==items)

     

    cout<<"I need more Yen!"; else

    cout<<a;

     

    return 0; cout<<"for(i=1;i<=yen;i++) int i,j;";

    }

     

     

    Scrooge Mcduck's vault is practically :-

     

    #include<iostream> using namespace std; int main()

    {

     

    int p,q,r,i; int c; cin>>c;

    for(i=0;i<c;i++){ cin>>p>>q>>r; q=q+(r-1)/5;

    r=(r-1)%5+1;

     

    p=p+(q-1)/10;

     

    q=(q-1)%10+1;

     

    cout<<p<<" "; cout<<q<<" "; cout<<r<<endl;

    }

     

    return 0;

     

    }

     

     

    Tina owns a match making company, which even to her :-

     

    #include<bits/stdc++.h> using namespace std; int main()

    {

     

    int t,n; cin>>t; while(t–){

    cin>>n;

     

    int a[n],b[n],sum=0; for(int i = 0;i<n;i++) cin>>a[i];

    for(int i=0;i<n;i++) cin>>b[i]; sort(a,a+n);

    sort(b,b+n);

     

    for(int i=0;i<n;i++){

     

    if(a[i]%b[n-i-1]==0 || b[n-i-1]%a[i]==0) sum++;

    }

     

    cout<<sum<<endl;

     

    }

     

    return 0;

     

    }

     

     

    Ace ventura, pet detective, is on the hunt for a rare :-

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

    #define p1 cout<<"Ace, move fast, pigeon is at ("<<i<<",0)";

     

    #define p2 cout<<"Ace, move fast, pigeon is at ("<<(i-i/z)%z<<","<<i/z<<")"; #define p3 cout<<"No pigeon, try another map, Ace";

    #define a continue;

     

    #define f(n) for(int i=0;i<z;i++)

     

    #define while1 while((scanf("%c",&s[i])) != EOF) int main(){

    string s1; cin>>s1; int z=s1.size(); f(n){

    if(s1[i]=='P'){ p1

     

    return 0;}

     

    }

     

    //cout<<z<<endl; int i=0,cnt=0; char s[10000]; while1 {

    if(s[i]=='P'){

     

    cnt=1; break;

    } i++;

    }

     

     

    if(cnt==1) p2 else p3 }

     

     

    The sapphire consulting and marketing company is adding :-

     

    #include <stdio.h> #include <stdlib.h> int isqrt(n) int n; {

    int i; for(i=0;i*i<n;i++); return i;

     

    }

     

     

    int main() {

     

    int c;

     

    int t,h,s,i,j; int d;

     

     

    scanf("%d",&c); for(i=0;i<c;i++) {

    s=0;

     

    scanf("%d %d",&t,&h); d=isqrt(t);

    s+=t+(d*4);

     

     

    for(j=1;j<h;j++) {

     

    s+=3;

     

    s+=(d+j)*4; if((d+j)>2)

    s+= (d+j-2)*2;

     

    }

     

    printf("%d liters\n",s);

     

    }

     

    return 0;

     

    }

     

     

    Ganesan has a string S consisting of lowercase:-

    #include<bits/stdc++.h> using namespace std; int main()

    {

     

    string s,s2; cin>>s>>s2;

    int z = s.length(); int i;

    int a[z]; for(i=0;i<(int)s.length();i++){

    a[i]=s[i+1]-s[i];

     

    }

     

    for(int i=0;i<z-2;i++){ if(a[i]!=a[i+1]){ cout<<"No"; return 0;}

    }

     

    cout<<"Yes";

     

    return 0;

     

    }

     

    RSA is a public key cryptosystem.

    #include <bits/stdc++.h> using namespace std; void solve(){

    cout<<"break;";

     

    }

     

    bool isProduct(int num)

     

    {

     

    int cnt = 0;

     

    for (int i = 2; cnt < 2 && i * i <= num; ++i) { while (num % i == 0) {

    num /= i;

     

    ++cnt;

     

    }

     

    }

     

    if (num > 1)

     

    ++cnt; return cnt == 2;

    }

     

    void findNumbers(int N)

     

    {

     

    vector<int> vec;

     

    for (int i = 1; i <= N; i++) {

     

    if (isProduct(i) ) {

     

    vec.push_back(i);

     

    }

     

    }

     

    cout<<vec.size()<<endl;

     

    }

     

    int main()

     

    {

     

    int t,N; cin>>t; while(t–){

    cin>>N; findNumbers(N);

    }

     

    return 0;

     

    }

     

    ROYGBIV isn't just an acronym, it's a way of life :-

     

    #include<bits/stdc++.h> using namespace std; void solve(){

    cout<<"for break;";

     

    }

     

    int main()

     

    {

     

    int t=4; while(t–){

    string s1,s2; cin>>s1>>s2; if(s2=="WHITE")

    cout<<"LIGHT "<<s1<<endl; else if(s2=="BLACK") cout<<"DARK "<<s1<<endl; else if(s1=="WHITE") cout<<"LIGHT "<<s2<<endl; else if(s1=="BLACK") cout<<"DARK "<<s2<<endl;

    else if((s1=="RED"&&s2=="YELLOW")||(s1=="YELLOW"&&s2=="RED"))

     

    cout<<"ORANGE"<<endl;

     

    else if((s1=="BLUE"&&s2=="YELLOW")||(s1=="YELLOW"&&s2=="BLUE"))

     

    cout<<"GREEN"<<endl;

     

    else if((s1=="BLUE"&&s2=="RED")||(s1=="RED"&&s2=="BLUE"))

     

    cout<<"PURPLE"<<endl; else if(s1==s2) cout<<s1<<endl;

    else cout<<"N/A"<<endl;

    }

     

    return 0;

     

    }

     

    The people of vadipatti have decided to paint each of their villas :-

     

     

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

    #define fainou ios_base::sync_with_stdio(false);cin.tie(NULL) #define ll long long

    #define mod 1000000007 void solve(){

    cout<<"for(i=0;i<tc;i++) for(j=0;j<N;j++)for(j=1;j<N;j++)";

     

    }

     

    int main(){

     

    fainou; ll t; cin>>t; int i=1;

    while(t–){ ll n;

    cin>>n;

     

    ll a[n][3],ans; cin>>a[0][0]>>a[0][1]>>a[0][2];

    for(ll i=1;i<n;i++){

     

    cin>>a[i][0]>>a[i][1]>>a[i][2];

     

    a[i][0]+=min(a[i-1][1],a[i-1][2]);

     

    a[i][1]+=min(a[i-1][0],a[i-1][2]);

     

    a[i][2]+=min(a[i-1][0],a[i-1][1]);

     

    }

     

    ans=min(a[n-1][0],a[n-1][1]); ans=min(a[n-1][2],ans);

    cout<<"Line "<<i++<<": "<<ans<<"\n";

     

    }

     

    }

     

     

    Shankar is a volleyball trainer at government school in madurai:-

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

    const int N = (int) 1e6 + 6, mod = (int) 0; int a[N];

    long long sum[N]; int main() {

    int tc; cin >> tc;

    for (int tt = 1; tt <= tc; ++tt) { int n, p;

    cin >> n >> p;

     

    for (int j = 0; j < n; ++j)

     

    cin >> a[j]; sort(a, a + n);

     

    int i; for(i=0;i<n;i++)

    sum[i + 1] = sum[i] + a[i]; long long res = 1e18;

    for (int j = p – 1; j < n; ++j) {

     

    long long s = sum[j + 1] – sum[j – (p – 1)]; long long cost = (LL) a[j] * p – s;

    res = min(res, cost);

     

     

    }

     

    cout << res << '\n';

     

    }

     

    }

     

    Maakesh caught the trail of the ancient book

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

    #define f(n) for(int i=1;i<=n;i++) #define g(n) for(int i=1;i<n;i++) const int N=100005; vector<int>e[N];

    int can[N],d1[N],d2[N],d3[N],n,m,d,p1,p2,ans; void dfs(int u,int f,int* d){

    d[u]=d[f]+1;

    for(int i=0;i<(int)e[u].size();i++) if(e[u][i]!=f)

    dfs(e[u][i],u,d);

    }

    int main(){

    cin>>n>>m>>d; f(m) {

    int p; scanf("%d",&p); can[p]=1;

    }

    g(n) {

    int a,b; scanf("%d%d",&a,&b);

    e[a].push_back(b);

    e[b].push_back(a);

    }

    dfs(1,0,d1); f(n)

    if(can[i]&&d1[i]>d1[p1])

    p1=i;

     

    dfs(p1,0,d2); f(n)

    if(can[i]&&d2[i]>d2[p2])

    p2=i;

    dfs(p2,0,d3); f(n)

    if(d2[i]<=d+1&&d3[i]<=d+1) ans++;

    printf("%d\n",ans); return 0;

    cout<<"void evil(int u,int p=0)";

     

    }

     

     

     

     

    Padmavati is a clever girl and she wants to participate

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

    ll fen[1000001]{0},sum,ans=0; void upd(int i,int c){

    for(;i<=1000000;i+=i&-i) fen[i]+=c;

    }

    ll que(int i){

    for(sum=0;i;i-=i&-i) sum+=fen[i]; return sum;

    }

    int main(){

    int n; cin>>n; int a[n];

    map<int,int>pre,suff;

     

    for(int i=0;i<n;++i) cin>>a[i];

    for(int i=n-1;i>=0;–i) upd(++suff[a[i]],1); for(int i=0;i<n;++i){

    upd(suff[a[i]]–,-1); ans+=que(pre[a[i]]++);

    }

    cout<<ans;

     

    }

     

     

     

     

     

    In this problem you will meet the simplified model of game Pudding monsters.

     

     

    #include <bits/stdc++.h> #define fi first

    #define se second #define lo long long #define inf 1000000009

    #define md 1000000007

    #define li 300005 #define mp make_pair #define pb push_back using namespace std;

    int n,x,y,v[li],a[li],b[li],mn[li],mx[li],g[li]; lo int ans;

    void work(int *a,int *b){

    int n=a[0],m=b[0]; mn[m+1]=0;

    for(int i=1;i<=m;i++){

    mn[i]=min(mn[i-1],b[i]);

    mx[i]=max(mx[i-1],b[i]);

     

    }

    int mna=inf,mxa=0; int l=1,r=1;

    for(int i=1;i<=n;i++){

    mna=min(mna,a[i]);

    mxa=max(mxa,a[i]); int d=mxa-mna+1-i;

    if(d>0 && d<=m && mn[d]>mna && mx[d]<mxa) ans++; for( ;mn[r]>mna;r++) g[mx[r]-r]++;

    for( ;l<r&&mx[l]<mxa;l++) g[mx[l]-l]–; ans+=g[mna+i-1];

    }

    for(int i=l;i<r;i++) g[mx[i]-i]=0;

     

    }

    void solve(int l,int r){

    if(l==r) return ; int mid=(l+r)/2;

    a[0]=mid-l+1;b[0]=r-mid;

    for(int i=l;i<=mid;i++) a[mid+1-i]=v[i]; for(int i=mid+1;i<=r;i++) b[i-mid]=v[i]; work(a,b),work(b,a); solve(l,mid);solve(mid+1,r);

    }

    int main(){

    cin>>n;

    for(int i=1;i<=n;i++){

    cin>>x>>y; v[x]=y;

    }

    mn[0]=inf; solve(1,n);

     

    printf("%lld\n",ans+n); return 0;

    }

     

     

     

     

     

     

     

    Now sabanayagam becomes a commander of Ladakh.

     

     

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

    #define SOLVE void dfs(int u,int par) cin>>n; cin>>u>>v; #define f(n) for(int i = 0; i < n – 1; ++i)

    vector<int> g[100010]; char color[100010]; int dfs(int x, int p) {

    int b = (1 << 26) – 1;

    int cnt[26] = {};

    for(int y: g[x]) if(y != p) {

    int t = dfs(y, x);

    for(int i = 0; i < 26; ++i)

    if(~t & (1 << i))

    cnt[i]++;

    b &= t;

    }

    int c = -1;

    for(int i = 0; i < 26 && cnt[i] < 2; ++i) if(cnt[i] == 0)

    c = i; color[x] = 'A' + c;

    b |= ((1 << 26) – 1) ^ ((1 << c) – 1);

     

    b &= ~(1 << c); return b;

    }

    int main() {

    int n; scanf("%d", &n); f(n) {

    int a, b; scanf("%d%d", &a, &b); g[a].push_back(b);

    g[b].push_back(a);

    }

    dfs(1, 0);

    for(int i = 1; i <= n; ++i) printf("%c%c", color[i], " \n"[i == n]);

    }

     

     

     

     

    Let P be an array consisting of N numbers. The array's

     

     

    #include <stdio.h>

     

     

    int md;

     

     

    int s(int n) {

    return (n % 2 == 0 ? (n / 2 % md) * ((n + 1) % md) : (n % md) * ((n + 1) / 2 % md)) % md;

    }

     

     

    int sum, cnt;

     

     

    void queries(long long n, long long k, long long a) { int sum0, cnt0, sum1, cnt1;

     

    if (k <= 0 || a <= 0)

     

    sum = cnt = 0; else if (k >= n) {

    if (a > n)

    a = n;

    sum = s(a), cnt = a % md;

    } else {

    queries((n + 1) / 2, k, (a + 1) / 2), sum0 = sum, cnt0 = cnt; queries(n / 2, k – (n + 1) / 2, a / 2), sum1 = sum, cnt1 = cnt; sum = ((long long) sum0 * 2 – cnt0 + md + sum1 * 2) % md; cnt = (cnt0 + cnt1) % md;

    }

     

    }

     

     

    int main() {

    int n; int m;

     

    scanf("%d%d%d",&n,&m,&md); while (m–) {

    long long l, r, a, b; int ans;

     

    scanf("%lld%lld%lld%lld", &l, &r, &a, &b), l–, a–; ans = 0;

    queries(n, r, b), ans = (ans + sum) % md; queries(n, r, a), ans = (ans – sum + md) % md; queries(n, l, b), ans = (ans – sum + md) % md; queries(n, l, a), ans = (ans + sum) % md; printf("%d\n", ans);

    }

    return 0;

     

    }

     

     

     

     

     

    Fazil is an unemployed computer scientist who spends his days working at odd-jobs.

     

     

    #include <bits/stdc++.h> using namespace std; #define ll long long const int inf = 1e9;

    const int N = 62; char word[N];

    ll dp[N][N];

    long long calculate(int s,int e) { if (s > e) return 0;

    if (s ==e) return 1; ll &p = dp[s][e];

    if (p != -1) return p; p = 0;

    if (word[s] == word[e]) p = 1 + calculate(s+1, e-1);

    p += (calculate (s+1, e) + calculate (s, e-1) – calculate (s+1, e-1)); return p;

    }

    int main ()

    {

    ll res;

    cin>>word;

    memset (dp, -1, sizeof (dp));

    res = calculate (0, strlen(word)-1); printf ("%lld\n", res);

     

    return 0;

     

    }

     

     

     

     

     

    A set of points on a plane is called fair, if for any two points at least one of the three

     

     

    #include<bits/stdc++.h> using namespace std; void fiv(int l,int r){

    cout<<"cin>>n;cin>>a[i].first>>a[i].second;";

    }

    pair<int,int>p[10010]; set<pair<int,int> >s;

     

    void dfs(int l,int r)

    {

    if(l==r)

    {

    s.insert(p[l]); return;

    }

    int i,mid=(l+r)/2; dfs(l,mid);

    dfs(mid+1,r);

    for(i=l;i<=r;i++) s.emplace(p[mid].first,p[i].second);

    }

     

     

    int main()

    {

    int n,i;

     

    scanf("%d",&n);

    for(i=1;i<=n;i++) scanf("%d%d",&p[i].first,&p[i].second); sort(p+1,p+n+1);

    dfs(1,n); printf("%d\n",(int)s.size());

    for(auto it:s) printf("%d %d\n",it.first,it.second); return 0;

    }

     

     

     

     

    Teja has given a permutation of numbers from 1 to n.

     

     

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

    int a[300010],n,p[300010];

    void update(int t,int l,int r,int x){

    cout<<"int query(int t,int l,int r,int L,int R)cin>>x;";

    }

    int main()

    {

    scanf("%d",&n); for(int i=1;i<=n;i++)

    {

    scanf("%d",&a[i]); p[a[i]]=i;

    }

    for(int i=1;i<=n;i++)

    {

    for(int j=i+1;j<=min(n,i+5);j++)

    {

    if(a[i]*2-a[j]>0&&a[i]*2-a[j]<=n&&p[a[i]*2-a[j]]<i)

     

    return puts("YES"),0;

    if(a[j]*2-a[i]>0&&a[j]*2-a[i]<=n&&p[a[j]*2-a[i]]>j) return puts("YES"),0;

    }

     

    }

    puts("NO"); return 0;

    }

     

     

     

     

    Programmer Sandhosh and you have a new year tree

     

     

    #include <iostream>

    int L[1000005],N=4,P[1000005][20],Q,v,i,p=2,q=3,d=2;

    using namespace std; int lca(int x,int y){

    cout<<"int dis(int x,int y) cin>>u;"; return 1;

    }

    int f(int a, int b)

    {

    if(L[a]<L[b])swap(a,b); for(i=0;i<20;i++)if((L[a]-L[b])&(1<<i))a=P[a][i];

    for(i=19;i>=0;i–)if(P[a][i]!=P[b][i])a=P[a][i],b=P[b][i]; return P[a][0];

    }

    int main()

    {

    L[2]=L[3]=L[4]=1,P[2][0]=P[3][0]=P[4][0]=1;

    cin>>Q; while(Q–)

     

    {

     

    cin>>v; L[N+1]=L[N+2]=L[v]+1,P[N+1][0]=P[N+2][0]=v,N+=2;

    for(i=1;i<20;i++)P[N][i]=P[P[N][i-1]][i-1],P[N-1][i]=P[P[N-1][i-1]][i-1]; if(L[N]+L[p]-2*L[f(N,p)]>d)q=N,d++;

    if(L[N]+L[q]-2*L[f(N,q)]>d)p=N,d++;

    cout<<d<<"\n";

     

    }

     

    }

     

     

     

     

     

    Leopard is in the amusement park. And now she is in a queue

     

     

    #include<iostream> using namespace std; inline int getint(){ char c;

    while((c=getchar())<'0'||c>'9');return c-'0';

    }

    const int N=4005,inf=.5e9; int n,k,sum[N][N],f[N],g[N]; int main(){

    cin>>n>>k;

    for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)

    sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+getint(); g[n+1]=n;

    for(int kk=2;kk<=k;kk++) for(int i=n;i;i–){

    f[i]=-inf;

     

    for(int j=g[i];j<=g[i+1]&&j<i;j++){ int now=f[j]-sum[j][j]+sum[j][i]; if(now>f[i]){

    f[i]=now;

    g[i]=j;

    }

    }

    }

    printf("%d\n",sum[n][n]/2-f[n]);

    }

     

    simon has given n numbers a1

     

     

    #include <bits/stdc++.h> using namespace std; const int N=200000;

    int n,k,x;

    long long z=1,a[N+9],pr[N+9],Ans;

     

     

    int main() {

    cin>>n>>k>>x;

    for (int i=1;i<=n;i++) { cin>>a[i];

    pr[i]=pr[i-1]|a[i];} while (k–) z*=x; long long sf=0;

    for (int i=n;i>=1;i–) {

    Ans=max(Ans,pr[i-1]|a[i]*z|sf); sf|=a[i];

    }

    cout<<Ans<<endl; return 0;

     

    }

     

     

    Nandanan's company employed n people.

     

     

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

    #define ans cin>>ans[0];cin>>a>>b>>c; #define f(n) for(int i=0;i<n;i++)

    void solve(){}

     

    int main(){

    int n;cin>>n;

    int a[n];f(n) cin>>a[i]; int M;cin>>M; map<int,int> m; while(M–){

    int x,y,c;cin>>x>>y>>c; if(m.find(y)==m.end())

    m[y]=c; else if(c<m[y])

    m[y]=c;

    }

    if((int)m.size()==n-1){

    long long int sum=0; for(auto j : m){

    sum+=j.second;

    }

    cout<<sum;

    }

    else cout<<-1;

    }

     

     

    Leave a Reply

    Back to Top