CSES - Datatähti 2025 alku - Results
Submission details
Task:Tikut
Sender:Verlet
Submission time:2024-11-10 12:46:57 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void cut(std::vector<std::tuple<double, int, int> >&, int)':
input/code.cpp:22:7: error: conflicting declaration 'int cutval'
   22 |   int cutval = max(ceil (cutval), ceil (get<0>(t[1  ])));
      |       ^~~~~~
input/code.cpp:20:10: note: previous declaration as 'double cutval'
   20 |   double cutval = (double) get<1>(t[0]) / (get<2>(t[0]) + 1);
      |          ^~~~~~
input/code.cpp:23:7: error: conflicting declaration 'int cutval'
   23 |   int cutval = min(floor(cutval), floor(get<0>(t[n-1])));
      |       ^~~~~~
input/code.cpp:20:10: note: previous declaration as 'double cutval'
   20 |   double cutval = (double) get<1>(t[0]) / (get<2>(t[0]) + 1);
      |          ^~~~~~
input/code.cpp:17:7: warning: unused variable 'initialDifference' [-Wunused-variable]
   17 |   int initialDifference = initialMax - initialMin;
      |       ^~~~~~~~~~~~~~~~~

Code

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

typedef tuple<double, int, int> stick;

void cut(vector<stick> & t, int n)
{
  sort(t.rbegin(), t.rend());
  
  int initialMax = ceil (get<0>(t[0  ]));
  int initialMin = floor(get<0>(t[n-1]));
  
  int initialDifference = initialMax - initialMin;

  // Try the longest stick
  double cutval = (double) get<1>(t[0]) / (get<2>(t[0]) + 1);

  int cutval = max(ceil (cutval), ceil (get<0>(t[1  ])));
  int cutval = min(floor(cutval), floor(get<0>(t[n-1])));

  int cutDifference = cutval - cutval;
  
  int cutIndex = 0;

  // Longest stick increased the difference.
  // Try cutting one of the other sticks such that the difference remains constant
  for (int i = 1; i < n; i++)
  {
    double newval = (double) get<1>(t[i]) / (get<2>(t[i]) + 1);

    int newmax = max((int) ceil (newval), initialMax);
    int newmin = min((int) floor(newval), initialMin);

    int newDifference = newmax - newmin;

    if (newDifference < cutDifference)
    {
      cutDifference = newDifference;
      cutval = newval;
      cutIndex = i;
    }
  }

  cout << cutDifference << " ";
  get<0>(t[cutIndex]) = cutval;
  get<2>(t[cutIndex])++;
}

int main()
{
  int n, m;

  cin >> n >> m;

  vector<stick> t;

  for (int i = 0; i < n; i++)
  {
    int l;

    cin >> l;

    t.push_back(make_tuple(l, l, 1));
  }

  for (int i = 1; i <= m; i++)
  {
    cut(t, n);
  }
}