/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 532.0 KiB
#2 Accepted 2ms 320.0 KiB
#3 Accepted 483ms 664.0 KiB
#4 Accepted 372ms 676.0 KiB
#5 Accepted 629ms 604.0 KiB
#6 Accepted 149ms 624.0 KiB
#7 Accepted 167ms 632.0 KiB
#8 Accepted 250ms 676.0 KiB
#9 Accepted 290ms 692.0 KiB
#10 Accepted 82ms 616.0 KiB
#11 Accepted 34ms 592.0 KiB
#12 Time Exceeded ≥1093ms ≥580.0 KiB
#13 Time Exceeded ≥1096ms ≥496.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=2e4+10,MOD=1000000007;
typedef long long ll;
int p[M];
int r[M];
vector<ll>vec[M],val[M];
int find(int x){
    if(p[x]==x)return x;
    else return find(p[x]);
}
void combine(int x,int y){
int a=find(x);
int b=find(y);
if(a==b)return;
if(r[a]>r[b]){
    r[a]+=r[b];
    p[b]=a;
}
else{
    r[b]+=r[a];
    p[a]=b;
}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    while(t--){
      int n,k;
      cin>>n>>k;
      vector<ll>a(n+1);
      for(int i=1;i<=n;i++)cin>>a[i];
      for(int i=1;i<=n;i++)p[i]=i,r[i]=1;
      for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            int gd=__gcd(a[i],a[j]);
            if(gd>1){
                combine(i,j);
            }
        }
      }
      for(int i=1;i<=n;i++){
        int x=find(i);
        vec[x].push_back(i);
      }
      for(int i=1;i<=n;i++){
        vector<ll>v;
        for(auto it:vec[i]){
            v.push_back(a[it]);
            //cout<<it<<" ";
        }
        sort(v.rbegin(),v.rend());
        for(auto it:v)val[i].push_back(it);
           // cout<<endl;

      }

     ll mx=0;
     for(int i=1;i<=n;i++){
        int l=i;
        int r=i+k-1;
        int cnt=0;
        ll sum=0;
      //  cout<<"index"<<i<<"\n";
        for(int j=1;j<=n;j++){
            cnt=0;
        for(auto it:vec[j]){
            if(it>=l && it<=r){
               // cout<<val[j][cnt]<<" ";
                sum+=val[j][cnt];
                cnt++;
            }
        }
       // cout<<d<<endl;
    }
       //cout<<d<<endl;
        mx=max(mx,sum);
     }
     cout<<mx<<"\n";
     for(int i=1;i<=n;i++){
        vec[i].clear();
        val[i].clear();
     }

     

    }




    
   
   return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1063 Another Maximum Sum in Subarray
Language
C++20 (G++ 13.2.0)
Submit At
2024-05-24 09:19:50
Judged At
2024-11-11 03:29:35
Judged By
Score
65
Total Time
≥1096ms
Peak Memory
≥692.0 KiB