atcoder_official's blog

By atcoder_official, history, 15 months ago, In English

We will hold Toyota Programming Contest 2025(AtCoder Beginner Contest 389).

We are looking forward to your participation!

  • Vote: I like it
  • +34
  • Vote: I do not like it

| Write comment?
»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I'm happy to attend it and slove $$$5$$$ problems!

»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hoping to do 5 problems

»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

It's my first time to join this contest seriously.Good luck to me

»
15 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

I just solved 4 problems and gave up. It is too hard for me.

»
15 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

man , i dont know why my c dont is correct , aaaaa

  • »
    »
    15 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    after the contest i post my c here

    • »
      »
      »
      15 months ago, hide # ^ |
       
      Vote: I like it +3 Vote: I do not like it

      hope your answer

      • »
        »
        »
        »
        15 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        the logic is correct but need be more faster, i hate tle . ~~~~~ //this is code

        include <bits/stdc++.h>

        using namespace std;

        define f first

        define s second

        typedef long long ll;

        int main(){ int q; cin >> q;

        queue<pair<ll,ll>> snakes;
        ll global = 0;
        
        while(q--){
            int tipo; cin >> tipo;
        
            if(tipo == 1){
                ll l; cin >> l;
                if(snakes.empty()){
                    snakes.push({0,l});
                }else{
                    snakes.push({snakes.back().f + snakes.back().s,l});
                }
            }else if(tipo == 3){
                int k; cin >> k;
                queue<pair<ll,ll>> copia = snakes;
        
                for(ll i = 1; i < k; i++){
                    copia.pop();
                }
                cout << copia.front().f  - global << endl;
            }else{
                int m = snakes.front().s;
                snakes.pop();
                global += m;
            }
        }

        } ~~~~~

»
15 months ago, hide # |
Rev. 2  
Vote: I like it +3 Vote: I do not like it

after contest someone explain solution for D.

»
15 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

