poj 1046 Color Me Less

Color Me Less

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 33007   Accepted: 16050

Description

A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just such a mapping in a standard twenty-four bit RGB color space. The input consists of a target set of sixteen RGB color values, and a collection of arbitrary RGB colors to be mapped to their closest color in the target set. For our purposes, an RGB color is defined as an ordered triple (R,G,B) where each value of the triple is an integer from 0 to 255. The distance between two colors is defined as the Euclidean distance between two three-dimensional points. That is, given two colors (R1,G1,B1) and (R2,G2,B2), their distance D is given by the equation 

Input

The input is a list of RGB colors, one color per line, specified as three integers from 0 to 255 delimited by a single space. The first sixteen colors form the target set of colors to which the remaining colors will be mapped. The input is terminated by a line containing three -1 values.

Output

For each color to be mapped, output the color and its nearest color from the target set.

If there are more than one color with the same smallest distance, please output the color given first in the color set.

Sample Input

0 0 0
255 255 255
0 0 1
1 1 1
128 0 0
0 128 0
128 128 0
0 0 128
126 168 9
35 86 34
133 41 193
128 0 128
0 128 128
128 128 128
255 0 0
0 1 0
0 0 0
255 255 255
253 254 255
77 79 134
81 218 0
-1 -1 -1

Sample Output

(0,0,0) maps to (0,0,0)
(255,255,255) maps to (255,255,255)
(253,254,255) maps to (255,255,255)
(77,79,134) maps to (128,128,128)
(81,218,0) maps to (126,168,9)

Source

Greater New York 2001

先读懂题意

很简单,就是给定一个三维点以及一个由16个三维点组成的点集,求这个给定三维点与点集中哪个三维坐标点的距离最小。

基本思路:

枚举,就OK了。

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int R[17],G[17],B[17],r,g,b;
double cl(int r,int g,int b,int i){
    return sqrt((double)(r-R[i])*(r-R[i])+(double)(g-G[i])*(g-G[i])+(double)(b-B[i])*(b-B[i]));
}
int main(){
    for(int i=1;i<=16;i++){
        cin>>R[i]>>G[i]>>B[i];
    }
    while(scanf("%d%d%d",&r,&g,&b)==3&&r!=-1&&g!=-1&&b!=-1){
        int ans,flag=0,minn=0x3f3f3f;
        for(int i=1;i<=16;i++){
            ans=cl(r,g,b,i);
            if(ans<minn){
                minn=ans;
                flag=i;
            }
        }
        printf("(%d,%d,%d) maps to (%d,%d,%d)\n",r,g,b,R[flag],G[flag],B[flag]);
    }
    return 0;
}
时间: 2024-10-05 15:07:39

poj 1046 Color Me Less的相关文章

POJ 1046 Color Me Less 最详细的解题报告

题目来源:POJ 1046 Color Me Less 题目大意:每一个颜色由R.G.B三部分组成,D=Math.sqrt(Math.pow((left.red - right.red), 2)+ Math.pow((left.green - right.green), 2)+ Math.pow((left.blue - right.blue), 2)) 表示两个不同颜色的之间的距离(以left和right为例,left和right分别为两种不同的颜色),现给出16组目标颜色,剩下的为待匹配的颜

[ACM] POJ 1046 Color Me Less

Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30146   Accepted: 14634 Description A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just

POJ 1046 Color Me Less解题报告

Description A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just such a mapping in a standard twenty-four bit RGB color space. The input consists of a target set of

[ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7630   Accepted: 2507 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th

poj 2154 Color(polya计数 + 欧拉函数优化)

http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目,旋转后一样的属于同一种,结果模p. n个珠子应该有n种旋转置换,每种置换的循环个数为gcd(i,n).如果直接枚举i,显然不行.但是我们可以缩小枚举的数目.改为枚举每个循环节的长度L,那么相应的循环节数是n/L.所以我们只需求出每个L有多少个i满足gcd(i,n)= n/L,就得到了循环节数为n/L的个数.重点就是求出这样的i的个数. 令cnt = gcd(i,n) =

POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,数论-整数快速幂)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7693   Accepted: 2522 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th

poj 2154 Color 欧拉函数优化的ploya计数

枚举位移肯定超时,对于一个位移i,我们需要的是它的循环个数,也就是gcd(i,n),gcd(i,n)个数肯定不会很多,因为等价于n的约数的个数. 所以我们枚举n的约数,对于一个约数k,也就是循环个数为n/k这样的个数有phi[k]种,证明网上有很多.所以答案就是 phi[k]*(pow(n,n/k)) (k是n的所有约数) 由于约数会很大所以不能打表,只能单个算. 再由于最后要除以n,如果做除法就不能直接取模,所以我们在算每一次pow(n,n/k)的时候,都少乘一个n,这样就相当于除法了. #i

组合数学 - 波利亚定理 --- poj : 2154 Color

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7873   Accepted: 2565 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th

POJ 2777-Count Color(线段树_区间染色)

Count Color Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2777 Appoint description:  System Crawler  (2015-04-10) Description Chosen Problem Solving and Program design as an optional course, y