Блог пользователя lis05

Автор lis05, история, 5 лет назад, По-английски

Hello codeforces! Try to solve both of the problems from https://mirror.codeforces.com/contestInvitation/fcf424afaf5b7a5d01aebf1a908d6ef3589e4fb3. good luck

Solution:

This is a simple dp problem(very simple), but with a high constraints. Standard knapsack runtime is O(NW), but we can optimize it to run in O(NW/32) using bitset.

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

const int W=2e5;
bitset<W>b;
 
signed main(){
    int n,w;
    scanf("%d %d",&n,&w);
    b[0]=1;
    while(n--){
        int a;
        scanf("%d",&a);
        b|=(b<<a);
    }
    if(b[w])printf("YES\n");
    else printf("NO\n");
}

How does it work? b[k] contains 0, if it's not possible to get sum k, and 1, if it's possible.

At start, we set b[0] to 1(because we can get sum 0). Next, for each item we left-shift out bitset by a[i]. new_b=b<<a[i] After this move, new bitset contains information about what can we get if we will take current item, but it ignores all previous moves(when we didn't take current item). To fix this, we need to connect two bitsets in one using bitwise or. new_new_b=new_b|b

To do this fast, we just write b|=(b<<a)

Why this works fast? We do N operations of shifting W elements. But bitset works as a long binary number, which constructs of a many 32bit integers. So we don't shift W numbers, we shift only W/32. this is why it work's so fast

But this solution runs in 1s, which is too much. How to improve the improvement? Add some useful pragmas:

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

// very useful pragmas
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")


const int W=2e5;
bitset<W>b;
 
signed main(){
    int n,w;
    scanf("%d %d",&n,&w);
    b[0]=1;
    while(n--){
        int a;
        scanf("%d",&a);
        b|=(b<<a);
    }
    if(b[w])printf("YES\n");
    else printf("NO\n");
}

Now it runs in ~700ms

P.S This was our first ever problem created on polygon, so we failed it a little with bad tests. Sorry, we will improve ourself for the future!

Also thanks for a great callback!

  • Проголосовать: нравится
  • +35
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится -10 Проголосовать: не нравится

how is the second problem possible to solve?

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

Ok so isnt it kinda stupid to not be able to see other's submissions, since its obviously some trick? I feel like would be much more productive otherwise

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

For 2nd task memory limit is quite tough, using long long was giving mle and int was giving ac.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by lis05 (previous revision, new revision, compare).

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

$$$\mathcal O(n \sqrt S)$$$ solution where $$$S = a_1 + \dots + a_n$$$. Passes A2 in 46 ms.

»
5 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +19 Проголосовать: не нравится

Because of condition $$$a_1 + a_2 + \dots + a_n \le 2 \cdot 10^5$$$ it's possible to solve this problem in $$$O(\frac{w \cdot \sqrt{2 \cdot 10^5}}{32})$$$.

If we have $$$3$$$ or more items with equal value (say $$$x$$$), we can remove $$$2$$$ of this items and instead of them add one item with value $$$2x$$$. Let's do all this possible exchanges. Now we have $$$\le 2$$$ items of each value. And values sum is still $$$\le 2 \cdot 10^5$$$, because exchanges don't change total sum. It's easy to prove then that items count is $$$O(\sqrt{2 \cdot 10^5})$$$. And we just do simple knapsack with bitset on this items.

Source code

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Btw, your code doesn't work for W = 2e5 because bitsets start indexing at 0. And it's also pretty tight on TL for N, M = 2e5 and all Ai = 1.

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In A2, submitting the above code even with the pragmas will TLE in C++ (64). Use C++ (32 bit).