QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#372504#3030. Fantastic CompressionSolitaryDream#AC ✓403ms22660kbC++172.1kb2024-03-31 14:19:382024-03-31 14:19:38

Judging History

你现在查看的是最新测评结果

  • [2024-03-31 14:19:38]
  • 评测
  • 测评结果:AC
  • 用时:403ms
  • 内存:22660kb
  • [2024-03-31 14:19:38]
  • 提交

answer

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

const int N=1e5+1e3+7;

int T,n,k;

int s[N];

vector<vector<pair<int,int> > >v;

int p[N],use[N];

int vis[N];

vector<vector<int> >ans;

void dfs(int x)
{
    if(x==k)
    {
        for(int i=1;i<=n;i++)
            if(!use[i])
                return;
        int w=0;
        for(int i=0;i<n;i++)
        {
            if(!p[i])
                return;
            w+=p[i];
            if(i>=k)
                w-=p[i-k];
            if(i>=k-1&&w!=s[i-(k-1)])
                return;
        }
        ans.push_back(vector<int>(p,p+n));
    }
    int y=-1;
    for(int i=1;i<=n;i++)
        if(!use[i])
        {
            y=i;
            break;
        }
    for(int i=0;i<k;i++)
        if(!vis[i])
        {
            bool ok=1;
            for(auto [d,j]:v[i])
            {
                int w=d-v[i][0].first+y;
                if(w<=0||w>n||use[w])
                {
                    ok=0;
                    break;
                }
            }
            if(!ok)
                continue;
            for(auto [d,j]:v[i])
                p[j]=d-v[i][0].first+y,use[p[j]]=1;
            vis[i]=1;
            
            dfs(x+1);
            vis[i]=0;
            for(auto [d,j]:v[i])
                use[p[j]]=0,p[j]=0;
        }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>T;
    while(T--)
    {
        cin>>n>>k;
        v.clear();
        v.resize(k);
        for(int i=0;i<k;i++)
            v[i].push_back({0,i});
        int la;
        cin>>la;
        s[0]=la;
        for(int i=0;i<n-k;i++)
        {
            int x;
            cin>>x;
            s[i+1]=x;
            v[i%k].push_back({x-la+v[i%k].back().first,i+k});
            la=x;
        }
        for(int i=0;i<k;i++)
            sort(v[i].begin(),v[i].end());
        ans.clear();
        dfs(0);
        sort(ans.begin(),ans.end());
        cout<<ans.size()<<"\n";
        for(auto v:ans)
            for(int i=0;i<n;i++)
                cout<<v[i]<<" \n"[i+1==n];
    }
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 3816kb

input:

964
5 4
10 11
5 3
9 8 10
5 2
9 6 5 4
4 4
9
4 4
9
5 4
13 11
5 3
11 10 10
4 2
4 6 3
5 4
12 13
5 3
9 12 9
5 2
9 8 6 3
5 3
10 8 7
5 3
7 10 9
5 4
13 14
5 3
7 8 11
4 2
6 7 4
4 4
11
4 3
8 6
5 3
7 6 6
5 3
10 9 11
5 4
10 11
5 3
12 9 8
5 5
15
5 3
9 10 7
5 5
15
4 2
4 7 6
5 2
4 5 9 7
5 2
6 3 6 7
5 3
7 6 9
4 2
3...

output:

6
4 1 2 3 5
4 1 3 2 5
4 2 1 3 5
4 2 3 1 5
4 3 1 2 5
4 3 2 1 5
1
2 3 4 1 5
1
5 4 2 3 1
0
0
6
4 1 3 5 2
4 1 5 3 2
4 3 1 5 2
4 3 5 1 2
4 5 1 3 2
4 5 3 1 2
0
0
6
2 1 4 5 3
2 1 5 4 3
2 4 1 5 3
2 4 5 1 3
2 5 1 4 3
2 5 4 1 3
2
1 5 3 4 2
2 4 3 5 1
0
1
3 5 2 1 4
1
2 4 1 5 3
6
1 3 4 5 2
1 3 5 4 2
1 4 3 5 2
1 ...

result:

ok 21593 lines

Test #2:

score: 0
Accepted
time: 31ms
memory: 4084kb

input:

11
23853 5
56129 66231 66759 74651 77738 77157 85513 66445 73648 57593 74488 61493 64993 60263 75748 73267 81681 86675 92728 73438 57233 47483 52402 42838 42284 58984 70785 56245 55687 58425 43712 34748 37286 34598 31262 36760 48165 57037 66885 67122 67572 62188 51356 32408 33791 44793 45602 58279 6...

output:

1
1955 20238 7208 20162 6566 12057 20766 15100 23249 5985 20413 1698 22303 7194 22880 7418 5198 17573 22679 20399 15832 10192 23626 3389 4194 6082 15111 14062 2835 20894 17883 571 13504 5573 6181 8919 3109 10816 2237 11679 20324 11981 20664 2474 12129 14940 1149 1716 3857 23131 15749 13826 5377 8992...

result:

ok 16 lines

Test #3:

score: 0
Accepted
time: 403ms
memory: 22660kb

input:

7
6666 6
16671 16672 16673 16674 16675 16676 16677 16678 16679 16680 16681 16682 16683 16684 16685 16686 16687 16688 16689 16690 16691 16692 16693 16694 16695 16696 16697 16698 16699 16700 16701 16702 16703 16704 16705 16706 16707 16708 16709 16710 16711 16712 16713 16714 16715 16716 16717 16718 167...

output:

720
1 1112 2223 3334 4445 5556 2 1113 2224 3335 4446 5557 3 1114 2225 3336 4447 5558 4 1115 2226 3337 4448 5559 5 1116 2227 3338 4449 5560 6 1117 2228 3339 4450 5561 7 1118 2229 3340 4451 5562 8 1119 2230 3341 4452 5563 9 1120 2231 3342 4453 5564 10 1121 2232 3343 4454 5565 11 1122 2233 3344 4455 55...

result:

ok 971 lines