L Grayscale Confusion
思路:题解 | #Grayscale Confusion#
大概相当于是一个构造题,将(r, g, b)映射为函数值,满足大小关系以及最初两组(c0和c1)rgb的值相等
f(r, g, b) = x*r + y*g + z*b
x + y + z = 1
- c0和c1有大小,无法满足,输出-1
- c0和c1有一个相等,假设r0r1相等,就令
x = 1, y = 0, z = 0
- c0和c1有交错,假设是
r0 > r1, g0 < g1
,令
则p = r0 - r1 q = g1 - g0 x = q / (p + q) y = p / (p + q)
f(c0) - f(c1) = q / (p + q) * q - p / (p + q) * p = 0
看懂了之后自己打了一遍
尤其注意的是公式得到的值是double,最后不能直接取整,要用lround
(因此得过一次93.3333的高分)
lround(double)
,将浮点值舍入为最接近的整数
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <map>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
#define N 1005
int c[N][3];
double f[3];
bool bigger(int x, int y)
{
if (c[x][0] > c[y][0] && c[x][1] > c[y][1] && c[x][2] > c[y][2]) return true;
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> c[i][0] >> c[i][1] >> c[i][2];
}
if (bigger(0, 1) || bigger(1, 0))
{
cout << -1 << endl;
return 0;
}
f[0] = f[1] = f[2] = 0;
int fl = 0;
for (int i = 0; i < 3; i++)
{
if (c[0][i] == c[1][i])
{
f[i] = 1;
fl = 1;
break;
}
}
if (!fl)
{
int x = 0, y = 0;
for (int i = 0; i < 3; i++)
{
if (c[0][i] > c[1][i]) x = i;
else y = i;
}
double p = c[0][x] - c[1][x], q = c[1][y] - c[0][y];
f[x] = q / (p + q);
f[y] = p / (p + q);
}
for (int i = 0; i < n; i++)
{
double res = 0;
for (int j = 0; j < 3; j++)
{
res += f[j] * c[i][j];
}
cout << lround(res) << endl;
}
return 0;
}