답안 #965214

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965214 2024-04-18T08:20:16 Z vjudge1 Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
1662 ms 170844 KB
#include <bits/stdc++.h>

#pragma optimize("Ofast")
#pragma target("avx2")

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define y1 yy
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b) __gcd(a,b)
#define in insert
// #define int ll
#define ull unsigned ll

const int MAX=2500+15;
const int B=331;
const int maxB=1000;
const int N=104;
const int block=450;
const ll inf=1e13;  
const int mod=1e9+7;
const int mod1=1e9+9;
const ld eps=1e-9;

int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,-1,1,-1};

int binpow(int a,int n){
  if(!n)return 1;
  if(n%2==1)return a*binpow(a,n-1)%mod;
  int k=binpow(a,n/2);
  return k*k%mod;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n,a,b,c;
string s;

const int L=3;

struct hash{
  ll mod,C;
  ll h[MAX],p[MAX];

  void init(int module,int constant,string s){
    mod=module;
    C=constant;
    p[0]=1;
    for(int i=1;i<sz(s);i++){
      h[i]=(h[i-1]*1ll*C+s[i])%mod;
      p[i]=p[i-1]*1ll*C%mod;
    }
  }

  int get(int l,int r){
    return (h[r]-h[l-1]*1ll*p[r-l+1]%mod+mod)%mod;
  }

}H[L];

struct pair_hash {
    template <class T1, class T2>
    std::size_t operator () (const std::pair<T1,T2> &p) const {
        auto h1 = std::hash<T1>{}(p.first);
        auto h2 = std::hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;  
    }
};

ll dp[MAX][MAX];
int last[MAX][MAX];
unordered_map<pii,int,pair_hash> was;

struct segtree{
  ll t[4*MAX];

  int p[MAX];

  void build(int v,int tl,int tr){
    t[v]=inf;
    if(tl==tr){
      p[tl]=v;
      return;
    }
    int tm=(tl+tr)/2;
    build(2*v,tl,tm);
    build(2*v+1,tm+1,tr);
  }

  void update(int pos,ll x){
    int v=p[pos];
    t[v]=x;  
    
    v/=2;   

    while(v){

      t[v]=min(t[2*v],t[2*v+1]);
      v/=2;  
    }
  }
}t;

void solve(){
  cin>>n>>s; 
  cin>>a>>b>>c;
  s="#"+s;
  H[0].init(1e9+7,331,s);
  H[1].init(1e9+9,331,s);
  H[2].init(998244353,331,s);

  for(int zs=1;zs<=n;zs++){
    for(int r=zs;r<=n;r++){
      int R=r-zs;
      int L=R-zs+1;
      if(1<=L){
        was[{H[0].get(L,R),H[1].get(L,R)}]=R;
      }

      int l=r-zs+1;
      if(was.count({H[0].get(l,r),H[1].get(l,r)})){
        last[l][r]=was[{H[0].get(l,r),H[1].get(l,r)}];
      }

    }
  }

  for(int i=1;i<=n;i++)dp[i][i]=a;

  for(int r=2;r<=n;r++){
    t.build(1,1,r);
    vector<int> upd[r];
    vector<int> cnt(r,0);
    vector<ll> cost(r,0);
    for(int l=r-1;l>=1;l--){
      cost[r-l]=dp[l+1][r]+c-a*1ll*(r-l);
      
      t.update(r-l,cost[r-l]);

      if(last[l+1][r]-(r-l)+1>0)upd[last[l+1][r]-(r-l)+1].pb(r-l);
      
      for(int zs:upd[l]){
        
        cost[zs]+=(c-a*1ll*zs);
        
        t.update(zs,cost[zs]);
        
        if(last[l][l+zs-1]-zs+1>0)upd[last[l][l+zs-1]-zs+1].pb(zs);
      }

      dp[l][r]=min(dp[l][r-1]+a,t.t[1]+(r-l+1)*1ll*a+b);
    }
  }

  // for(int zs=1;zs<=n;zs++){
  //   for(int l=1;l+zs-1<=n;l++){
  //     cout<<dp[l][l+zs-1]<<" ";
  //   }
  //   cout<<"\n";
  // }

  cout<<dp[1][n]<<"\n";

}

signed main(){
  // freopen("triangles.in","r",stdin);
  // freopen("triangles.out","w",stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // prec();
  int t=1;
  // cin>>t;
  while(t--)solve();
}

Compilation message

copypaste3.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
    3 | #pragma optimize("Ofast")
      | 
copypaste3.cpp:4: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
    4 | #pragma target("avx2")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2520 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2516 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 785 ms 56472 KB Output is correct
4 Correct 988 ms 60604 KB Output is correct
5 Correct 1173 ms 66476 KB Output is correct
6 Correct 1330 ms 72612 KB Output is correct
7 Correct 1662 ms 74996 KB Output is correct
8 Correct 1554 ms 75032 KB Output is correct
9 Correct 1547 ms 75076 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2520 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2516 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4440 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4632 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4640 KB Output is correct
27 Correct 1 ms 4620 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4572 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 1 ms 2392 KB Output is correct
36 Correct 1 ms 2520 KB Output is correct
37 Correct 1 ms 2524 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2520 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2516 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4440 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4632 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4640 KB Output is correct
27 Correct 1 ms 4620 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4572 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 1 ms 2392 KB Output is correct
36 Correct 1 ms 2520 KB Output is correct
37 Correct 1 ms 2524 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4444 KB Output is correct
40 Correct 2 ms 4700 KB Output is correct
41 Correct 6 ms 6624 KB Output is correct
42 Correct 5 ms 7000 KB Output is correct
43 Correct 6 ms 7004 KB Output is correct
44 Correct 4 ms 7184 KB Output is correct
45 Correct 5 ms 7004 KB Output is correct
46 Correct 5 ms 7000 KB Output is correct
47 Correct 5 ms 6744 KB Output is correct
48 Correct 5 ms 7256 KB Output is correct
49 Correct 5 ms 7000 KB Output is correct
50 Correct 5 ms 7004 KB Output is correct
51 Correct 5 ms 6748 KB Output is correct
52 Correct 6 ms 6928 KB Output is correct
53 Correct 5 ms 7136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2520 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2516 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4440 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4632 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4640 KB Output is correct
27 Correct 1 ms 4620 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4572 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 1 ms 2392 KB Output is correct
36 Correct 1 ms 2520 KB Output is correct
37 Correct 1 ms 2524 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4444 KB Output is correct
40 Correct 2 ms 4700 KB Output is correct
41 Correct 6 ms 6624 KB Output is correct
42 Correct 5 ms 7000 KB Output is correct
43 Correct 6 ms 7004 KB Output is correct
44 Correct 4 ms 7184 KB Output is correct
45 Correct 5 ms 7004 KB Output is correct
46 Correct 5 ms 7000 KB Output is correct
47 Correct 5 ms 6744 KB Output is correct
48 Correct 5 ms 7256 KB Output is correct
49 Correct 5 ms 7000 KB Output is correct
50 Correct 5 ms 7004 KB Output is correct
51 Correct 5 ms 6748 KB Output is correct
52 Correct 6 ms 6928 KB Output is correct
53 Correct 5 ms 7136 KB Output is correct
54 Correct 23 ms 19820 KB Output is correct
55 Correct 217 ms 31572 KB Output is correct
56 Correct 140 ms 45524 KB Output is correct
57 Correct 118 ms 45632 KB Output is correct
58 Correct 121 ms 45676 KB Output is correct
59 Correct 123 ms 45752 KB Output is correct
60 Correct 124 ms 45740 KB Output is correct
61 Correct 106 ms 37572 KB Output is correct
62 Correct 187 ms 31484 KB Output is correct
63 Correct 103 ms 39876 KB Output is correct
64 Correct 123 ms 39596 KB Output is correct
65 Correct 151 ms 35932 KB Output is correct
66 Correct 155 ms 36116 KB Output is correct
67 Correct 146 ms 45696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2520 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2516 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 785 ms 56472 KB Output is correct
14 Correct 988 ms 60604 KB Output is correct
15 Correct 1173 ms 66476 KB Output is correct
16 Correct 1330 ms 72612 KB Output is correct
17 Correct 1662 ms 74996 KB Output is correct
18 Correct 1554 ms 75032 KB Output is correct
19 Correct 1547 ms 75076 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 4440 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4440 KB Output is correct
34 Correct 0 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 1 ms 2396 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 4632 KB Output is correct
40 Correct 1 ms 4444 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4444 KB Output is correct
43 Correct 1 ms 4640 KB Output is correct
44 Correct 1 ms 4620 KB Output is correct
45 Correct 1 ms 4700 KB Output is correct
46 Correct 1 ms 4444 KB Output is correct
47 Correct 1 ms 4572 KB Output is correct
48 Correct 1 ms 4444 KB Output is correct
49 Correct 1 ms 4444 KB Output is correct
50 Correct 1 ms 4444 KB Output is correct
51 Correct 1 ms 2396 KB Output is correct
52 Correct 1 ms 2392 KB Output is correct
53 Correct 1 ms 2520 KB Output is correct
54 Correct 1 ms 2524 KB Output is correct
55 Correct 1 ms 4444 KB Output is correct
56 Correct 1 ms 4444 KB Output is correct
57 Correct 2 ms 4700 KB Output is correct
58 Correct 6 ms 6624 KB Output is correct
59 Correct 5 ms 7000 KB Output is correct
60 Correct 6 ms 7004 KB Output is correct
61 Correct 4 ms 7184 KB Output is correct
62 Correct 5 ms 7004 KB Output is correct
63 Correct 5 ms 7000 KB Output is correct
64 Correct 5 ms 6744 KB Output is correct
65 Correct 5 ms 7256 KB Output is correct
66 Correct 5 ms 7000 KB Output is correct
67 Correct 5 ms 7004 KB Output is correct
68 Correct 5 ms 6748 KB Output is correct
69 Correct 6 ms 6928 KB Output is correct
70 Correct 5 ms 7136 KB Output is correct
71 Correct 23 ms 19820 KB Output is correct
72 Correct 217 ms 31572 KB Output is correct
73 Correct 140 ms 45524 KB Output is correct
74 Correct 118 ms 45632 KB Output is correct
75 Correct 121 ms 45676 KB Output is correct
76 Correct 123 ms 45752 KB Output is correct
77 Correct 124 ms 45740 KB Output is correct
78 Correct 106 ms 37572 KB Output is correct
79 Correct 187 ms 31484 KB Output is correct
80 Correct 103 ms 39876 KB Output is correct
81 Correct 123 ms 39596 KB Output is correct
82 Correct 151 ms 35932 KB Output is correct
83 Correct 155 ms 36116 KB Output is correct
84 Correct 146 ms 45696 KB Output is correct
85 Correct 505 ms 84252 KB Output is correct
86 Correct 1621 ms 169856 KB Output is correct
87 Correct 1418 ms 170184 KB Output is correct
88 Correct 1444 ms 170436 KB Output is correct
89 Correct 1392 ms 170768 KB Output is correct
90 Correct 1455 ms 170844 KB Output is correct
91 Correct 886 ms 79040 KB Output is correct
92 Correct 1150 ms 76004 KB Output is correct
93 Correct 1065 ms 131672 KB Output is correct
94 Correct 1095 ms 129100 KB Output is correct
95 Correct 1277 ms 104584 KB Output is correct
96 Correct 1274 ms 104288 KB Output is correct
97 Correct 1396 ms 170788 KB Output is correct