【2023牛客多校训练营10】L Grayscale Confusion

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值