After contest, anyone explain approach for D and F, getting stuck in TLE :(

  • »
    »
    15 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You can go at max r distance from the center in any direction.So, Move across the x-axis from [-r to r] and find the corresponding max y above[0,r]and lowest y below [-r,0] with binary search. 61834784

    • »
      »
      »
      15 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Can also use the equation of a circle. Fix values of x like 0.5,1.5,.... and just calculate the y . Since its a circle its symmetrical so you can just multiply by 2

      • »
        »
        »
        »
        15 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it
        void solve()
        {
        	ll r;
        	cin >> r;
        
        	ll n1 = sqrt(1.0 * r * r - 0.5 * 0.5) - 0.5;
        	ll n2 = (1.0 * r) / sqrt( 2.0) - 0.5;
        
        	cout << 4 * (n1 + n2) + 1 << nl;
        }
        

        What's wrong with this solution ?

        • »
          »
          »
          »
          »
          15 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          I dont get what you are trying to do mate.

          I used the equation (x*x+y*y=r*r) for circles, we know r , if we iterate over x from 0.5 we can find the maximum y for which this is valid. I start from the second square , go till the end , multiply this by 2 and then finally add the middle column once.

          Code
          • »
            »
            »
            »
            »
            »
            15 months ago, hide # ^ |
             
            Vote: I like it 0 Vote: I do not like it

            I'm trying to get the maximum count of squares above the root square. With symmetry , we can get the count for the 4 directions , similar logic for diagonal squares .

            • »
              »
              »
              »
              »
              »
              »
              15 months ago, hide # ^ |
               
              Vote: I like it 0 Vote: I do not like it

              Oh ok , well im also doing the same thing i just caluclate apart from the base line as it only occurs once so what i do is calculate from above multipy it by 2 then add. After this i multiply this value by 2 for the whole circle then i manually add the center line which would have occured only once

»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I loved problem E,gave D an hour and still can't do it,i'm really bad at geometry.

»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Looking at the number of ACs on Problem D, it was really disappointing that I couldn’t come up with the solution. Can anyone explain the solution to Problem D?

»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

E was a bit painful

»
15 months ago, hide # |
 
Vote: I like it +15 Vote: I do not like it

My thought process on $$$F$$$.

  1. Bla
  2. Bla
  3. Bla
  4. Implement a segment tree with operations $$$X$$$.

"Maybe I can avoid it? Let's try another idea."

  1. Blo
  2. Blo
  3. Blo
  4. Implement a segment tree with operations $$$Y$$$.

"Try again."

  1. Blu
  2. Blu
  3. Blu
  4. Implement a segment tree with operations $$$Z$$$.
»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Has anyone solved problem E using Lagrange multipliers?

»
15 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it
D Major Spoilers
»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Solving F by performing binary search on segment tree. Narrowly pass with 800+ms. When calculating the complexity during contest, always worrying about TLE

»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

how e

»
15 months ago, hide # |
Rev. 6  
Vote: I like it 0 Vote: I do not like it

With Problem E

I change the value of inf in following code from 1e18+1 to 2e18+1 will result in different answer. Why this happend

#include <bits/stdc++.h>
using namespace std;
typedef __uint128_t ll;
// typedef long long ll;
unsigned long long n, m;
vector<unsigned long long> a;
const long double inf = 1e18+1;
ll calcnt(ll p, ll limit)
{
    return (limit / p + 1) / 2;
}
ll calcost(ll limit)
{
    ll ans = 0;
    for (auto x : a)
    {
        ll cnt = calcnt(x, limit);
        if (x * cnt * cnt > inf)
            return inf;
        ans += cnt * cnt * x;
        if (ans > inf)
            return inf;
    }
    return ans;
}
unsigned long long cal(ll limit)
{
    ll ans = 0;
    for (auto x : a)
    {
        ll cnt = calcnt(x, limit);
        ans += cnt;
    }
    return ans;
}
int main()
{
    cin >> n >> m;
    a.resize(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    sort(a.begin(), a.end());
    ll l = 0, r = 1e18;
    while (l < r)
    {
        ll mid = (l + r) / 2;
        if (calcost(mid) > m)
            r = mid;
        else
            l = mid + 1;
    }
    unsigned long long ans = cal(l - 1);
    m -= (unsigned long long)calcost(l - 1);
    cout << ans + m / (unsigned long long)l << endl;
}
»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone explain the logic for E ? How is binary search applicable ?

  • »
    »
    15 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    it's pretty common trick: You need to pack the backpack with some items. And your task is to put the maximum number of items there. So logically, you'll put the cheapest items. So you perform the BS over the price of last item you can put there.

»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

E > F ?

But I still solve E :)

»
15 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

Problem E can be ACed using the Cauchy–Schwarz inequality with real numbers, followed by removing sub-optimal buys using a heap.

https://atcoder.jp/contests/abc389/submissions/61842706

»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hi, I solved 5th question, this is my code, can someone point out what is my mistake?

#include<bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
void solve(){
    int n,m;
    cin>>n>>m;
    vector<int> a(n);
    for(int i=0; i<n; i++) cin>>a[i];
    int s=0,e=1^18;
    int ans=-1;
    int pnt=0;
    int ss=0;
    while(s<=e){
        int mid=(e-s)/2+s;
        int sum=0;
        int cnt=0;
        for(int i=0; i<n; i++){
            int k=floor(sqrt(double(mid)/double(a[i])));
            sum+=(k*k*a[i]);
            cnt+=k;
        }
        if(sum<=m){
            ans=mid;
            ss=sum;
            pnt=cnt;
            s=mid+1;
        }
        else{
            e=mid-1;
        }
    }
    // cout<<pnt<<endl;
    // cout<<ans<<endl;
    //  cout<<m<<' '<<ss<<' '<<endl;
    pnt+=(m-ss)/(ans+1);
    cout<<pnt<<endl;

}
int32_t main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }
}

Can someone please find mistake.

  • »
    »
    15 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    k*k*a[i] is the total price of k products, the price of the kth product should be (2*k-1)*a[i].

    • »
      »
      »
      15 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      I am stuck in same doubt , I dont understand why calculating using sqrt is wrong. for checking how many k satisfy k^2*p[i] <= x .Why is this incorrect

      • »
        »
        »
        »
        15 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        For each i, check how many k_i satisfy (2*k_i-1)*a[i]<=x, and the sum of k_i^2*a[i] should be <= m.

        • »
          »
          »
          »
          »
          15 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          I was trying to bruteforce with a heap , I do understand that the price of each product would be (2*k_i-1)*a[i] but i dont really understand why is the above comment incorrect it doesnt feel trivial/intituitive to me . Can you explain maybe why the search space for total price wouldnt be monotonic or whats the flaw here

          • »
            »
            »
            »
            »
            »
            15 months ago, hide # ^ |
            Rev. 2  
            Vote: I like it 0 Vote: I do not like it

            It seems that you don't understand the meaning of x here. x is the upper limit of the purchase price of each product. For product i, the purchase quantity k must satisfy (2*k_i-1)*a[i]<=x.

            Our goal is to find the largest x so that m is enough to buy all products with a price not exceeding x, and the remaining money can be used to buy products with a price of x+1. This x can be found by binary search x and checking the total price sum(k_i^2*a[i])<=m

»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Give clarity on this please.

In problem E. It calculated a price x(using binary search) for which we can by all items with price <=x. Such that total cost is <=M but there will be some remaining money. why is this remaining money used to buy as many items as possible of price x+1. (remaining_money)/(x+1). How we know if item priced x+1 will exist with surety and we can buy as many as possible.