Advertisement
tamionv

Untitled

Jan 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <iostream>
  4. #include <deque>
  5. using namespace std;
  6.  
  7. using ll = long long;
  8.  
  9. constexpr ll dist(const ll a, const ll b){
  10.     return (a > b) ? a-b : b-a; }
  11.  
  12. ll suma_secv(const vector<ll>& s_part, const ll a, const ll b){
  13.     return s_part[b+1] - s_part[a]; }
  14.  
  15. void citeste_date(vector<ll>& s_part, ll& k, ll& s, vector<ll>& rez_init){
  16.     ifstream f("secvbest.in");
  17.     ll n = 0;
  18.     f >> n >> k >> s;
  19.     s_part.resize(n+1, 0);
  20.     rez_init.resize(n, 0);
  21.     for(ll i = 1; i <= n; ++i){
  22.         f >> s_part[i];
  23.         s_part[i] += s_part[i-1];
  24.         rez_init[i-1] = dist(s, s_part[i]); } }
  25.  
  26. ll cost_asociat(const vector<ll>& rez_cur, const vector<ll>& s_part, const ll x, const ll i, const ll s){
  27.     return rez_cur[x-1] + dist(suma_secv(s_part, x, i), s); }
  28.  
  29. void fa_rand(const vector<ll>& s_part,
  30.     const vector<ll>& rez_cur, const ll s,
  31.     const ll k, vector<ll>& rez_next){
  32.  
  33.     ll optim_stang = k-1;
  34.     rez_next[k-1] = cost_asociat(rez_cur, s_part, k-1, k-1, s);
  35.  
  36.     deque<ll> dreapta;
  37.  
  38.     for(ll i = k, gr = k; i < rez_cur.size(); ++i){
  39.         while((!dreapta.empty()) &&
  40.             cost_asociat(rez_cur, s_part, dreapta.back(), i, s) >
  41.             cost_asociat(rez_cur, s_part, i, i, s)){
  42.             dreapta.pop_back(); }
  43.         dreapta.push_back(i);
  44.  
  45.         while(suma_secv(s_part, gr, i) > s){
  46.             if(cost_asociat(rez_cur, s_part, optim_stang, i, s) >
  47.             cost_asociat(rez_cur, s_part, gr, i, s)){
  48.                 optim_stang = gr; }
  49.             if((!dreapta.empty()) && dreapta.front() == gr){
  50.                 dreapta.pop_front(); }
  51.             ++gr; }
  52.         rez_next[i] = min(cost_asociat(rez_cur, s_part, dreapta.front(), i, s),
  53.             cost_asociat(rez_cur, s_part, optim_stang, i, s)); } }
  54.  
  55. int main(){
  56.     ll s, k;
  57.     vector<ll> s_part, rez_cur;
  58.     citeste_date(s_part, k, s, rez_cur);
  59.     vector<ll> rez_next(rez_cur.size());
  60.     ofstream g("secvbest.out");
  61.     g << rez_cur.back() << ' ';
  62.     for(ll i = 2; i <= k; ++i){
  63.         fa_rand(s_part, rez_cur, s, i, rez_next);
  64.         g << rez_next.back() << ' ';
  65.         swap(rez_cur, rez_next); }
  66.     return 0; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement