제출 #853010

#제출 시각아이디문제언어결과실행 시간메모리
853010BenmathHomecoming (BOI18_homecoming)C++14
13 / 100
1054 ms21820 KiB
//#include<homecoming.h> #include<bits/stdc++.h> using namespace std; long long solve(int N, int K, int *A, int *B){ int n = N; int k = K; long long int a[n]; long long int b[n]; long long int dp[n]; long long int dp1[n]; long long int sum1 = 0; for(int i = 0; i < n; i++){ a[i] = A[i]; b[i] = B[i]; sum1 = sum1 + a[i] - b[i]; dp[i] = 0; dp1[i] = -1e18; } long long int ans = 0; ans = max (ans, sum1); // ako ne uzimamo prvi element for(int i = 0; i < n; i++){ if (i == 0){ long long int sum = 0; for (int j = i; j < (i + k); j++){ sum = sum + b[j % n]; } dp1[i] = a[i] - sum; // dp[i] = max(dp[i], dp1[i]); dp[i] = 0; dp1[i] = -1e18; }else{ dp[i] = dp[i-1]; long long int sum = 0; for (int j = i; j < (i + k); j++){ sum = sum + b[j % n]; } sum = a[i] - sum; if (i >= k){ dp1[i] = sum + dp[i-k]; } dp1[i] = max(dp1[i], dp1[i-1] + a[i] - b[(i + k - 1) % n] ); dp[i] = max(dp[i], dp1[i]); } } ans = max(ans, dp[n-1]); // ako uzimamo prvi element int vis[n]; for (int i = 0; i < n; i++){ dp[i] = -1e18; dp1[i] = -1e18; vis[i] = 0; } for (int i = 0; i < n; i++){ if (i == 0){ long long int sum = 0; for (int j = i; j < (i + k); j++){ sum = sum + b[j % n]; vis[j] ++; } dp1[i] = a[i] - sum; dp[i] = dp1[i]; }else{ if (i >= k){ dp[i] = dp[i-1]; long long int sum = 0; for (int j = i; j < (i + k); j++){ sum = sum + b[j % n]; } sum = a[i] - sum; dp1[i] = sum + dp[i-k]; long long int ro = a[i]; if(vis[(i + k - 1) % n] == 0){ ro = ro - b[(i + k - 1) % n]; } dp1[i] = max(dp1[i], dp1[i-1] + ro); dp[i] = max(dp[i], dp1[i]); sum = 0; for (int j = i; j <= (n-1); j++){ sum = sum + a[j] - b[j]; } ans = max(ans, sum + dp[i-k]); }else{ dp[i] = dp[i-1]; long long int ro = a[i]; if(vis[(i + k - 1) % n] == 0){ ro = ro - b[(i + k - 1) % n]; } dp1[i] = dp1[i-1] + ro; dp[i] = max(dp[i], dp1[i]); } } } ans = max(ans, dp[n-1]); return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...