Guest User

Untitled

a guest
Dec 13th, 2015
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <map>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <cmath>
  8. #include <cstring>
  9. #include <vector>
  10. #include <algorithm>
  11.  
  12. using namespace std;
  13.  
  14. const int maxn = (1e5) + 1;
  15. int dp[maxn];
  16. int pr[maxn];
  17. int a[maxn];
  18. map <int, int> mp;
  19. vector <pair <int, int> > v;
  20.  
  21. int main()
  22. {
  23.     freopen("parties.in", "r", stdin);
  24.     freopen("parties.out", "w", stdout);
  25.     int n;
  26.    
  27.     scanf("%d", &n);
  28.     clock_t t = clock();
  29.     for (int i = 0; i < n; i++)
  30.     {
  31.         int k;
  32.         scanf("%d", &k);
  33.         a[i] = k;
  34.         mp[k + 1]++;
  35.     }
  36.    
  37.     //O(sqrt(n))
  38.     for (auto it = mp.begin(); it != mp.end(); it++) //mp size can not be more than sqrt(n)
  39.         v.push_back(*it);
  40.  
  41.     for (int i = 1; i <= n; i++)
  42.         dp[i] = -1;
  43.  
  44.     for (int z = 0; z < v.size(); z++)
  45.     {
  46.         int len = v[z].first;
  47.         int cnt = v[z].second;
  48.         for (int x = 0; x + v[z].first <= n; x++)
  49.         {
  50.             int y = x + len;
  51.             if (dp[x] != -1 && dp[y] == -1)
  52.             {
  53.                 if (pr[x] != len)
  54.                     dp[y] = 1, pr[y] = len;
  55.                 else
  56.                     if (dp[x] < cnt)
  57.                         dp[y] = dp[x] + 1, pr[y] = len;
  58.             }
  59.         }
  60.     }
  61.    
  62.     if (dp[n] == -1)
  63.         puts("NO");
  64.     else
  65.     {
  66.         multiset <int> ans;
  67.         puts("YES");
  68.         int cur = n;
  69.         while (cur > 0)
  70.         {
  71.             ans.insert(pr[cur]);
  72.             cur = cur - pr[cur];
  73.         }
  74.         for (int i = 0; i < n; i++)
  75.         {
  76.             auto it = ans.lower_bound(a[i] + 1);
  77.             if (it == ans.end() || *it != (a[i] + 1))
  78.                 printf("2 ");
  79.             else
  80.                 printf("1 "), ans.erase(it);
  81.         }
  82.     }
  83.  
  84.     cerr << float(clock() - t) / CLOCKS_PER_SEC;
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment