Max_Leb

Untitled

Apr 1st, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void swap(int *array, int a, int b){
  7.     int tmp = array[b];
  8.     array[b] = array[a];
  9.     array[a] = tmp;
  10. }
  11.  
  12. void InsertionSort(int *array, int n){
  13.     for (int i = 0; i < n; ++i){
  14.         int j = i - 1;
  15.         while (j >= 0 and array[j] < array[j+1]){
  16.             swap(array, j, (j+1));
  17.             j--;
  18.         }
  19.     }
  20. }
  21.  
  22. int main() {
  23.     ifstream fin("input.txt");
  24.     ofstream fout("output.txt");
  25.     int N;
  26.     fin >> N;
  27.     int k;
  28.     string str;
  29.     int one_plus_zero[N];
  30.     int zero_plus_one[N];
  31.     for(int i = 0 ; i < N; ++i){
  32.         one_plus_zero[i]=0;
  33.         zero_plus_one[i]=0;
  34.     }
  35.     int tlen1=0;
  36.     int tlen2=0;
  37.     int sum1=0, sum0 = 0;
  38.     int answer=0;
  39.     for(int i = 0; i < N; ++i){
  40.         fin >> k >> str;
  41.         if (str[0] == '1' and str[k-1] == '0'){
  42.             one_plus_zero[tlen1]=k;
  43.             tlen1++;
  44.         }
  45.         else if(str[0] == '0' and str[k-1] == '1'){
  46.             zero_plus_one[tlen2]=k;
  47.             tlen2++;
  48.         }
  49.         else if (str[0] == '1' and str[k-1] == '1') {
  50.             sum1 += k;
  51.         }
  52.         else
  53.             sum0 += k;
  54.     }
  55.  
  56.     if (tlen1 == 0 && tlen2 == 0){
  57.         answer = sum1 > sum0 ? sum1 : sum0;
  58.         std::cout << answer << std::endl;
  59.         return 0;
  60.     }
  61.  
  62.     InsertionSort(one_plus_zero, tlen1);
  63.     InsertionSort(zero_plus_one, tlen2);
  64.  
  65.     answer+=sum1;
  66.     if(tlen1>tlen2){
  67.         int index = 0;
  68.         while(tlen2 != 0){
  69.             answer+=one_plus_zero[index]+zero_plus_one[index];
  70.             tlen2--;
  71.             index++;
  72.         }
  73.         answer+=one_plus_zero[index];
  74.     }
  75.     else if(tlen1<tlen2){
  76.         int index = 0;
  77.         while(tlen1 != 0){
  78.             answer+=one_plus_zero[index]+zero_plus_one[index];
  79.             tlen1--;
  80.             index++;
  81.         }
  82.         answer+=zero_plus_one[index];
  83.     }
  84.     else{
  85.         for(int i = 0; i < tlen1; ++i){
  86.             answer+=one_plus_zero[i]+zero_plus_one[i];
  87.         }
  88.     }
  89.     fout << answer;
  90.     fin.close();
  91.     fout.close();
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment