Advertisement
Guest User

Untitled

a guest
Jan 29th, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <unordered_map>
  4. #include <vector>
  5. #include <utility>
  6. #include <unordered_set>
  7. using namespace std;
  8.  
  9. #define MAXN 100010
  10.  
  11. int hang(int N, int C, int S[]) {
  12.     //int pos_colori[C];
  13.     vector<pair<int, int>> pos_colori;
  14.     unordered_set<int> aggiunti;
  15.  
  16.     int numero_pile = 0;
  17.     int cambiamenti = 0;
  18.     for (int i = 0; i < N; i++) {
  19.     //printf("Cambiamenti %d\n", cambiamenti);
  20.         //printf("Cosetta %d\n", S[i]);
  21.         // se il colore non c'�
  22.         bool trovato = false;
  23.         int posizione = 0;
  24.         if (aggiunti.count(S[i]) > 0) {
  25.             for (auto x : pos_colori) {
  26.                 if (x.first == S[i]) {
  27.                     posizione = x.second;
  28.                     trovato = true;
  29.                     break;
  30.                 }
  31.             }
  32.         }
  33.         aggiunti.insert(S[i]);
  34.         if (!trovato) {
  35.             pos_colori.push_back({S[i], C-numero_pile-1});
  36.             cambiamenti += numero_pile;
  37.             numero_pile++;
  38.             //printf("Nuova pila %d in %d\n", S[i], pos_colori[S[i]]);
  39.         }
  40.         else {
  41.             //printf("Pila %d si trova in %d\n", S[i], pos_colori[S[i]]);
  42.             int differenza = C - 1 - posizione;
  43.             //printf("\tDifferenza %d\n", differenza);
  44.             cambiamenti += differenza;
  45.         }
  46.     }
  47.     return cambiamenti;
  48. }
  49.  
  50.  
  51. int S[MAXN];
  52.  
  53. int main() {
  54.     FILE *fr, *fw;
  55.     int N, C, i;
  56.  
  57.     fr = fopen("input.txt", "r");
  58.     fw = fopen("output.txt", "w");
  59.     assert(2 == fscanf(fr, "%d %d", &N, &C));
  60.     for(i=0; i<N; i++)
  61.         assert(1 == fscanf(fr, "%d", &S[i]));
  62.  
  63.     fprintf(fw, "%d\n", hang(N, C, S));
  64.     fclose(fr);
  65.     fclose(fw);
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement