Showing posts with label Codeforces. Show all posts
Showing posts with label Codeforces. Show all posts

Monday, April 6, 2026

Codeforces round 1090 (div4) - A. 67th integer problem solution

 Hi. I attended a Codeforces round, it was 3 years later from the last online contest I participated. I just solved 2 out of 6 or 7 :) questions. 

A, the first one is here: https://codeforces.com/contest/2218/problem/A

The div4 announcement is here: https://codeforces.com/blog/entry/152610

CF round 1090 editorial is here: https://codeforces.com/blog/entry/152680

Round 1090 itself is here: https://codeforces.com/contest/2218

 So, lets come to questionA. It just asks you to output number+1, unless it is less than the highest point.

The question itself: https://codeforces.com/contest/2218/problem/A 

And here is my solution:  

  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int t, x;
  5. scanf("%d", &t);
  6. while (t--)
  7. {
  8. scanf("%d", &x);
  9. if(x<67) printf("%d\n", x+1);
  10. else printf("%d\n", x);
  11. }
  12. return 0;
  13. }

Thursday, April 10, 2025

Codeforces Round 355 A Vanya and fence solution

 Codeforces Round 355 A Vanya and fence solution

I am glad that I am finally back after years of staying away from competitive programming.

This is my first code after two years of not writing a single line of code.

https://codeforces.com/problemset/problem/677/A


#include 
#include 

#define MAX_BUFFER_SIZE 1024
/*
* Vanya and Fence
* Problem: https://codeforces.com/problemset/problem/677/A
* this program calculates the width of the fence needed to
* cover all the people. some neeed to bend down, creating 2
* spaces, while others can stand up, creating 1 space.
* Today is 10 Apr 2025, Thursday. Istanbul, Turkey. 
*/

/*
* Main function: entry point of the program
* argc: count of command-line arguments
* argv: argument vector
*/

int main(int argc, char *argv[]) {
    // TODO: Add your program logic here.
    // For example, you might fill the buffer, process input, 
    // or execute other functions
    int n, h;
    int heights[1000];

    // Read the number of people and the height of the fence
    // read the height of each person
    // and store it in the heights array
    scanf("%d %d", &n, &h);
    for (int i = 0; i < n; i++) {
        scanf("%d", &heights[i]);
    }
    
    // now check the height of each person
    // if the height is greater than the fence height
    // then add 2 to the width
    // otherwise add 1 to the width
    int width = 0;
    for (int i = 0; i < n; i++) {
        if (heights[i] > h) {
            width = width + 2;
        } else {
            width = width + 1;
        }
    }
    
    // print total width needed to cover all the people
    printf("%d\n", width);

    // to run it cd /path/to/your/code
    // clang 677A-Vanya_and_fence.c -o output
    // you have compiled the code
    // now run the output
    // ./output

    // to read the input from a file
    // clang 677A-Vanya_and_fence.c -o output < input.txt
    
    // Return success status
    return EXIT_SUCCESS;
}

Monday, March 13, 2023

Codeforces Round 379 (Div. 2) - 734 A. Anton and Danik solution

 Codeforces Round 379 (Div. 2) - 734 A. Anton and Danik solution

 

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main() {
  5. #ifndef ONLINE_JUDGE
  6. freopen("input.txt", "r", stdin);
  7. freopen("output.txt", "w", stdout);
  8. #endif
  9. int n, anton=0, danik=0;
  10. char c;
  11. scanf("%d", &n);
  12. do {
  13. scanf("%c", &c);
  14. if(c=='A') anton++;
  15. else if(c=='D') danik++;
  16. } while(n--);
  17. if(anton<danik) printf("Danik\n");
  18. else if(danik<anton) printf("Anton\n");
  19. else printf("Friendship\n");
  20. //printf("Anton: %d, and Danik:%d.\n", anton, danik);
  21. return 0;
  22. }

 



Sunday, March 12, 2023

Codeforces Beta Round 55 (Div. 2) - 59 A. Word solution

 

Codeforces Beta Round 55 (Div. 2) - 59 A. Word solution

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. int main() {
  6. #ifndef ONLINE_JUDGE
  7. freopen("input.txt", "r", stdin);
  8. freopen("output.txt", "w", stdout);
  9. #endif
  10. char s[101];
  11. scanf("%s", s);
  12. int upL=0, lowL=0;
  13. for(int i=0; i<strlen(s); i++) {
  14. //check how many uppercase letters
  15. if(isupper(s[i])!=0) upL++;
  16. //isupper() function of ctype.h checks whether the char ->
  17. //-> is a upper (or capital) letter, returns true ->
  18. //-> if it is upper letter. returns 0 if not
  19. } //check how many uppercase letters, for loop ends here
  20. for(int i=0; i<strlen(s); i++) {
  21. //check how many lower letters
  22. if(islower(s[i])!=0) lowL++;
  23. } //lower letter check loop ends here
  24. if(lowL<upL) { //checking if lower letter is less than upper letter
  25. for(int i=0; i<strlen(s); i++) {
  26. //if so then we need to convert all chars in string to uppercase
  27. s[i]=toupper(s[i]);
  28. //that is achieved by toupper() of ctype.h
  29. //basically we pass through all chars in a string
  30. //and convert them to upper letter
  31. }
  32. }
  33. else { //if lower letters are equal to or more than upper letters
  34. for(int i=0; i<strlen(s); i++) {
  35. s[i]=tolower(s[i]);
  36. //similar to toupper(). tolower() function of ctype.h helps us
  37. //to lowerletter all chars of a string
  38. }
  39. }
  40. printf("%s\n", s);
  41. //printf("up letter is %d\n", upL);
  42. //printf("low letter is %d\n", lowL);
  43. return 0;
  44. }

all necessary information is provided in the code. thanks




Codeforces Round 340 (Div. 2) - 617 A. Elephant solution

Codeforces Round 340 (Div. 2) - 617 A. Elephant solution

I tried to solve it via ceil() funtion in math.h but it gave errors. 

The easiest way to get the least amount of steps is to have the largest number, 5 here, as many times as possible. And then you cover the remaining part with other numbers once. For instance, let's assume input is 14; you need two 5 and one 4; or 27, you need five 5 and one 2; and so on. 

If entered number is divisible by 5, meaning mod 5 is 0, then just divide it by 5 to get the number of the steps. If its mod 5 is not equal to 0 then divide the number by 5 and 1 so as to floor it up, or ceil up, or round up. 

I wish we could use ceil() function. 

Write your comments below. 

Thanks. Good luck! 

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main() {
  5. #ifndef ONLINE_JUDGE
  6. freopen("input.txt", "r", stdin);
  7. freopen("output.txt", "w", stdout);
  8. #endif
  9. int a;
  10. scanf("%d", &a);
  11. if(a%5!=0) printf("%d\n", a/5+1);
  12. else printf("%d\n", a/5);
  13. return 0;
  14. } 

Codeforces Round 405 (rated, Div. 2, based on VK Cup 2017 Round 1) - 791 A Bear and Big Brother solution

After years, maybe 9 or 10, of being away from competitive programming, I'm finally back, and glad :)

I love competitive programming. I missed it. I missed those days in 2013 and 2014 when I would be listening to music in the background and would be coding. Finally, I'm back. This time I think I have brought with me the different skill set: Linux and Emacs. 

Anyway, let's get to the business. This is the solution for Codeforces Round 405 (rated, Div. 2, based on VK Cup 2017 Round 1) - 791 A Bear and Big Brother. 


  1. #include <stdio.h>
  2.  
  3. int main() {
  4. #ifndef ONLINE_JUDGE
  5. freopen("input.txt", "r", stdin);
  6. freopen("output.txt", "w", stdout);
  7. #endif
  8.  
  9. int a, b, c=0;
  10. scanf("%d %d", &a, &b);
  11. while(a<=b) {
  12. a=a*3, b=b*2, c++;
  13. }
  14. printf("%d\n", c);
  15. return 0;
  16. }

Friday, February 2, 2018

Faster solutions with ios_base::sync_with_stdio(0); C++ Codeforces Round #460 (Div. 2), problem: (C) Seat Arrangements solution

I learned the faster way the hard way. Use
ios_base::sync_with_stdio(0);

in your code. My
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
     ios_base::sync_with_stdio(0);
 int n, m, k, row[2005]={0}, col[2005]={0}, ans=0;
 char ch;
 cin>>n>>m>>k;
 for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) {
  cin>>ch;
  if(ch=='.') {
   row[i]++;
   col[j]++;
  }
  else if(ch=='*'){
   row[i]=0;
   col[j]=0;
  }
  if(row[i]>=k) ans++;
  if(col[j]>=k) ans++;
 }
 if(k==1) ans/=2;
 cout<<ans;
 return 0;
}
 
code passed but similar C++ code got TLE without ios base sync stdio. That is my 2 satoshis(cents :) for you.

Sunday, August 3, 2014

Codeforces Round #259 (Div. 2), problem: (A) Little Pony and Crystal Mine solution

Codeforces Round #259 (Div. 2), problem: (A) Little Pony and Crystal Mine: http://codeforces.com/contest/454/problem/A

Codeforces Round #259 (Div. 2), problem: (A) Little Pony and Crystal Mine editorial: http://codeforces.com/blog/entry/13190

Codeforces Round #259 (Div. 2), problem: (A) Little Pony and Crystal Mine solution: 


#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int n;
    char s[105][105];
    scanf("%d", &n);
    for(int i=0; i<n; i++) s[n/2][i]='D';
    for(int i=n/2-1, k=0; i>=0; i--, k++) for(int j=0; j<=k; j++) s[i][j]=s[i][n-1-j]=s[n-1-i][j]=s[n-1-i][n-1-j]='*';
    for(int i=0; i<n; i++) for(int j=0; j<n; j++) if(s[i][j]!='*' && s[i][j]!='D') s[i][j]='D';
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) printf("%c", s[i][j]);
        printf("\n");
    }
    return 0;
}

Monday, July 28, 2014

Codeforces 2014 MemSQL Start[c]UP 2.0 - Round 1, problem: (A) Eevee solution

Codeforces 2014 MemSQL Start[c]UP 2.0 - Round 1, problem: (A) Eevee: http://codeforces.com/contest/452/problem/A

Codeforces 2014 MemSQL Start[c]UP 2.0 - Round 1, problem: (A) Eevee solution: http://ideone.com/XyPs1J


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    int n, cnt=0, idx, b=0;
    char s[15], a[15][15]={"vaporeon","jolteon","flareon","espeon","umbreon","leafeon","glaceon","sylveon"};
    scanf("%d", &n);
    scanf("%s", s);
    for(int i=0; i<n; i++) if(s[i]!='.') b++;
    for(int i=0; i<8; i++) if(strlen(a[i])==n) {
        for(int j=0; j<n; j++) if(a[i][j]==s[j] && s[j]!='.') cnt++;
        if(cnt==b) {idx=i; break;}
        cnt=0;
    }
    printf("%s", a[idx]);
    return 0;
}

Monday, July 21, 2014

Codeforces Round #153 (Div. 1), problem: (A) Points on Line solution

Codeforces Round #153 (Div. 1), problem: (A) Points on Line: http://codeforces.com/problemset/problem/251/A

Codeforces Round #153 (Div. 1), problem: (A) Points on Line editorial: http://codeforces.com/blog/entry/6054

Codeforces Round #153 (Div. 1), problem: (A) Points on Line solution: http://ideone.com/N2cHDh


#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    int n, d, x[100005];
    long long int ans=0;
    scanf("%d%d", &n, &d);
    for(int i=0; i<n; i++) scanf("%d", x+i);
    for(int i=0, j=0; i<n; i++) {
        while(x[i]-x[j]>d) j++;
        ans+=(long long)(i-j)*(i-j-1)/2;
    }
    printf("%I64d", ans);
}

i tried the code below but it gave WA: http://ideone.com/e8VzQB

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;

int main() {
    int n, d, x[100005], it;
    long long int cnt1, cnt2, tmp=-1, ans=0, ma=0, mi=0, dif=0;
    const int con=1e9+1;
    map <int, int> m;
    scanf("%d%d", &n, &d);
    for(int i=0; i<n; i++) scanf("%d", &x[i]);
    for(int i=1; i<n; i++) for(int j=x[i-1]; j<x[i]; j++) m[j]=i;
    m[x[n-1]]=n;
    for(int i=0; i<n; i++) {
        if(x[i]+d>=x[n-1]) it=x[n-1];
        else it=x[i]+d;
        dif=m[it]-m[x[i]]+1;
        if(dif>=3) {
            if(m[it]==tmp) continue;
            cnt1=1, cnt2=1;
            for(int j=1; j<=dif; j++) cnt1*=j;
            for(int j=1; j<=dif-3; j++) cnt2*=j;
            ans+=cnt1/(cnt2*6);
            tmp=m[it];
        }
    }
    printf("%I64d", ans);
    return 0;
}
note: if you give right AC approach similar to my WA approach, that would be appreciated

