My submission gives WA without using cout<<fixed<<setprecision
. Since the median needs atmost 1 decimal place of precision, this feels weird. Any explanations?
Code which will give WA on removing fixedprecision
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
template <class X, class Y>
ostream &operator<<(ostream &os, pair<X, Y> const &p)
{
return os << "(" << p.first << ", " << p.second << ") ";
}
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr> &operator<<(basic_ostream<Ch, Tr> &os, Container const &x)
{
os << "[ ";
for (auto &y : x)
os << y << ", ";
return os << "]\n";
}
#define int long long
#define len(a) (int)a.size()
const long long INF = 1e18;
const double EPS = 1e-9;
const int di[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dj[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// dp?, graph?, bs on answer?, compress/sort queries?, stupid observation?
int solve_case()
{
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
double ans = 0;
vector<int> rest;
sort(a.begin(), a.end());
for (int i = 0; i < n; i++)
{
if (i < n - m + 1)
rest.push_back(a[i]);
else
ans += a[i];
}
int k = len(rest);
ans += (rest[(k - 1) / 2] + rest[k / 2]) / 2.0;
cout << ans << '\n';
return 0;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
cout << fixed << setprecision(12);
for (int _ = 1; _ <= t; _++)
{
cout << "Case #" << _ << ": ";
solve_case();
}
return 0;
}
if the length ofrest
is odd than you have to add middle elementThe expression
(rest[(k - 1) / 2] + rest[k / 2]) / 2.0
gives the median regardless. Give it a try.Oh actually you are getting WA because you didn't added the case where M == N. the rest array will be emptyThe rest vector will still have 1 element in it in that case as i<(n-m+1) so i<1 in the for loop while inserting elements in rest
i don't understood than why this code got AC
Faced the same issue.
I think it's because your code doesn't always print decimal point(
.
). See section "How does Kick Start handle real numbers? "i also faced same issue
bad question
Okay, you have put the code with setprecision included, this may cause some confusion I think. If you have a piece of code giving WA, i think it d be better to put the version that gives WA to not cause confusion.
About WA, this may be because of scientific notation. Without setprecision if the answer is 10^9 it ll output "1e+09", instead of the desired 1000000000 because its type is double.
Sorry for the confusion. Thanks for the explanation.
Auto comment: topic has been updated by justaninactivealt (previous revision, new revision, compare).
Without
fixed
in C++ your program often prints results in scientific notation:The first one differs from the expected answer by 211 (123457000-123456789).