Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions algorithmic/problems/263/chk.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

static inline long double clamp01(long double x) {
if (x < 0) return 0;
if (x > 1) return 1;
return x;
}

int main(int argc, char** argv) {
registerTestlibCmd(argc, argv);

int n = inf.readInt(3, 4000, "n");
int m = inf.readInt(1, 2000000, "m");
int Dlim = inf.readInt(0, min(m, 200000), "D");

vector<int> U(m + 1), V(m + 1);
vector<int> W(m + 1);
vector<int64_t> X(m + 1);

long double L_base = 0.0L;

for (int k = 1; k <= m; k++) {
int u = inf.readInt(1, n, ("u" + to_string(k)).c_str());
int v = inf.readInt(1, n, ("v" + to_string(k)).c_str());
if (u == v) quitf(_fail, "Input invalid: u==v at k=%d", k);

int64_t x = inf.readLong(1LL, 1000000000LL, ("x" + to_string(k)).c_str());
int w = inf.readInt(1, 1000, ("w" + to_string(k)).c_str());

U[k] = u; V[k] = v; X[k] = x; W[k] = w;

// Baseline is a[i]=1, no discards: p=1
// e = w * |1 - x| / x = w * (x-1)/x since x>=1
L_base += (long double)w * ((long double)(x - 1) / (long double)x);
}

// Read participant output
vector<int64_t> A(n + 1);
for (int i = 1; i <= n; i++) {
A[i] = ouf.readLong(1LL, 1000000000LL, ("a" + to_string(i)).c_str());
}

int t = ouf.readInt(0, Dlim, "t");
vector<unsigned char> discard(m + 1, 0);

for (int i = 0; i < t; i++) {
int idx = ouf.readInt(1, m, ("idx" + to_string(i + 1)).c_str());
if (discard[idx]) {
quitf(_wa, "Duplicate discarded observation index: %d", idx);
}
discard[idx] = 1;
}

ouf.seekEof();

// Compute participant loss
long double L_sub = 0.0L;
for (int k = 1; k <= m; k++) {
if (discard[k]) continue;

uint64_t p = (uint64_t)A[U[k]] * (uint64_t)A[V[k]]; // up to 1e18
uint64_t x = (uint64_t)X[k];
uint64_t diff = (p >= x) ? (p - x) : (x - p);

long double ek = (long double)W[k] * ((long double)diff / (long double)x);
L_sub += ek;
}

long double unbounded_ratio = 0.0L;
if (L_base > 0) unbounded_ratio = (L_base - L_sub) / L_base;
long double ratio = clamp01(unbounded_ratio);

quitp((double)ratio,
"Ratio: %.12Lf RatioUnbounded: %.12Lf L_sub: %.12Lf L_base: %.12Lf discarded: %d",
ratio, unbounded_ratio, L_sub, L_base, t);
}
7 changes: 7 additions & 0 deletions algorithmic/problems/263/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
checker: chk.cc
memory: 256m
subtasks:
- n_cases: 10
score: 100
time: 10s
type: default
139 changes: 139 additions & 0 deletions algorithmic/problems/263/statement.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
```markdown
# Sasha’s Smudged Multiplication Poster (Optimization)

## Problem
To learn multiplication, Big Ben printed a gigantic poster of a multiplication table created from his favorite integers collected over CALICO's 5 year history. Unfortunately the poster was damaged and now he's very sad :( Many entries were lost, and many others were corrupted. Big Ben now has only a vague memory of his favorite off diagonal cells.

Big Ben is working on a new multiplication table. He doesn't expect a perfect recreation of his old one, and he's happy tolerating a few different entries. Help Big Ben pick numbers for his new multiplication table!

Your task is to construct a list of $\var{N}$ integers $a_1, a_2, \dots, a_{\var{N}}$, which defines a $\var{N} \times \var{N}$ multiplication table where the entry at row $i$ and column $j$ is $a_i a_j$.

You are given $\var{M}$ constraints, each consisting of:
\begin{itemize}
\item A row index $\var{R}_i$ in the list $\var{R}_1, \var{R}_2, \dots, \var{R}_{\var{M}}$ and a column index $\var{C}_i$ in the list $\var{C}_1, \var{C}_2, \dots, \var{C}_{\var{M}}$
\begin{itemize}
\item Row and indices are guaranteed to be off-diagonal, $\var{R}_i \ne \var{C}_i$.
\end{itemize}
\item A value $\var{V}_i$ in the list $\var{V}_1, \var{V}_2, \dots, \var{V}_{\var{M}}$
\item A weight $\var{W}_i$ in the list $\var{W}_1, \var{W}_2, \dots, \var{W}_{\var{M}}$
\end{itemize}

Additionally, you may select a subset $S \subseteq \{1,2,\dots,\var{M}\}$ of $d$ constraints to discard, up to $\var{D}$.

\subsection{Input Format}

Each test file describes a single test case, which contains two lines:
\begin{itemize}
\item The first line of the input contains three space-separated integers $\var{N}$ $\var{M}$ $\var{D}$ denoting the size of the table, the number of observed cells, and the number of observations we can discard, respectively.
\item The next $\var{M}$ lines each describe a constraint and each contain four space-separated integers $\var{R_i}$ $\var{C_i}$ $\var{V_i}$ $\var{W_i}$ denoting the row index, column index, value, and weight respectively.
\begin{itemize}
\item The given constraints are 1-indexed: the first constraint contains $\var{R_1}$, the second contains $\var{R_2}$, and so on.
\end{itemize}
\end{itemize}

Note that multiple constraints may have the same $\var{R_i} = \var{R_j}$ and $\var{C_i} = \var{C_j}$. In these cases, both contribute to the penalty.

\subsection*{Output Format}
For the single test case in each test file, output two lines:
\begin{itemize}
\item The first line should contain $\var{N}$ space separated integers, $a_0$ $a_1$ $\dots$ $a_\var{N}$
\item The second line should contain $d+1$ space separated integers denoting the number of discarded observations followed by the discarded observations themselves, $d$ $S_1$ $S_2$ $\dots$ $S_d$
\end{itemize}

## Objective
For each observation `k` not discarded, let:

- `p_k = a_{uk} · a_{vk}` (computed in exact integer arithmetic; note `p_k` can be up to `10^18`)
- **weighted relative error**
\[
e_k = w_k \cdot \frac{|p_k - x_k|}{x_k}
\]

Your submission’s **loss** is:
\[
L = \sum_{k \notin \text{discarded}} e_k
\]

You must **minimize** `L`.

---

## Scoring
Let:

- `L_sub` be your loss.
- `L_base` be the **baseline loss**, defined deterministically as the loss obtained by:
- outputting `ai = 1` for all `i`,
- discarding `t = 0` observations.

The per-instance score is:
\[
S = 10^6 \cdot \text{clamp}\left(\frac{L_{base} - L_{sub}}{L_{base} - L_{ref}},\ 0,\ 1\right)
\]
where `clamp(z,0,1) = min(1, max(0, z))`.

Notes:
- `L_ref` is provided in the input and satisfies `0 < L_ref < L_base`.
- Achieving `L_sub = L_ref` gives full score `1,000,000`.
- Doing worse than baseline gives score `0`.
- The final contest score is the **sum of S** over all test instances.

All computations for judging use high-precision floating point; ties are broken by smaller `L_sub`.

---

Time Limit: \textbf{10 Seconds}

\subsubsection{Input Constraints}

$1 \leq \var{N} \leq 4 \cdot 10^{3}$ \\
$1 \leq \var{M} \leq 2 \cdot 10^6$ \\
$0 \leq \var{D} \leq \var{M}$

$1 \leq \var{R_i}, \var{C_i} \leq \var{N} \\
\var{R_i} \neq \var{C_i}$ \\
$1 \leq \var{V_i} \leq 10^9$ \\
$1 \leq \var{W_i} \leq 10^{3}$

Note that multiple constraints may have the same $\var{R_i} = \var{R_j}$ and $\var{C_i} = \var{C_j}$. In these cases, both contribute to the penalty.

\subsubsection{Output Constraints}

$1 \leq a_i \leq 10^9$ \\
$0 \leq d \leq \var{D}$ \\
$1 \leq S_i \leq \var{M}$

(Your program must be fast, but exact optimization is not expected; heuristics are essential.)

---

## Example
### Sample input
```
4 5 1
120.0
1 2 6 5
1 3 9 5
2 3 18 2
2 4 7 1
3 4 12 1
```

### Sample output
```
3 2 6 2
1 4
```

### Explanation (high level)
- Proposed array: `a = [3,2,6,2]`.
- Discard observation `#4` (at most `D=1` allowed).

