Submission #1167246

#TimeUsernameProblemLanguageResultExecution timeMemory
1167246SmuggingSpunLottery (CEOI18_lot)C++20
100 / 100
532 ms19784 KiB
#include<bits/stdc++.h>
#define taskname "C"
using namespace std;
const int lim = 1e4 + 5;
int n, len, q, a[lim];
namespace sub1{
    void solve(){
        vector<vector<int>>cnt(n - len + 2, vector<int>(len + 1, 0));
        for(int i = 1; i + len - 1 <= n; i++){
            for(int j = i + 1; j + len - 1 <= n; j++){
                int s = 0;
                for(int k = 0; k < len; k++){
                    if(a[i + k] != a[j + k]){
                        s++;
                    }
                }
                cnt[i][s]++;
                cnt[j][s]++;
            }
        }
        for(int i = 1; i + len - 1 <= n; i++){
            for(int j = 1; j <= len; j++){
                cnt[i][j] += cnt[i][j - 1];
            }
        }
        for(int _ = 0; _ < q; _++){
            int k;
            cin >> k;
            for(int i = 1; i + len - 1 <= n; i++){
                cout << cnt[i][k] << " ";
            }
            cout << "\n";
        }
    }
}
namespace sub2{
    void solve(){
        vector<vector<int>>f(n + 2, vector<int>(n + 2, 0));
        for(int i = 1; i <= n; i++){
            for(int j = i + 1; j <= n; j++){
                if(a[i] != a[j]){
                    f[i][j]++;
                    if(i > len){
                        f[i - len][j - len]--;
                    }
                }
            }
        }
        for(int i = n; i > 0; i--){
            for(int j = n; j > i; j--){
                f[i][j] += f[i + 1][j + 1];
            }
        }
        vector<vector<int>>cnt(n - len + 2, vector<int>(len + 1, 0));
        for(int i = 1; i + len - 1 <= n; i++){
            for(int j = i + 1; j + len - 1 <= n; j++){
                cnt[i][f[i][j]]++;
                cnt[j][f[i][j]]++;
            }
        }
        for(int i = 1; i + len - 1 <= n; i++){
            for(int j = 1; j <= len; j++){
                cnt[i][j] += cnt[i][j - 1];
            }
        }
        for(int _ = 0; _ < q; _++){
            int k;
            cin >> k;
            for(int i = 1; i + len - 1 <= n; i++){
                cout << cnt[i][k] << " ";
            }
            cout << "\n";
        }
    }
}
namespace sub345{
    const int LIM_Q = 105;
    int to[lim], f[lim][LIM_Q];
    vector<int>ans[LIM_Q];
    void solve(){
        vector<pair<int, int>>query(q);
        for(int i = 0; i < q; i++){
            cin >> query[query[i].second = i].first;
        }
        sort(query.begin(), query.end());
        memset(to, -1, sizeof(to));
        for(int i = q - 1; i > -1; i--){
            to[query[i].first] = i;
        }
        for(int i = lim - 2; i > -1; i--){
            if(to[i] == -1){
                to[i] = to[i + 1];
            }
        }
        vector<int>cnt(n + 2);
        auto work = [&] (int i){
            fill(cnt.begin(), cnt.end(), 0);
            int d = n - i;
            while(i > 0){
                if(a[i] != a[i + d]){
                    cnt[i]++;
                    if(i > len){
                        cnt[i - len]--;
                    }
                }
                i--;
            }
            for(int i = n; i > 0; i--){
                cnt[i] += cnt[i + 1];
                int j = i + d;
                if(j + len - 1 <= n && to[cnt[i]] != -1){
                    f[i][to[cnt[i]]]++;
                    f[j][to[cnt[i]]]++;
                }
            }
        };
        memset(f, 0, sizeof(f));
        for(int i = 1; i < n; i++){
            work(i);
        }
        for(int i = 1; i + len - 1 <= n; i++){
            ans[query[0].second].emplace_back(f[i][0]);
            for(int j = 1; j < q; j++){
                ans[query[j].second].emplace_back(f[i][j] += f[i][j - 1]);
            }
        }
        for(int i = 0; i < q; i++){
            for(int& j : ans[i]){
                cout << j << " ";
            }
            cout << "\n";
        }
    }
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
    cin >> n >> len;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    cin >> q;
    if(n <= 300){
        sub1::solve();
    }
    else if(n <= 2000){
        sub2::solve();
    }
    else{
        sub345::solve();
    }
}

Compilation message (stderr)

lot.cpp: In function 'int main()':
lot.cpp:138:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  138 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...