Codeforces Round #202 (Div. 1), problem: (A) Mafia solution

Codeforces Round #202 (Div. 1), problem: (A) Mafia: http://codeforces.com/problemset/problem/348/A

Codeforces Round #202 (Div. 1), problem: (A) Mafia editorial: http://codeforces.com/blog/entry/9031

Codeforces Round #202 (Div. 1), problem: (A) Mafia solution: http://ideone.com/JEoXON


#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

int main() {
    long long int n, a, ma=0, cnt=0;
    scanf("%I64d", &n);
    for(int i=0; i<n; i++) {
        scanf("%I64d", &a);
        if(a>ma) ma=a;
        cnt+=a;
    }
    a=ceil((double)cnt/(n-1)), a=max(a, ma);
    printf("%I64d", a);
    return 0;
}

note, courtesy of baklazan: http://codeforces.com/blog/entry/9031#comment-147355

Let's say you're going to play x games. If someone wanted to play more than x games, he wouldn't be able to do so, so he would be unhappy. Therefore, if everyone is happy, x ≥ ai must hold for each i, so
x ≥ max(a1, a2, ..., an)
Let $b_i$ be number of games, where i-th player is supervisor and let ci be number of games, where he plays. In each game i-th player is playing, XOR supervising, so ci + bi = x. Also, he wants to play at least ai games, soai ≤ ci, thus bi ≤ ci + bi - ai, so bi ≤ x - ai.
Each game is supervised by one player, so x = b1 + b2 + ... + bn. Giving it all together:
x = b1 + b2 + ... + bn ≤ (x - a1) + (x - a2) + ... + (x - an)
Now we have two lower bounds for $x$ . Smallest x satisfying both of them is
This number of games is enough, because if first $x - a_1$ games are supervised by first player, next x - a2games are supervised by second player etc., until the x-th game, then everyone plays enough games and each game is supervised by someone.

Codeforces Round #228 (Div. 2), problem: (C) Fox and Box Accumulation solution

Codeforces Round #228 (Div. 2), problem: (C) Fox and Box Accumulation: http://codeforces.com/contest/389/problem/C

Codeforces Round #228 (Div. 2), problem: (C) Fox and Box Accumulation editorial: http://codeforces.com/blog/entry/10629

Codeforces Round #228 (Div. 2), problem: (C) Fox and Box Accumulation solution: http://ideone.com/AJ4sh5


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    int n, x[105], pl=0, cnt=0, b[105]={0};
    vector <int> v;
    scanf("%d", &n);
    for(int i=0; i<n; i++) scanf("%d", &x[i]);
    sort(x, x+n); 
    while(cnt<n) {
        pl++;
        for(int i=0; i<n; i++) if(x[i]>=v.size() && b[i]==0) v.push_back(x[i]), b[i]=1, cnt++;
        v.clear();
    }
    printf("%d", pl);
    return 0;
}


Sunday, July 20, 2014

Codeforces Round #257 (Div. 2), problem: (B) Jzzhu and Sequences solution

Codeforces Round #257 (Div. 2), problem: (B) Jzzhu and Sequences: http://codeforces.com/contest/450/problem/B

Codeforces Round #257 (Div. 2), problem: (B) Jzzhu and Sequences editorial: http://codeforces.com/blog/entry/13112

Codeforces Round #257 (Div. 2), problem: (B) Jzzhu and Sequences solution: http://ideone.com/Eh6hpS


#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    long long int x, y, z, n, tmp, a[7]={0}, ans;
    const int md=1e9+7;
    scanf("%I64d%I64d", &x, &y);
    scanf("%I64d", &n);
    z=y-x, a[1]=x, a[2]=y;
    for(int i=3; i<=6; i++) {
        tmp=x, x=y, y=z, z=y-x, a[i%6]=y;
    }
    if(a[n%6]>=0) printf("%I64d", a[n%6]%md);
    else printf("%I64d", (md+a[n%6]%md)%md);
    return 0;
}

note: to learn about mod operation on negative numbers: http://math.stackexchange.com/questions/519845/modulo-of-a-negative-number