Loss is computed over observations {1,2,3,5}:
- (1,2): predicted 3·2=6, matches x=6 → error 0
- (1,3): 3·6=18 vs 9 → contributes `5*|18-9|/9 = 5`
- (2,3): 2·6=12 vs 18 → contributes `2*|12-18|/18 = 0.666...`
- (3,4): 6·2=12 vs 12 → error 0

Total `L_sub = 5.666...`. The judge computes `L_base` from all-ones with no discards, then applies the scoring formula using the given `L_ref`.
```
1 change: 1 addition & 0 deletions algorithmic/problems/263/testdata/1.ans
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6600.998934
46 changes: 46 additions & 0 deletions algorithmic/problems/263/testdata/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
10 45 3
1 2 300004143 240
1 3 332569042 193
4 1 217766973 115
1 5 106040737 113
6 1 2909319 259
7 1 202565065 262
1 8 15179986 217
9 1 1 16
10 1 369101630 70
2 3 550908092 90
2 4 383499942 116
5 2 199082994 165
2 6 4841610 273
2 7 381267817 211
2 8 26928942 95
2 9 128351708 160
2 10 600290268 184
3 4 412160510 151
5 3 211753121 161
6 3 5361067 130
3 7 426059007 105
3 8 24506646 293
3 9 129318641 142
10 3 590640324 141
4 5 151542467 138
6 4 3846393 124
7 4 285964884 221
8 4 18770834 91
9 4 90257774 269
10 4 408250772 241
5 6 1840646 282
5 7 143965739 94
5 8 9190095 179
9 5 44241116 209
10 5 246333246 210
7 6 3461894 184
8 6 222199 122
6 9 1161362 178
10 6 5354102 62
8 7 17353159 270
7 9 96515697 132
10 7 411854690 137
8 9 5624573 86
10 8 28279975 299
9 10 136814745 61
1 change: 1 addition & 0 deletions algorithmic/problems/263/testdata/10.ans
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
57340392.142855
Loading
Loading