1007.Photoshop Layers (十六进制 / 前缀和)(2021“MINIEYE杯”中国大学生算法设计超级联赛3)
作者:互联网
题意: 有n层具有颜色的涂层,其每层的颜色由Ri, Ci,Bi三个指标对应6个长度的16进制数表示的。现问你 [li, ri]区间涂层的颜色重叠在一起是什么颜色。重叠规则如下:
* 若第i层的 wi==1,那么它只会显示自己的颜色(Ri,Gi,Bi)。
* 若第i层的 wi==2,那么它的颜色就是自身与先前层次的颜色的混和(min(Rp+Ri,255), min(Gp+Gi,255), min(Bp+Bi,255),当然颜色会有个上限255。
针对 p 次询问回答每个区间最后的颜色是什么。
思路:不难看出这就是在求 [li, ri]的颜色区间和,只不过换成了16进制数;且当出现 '1'时就会阻断颜色的传递,即所求颜色为 [max(pos, l), r]区间内的和,pos为最靠近 r 的前方 '1' 位置。
赛中还写了16->10和10->16进制之间转换的自定义函数,但是直接T到想吐!!!最后甚至只用到10->16的转换并于map初始化标记,还利用了快读和getchar(),依旧T到怀疑人生.......
代码实现:
#include<bitsdc++.h>
#define LL long long
#define me(ar) memset(ar, 0, sizeof ar)
using namespace std;
const int N = 1e5 + 5;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
int T = 1;
int n, p, l, r, sa, sb, sc;
string s[N];
/ar s[N][10];
unordered_map<string, int> mp;
unordered_map<int, string> mp2;
int m[N], pos[N], a[N], b[N], c[N];
int pa[N], pb[N], pc[N], arr[N], num[N];
char h[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void work1(int x){
int cnt = 0, w = x;
string hh;
while(x>0){
num[cnt++]=x%16;
x /= 16;
}
if(cnt==1) hh += "0";
for(int i = cnt-1; ~i; i --){
int m = num[i];
hh += h[m];
}
mp[hh] = w;
mp2[w] = hh;
}
void Inint(){
for(int i = 1; i <= 255; i ++){
work1(i);
}
}
signed main()
{
Inint();
T = read();
while(T --){
n = read(); p = read();
for(int i = 1; i <= n; i ++){
m[i] = read();
// scanf("%s", s[i]);
// getchar();
for(int j = 1; j <= 6; j ++){
s[i] += getchar();
}
// getchar();
// cout << "s: " << s[i] << endl;
string ss1, ss2, ss3;
ss1 += s[i][0]; ss1 += s[i][1];
a[i] = mp[ss1];
ss2 += s[i][2]; ss2 += s[i][3];
b[i] = mp[ss2];
ss3 += s[i][4]; ss3 += s[i][5];
c[i] = mp[ss3];
// cout << "a: " << a[i] << " b: " << b[i] << " c: " << c[i] << endl;
pa[i] = pa[i-1]+a[i];
pb[i] = pb[i-1]+b[i];
pc[i] = pc[i-1]+c[i];
if(m[i]==1) pos[i] = i;
else pos[i] = pos[i-1];
}
while(p --){
l = read(); r = read();
if(pos[r]==r){
int le = s[r].size();
for(int i = 0; i < le; i ++)
printf("%c", s[r][i]);
}
else if(pos[r]>l){
int ll = pos[r];
sa = pa[r]-pa[ll-1], sb = pb[r]-pb[ll-1], sc = pc[r]-pc[ll-1];
sa = min(sa, 255), sb = min(sb, 255), sc = min(sc, 255);
int la = mp2[sa].size(), lb = mp2[sb].size(), lc = mp2[sc].size();
for(int i = 0; i < la; i ++)
printf("%c", mp2[sa][i]);
for(int i = 0; i < lb; i ++)
printf("%c", mp2[sb][i]);
for(int i = 0; i < lc; i ++)
printf("%c", mp2[sc][i]);
}
else {
sa = pa[r]-pa[l-1], sb = pb[r]-pb[l-1], sc = pc[r]-pc[l-1];
sa = min(sa, 255), sb = min(sb, 255), sc = min(sc, 255);
int la = mp2[sa].size(), lb = mp2[sb].size(), lc = mp2[sc].size();
for(int i = 0; i < la; i ++)
printf("%c", mp2[sa][i]);
for(int i = 0; i < lb; i ++)
printf("%c", mp2[sb][i]);
for(int i = 0; i < lc; i ++)
printf("%c", mp2[sc][i]);
}
puts("");
}
}
return 0;
}
呜呜呜~赛后深刻反思,不该一直纠结一个思路和一份代码硬磕,及时转换思想很重要!!
其实此题在进制之间的转换只涉及到两位数,每次利用的时候临时计算一下反而比规范的自定义函数更省时;再者去掉自定义函数后甚至可以利用纯C语言编写最简洁的程序,没有了string和map肯定更省时。
AC代码:
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define LL long long
#define me(ar) memset(ar, 0, sizeof ar)
using namespace std;
const int N = 1e5 + 5;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
int T = 1;
int n, p, l, r, a, b, c, sa, sb, sc;
char s[10];
int m[N], pos[N], pa[N], pb[N], pc[N];
char h[18]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int matche(char o){
if('0'<=o&&o<='9') return o-'0';
else return o-'A'+10;
}
signed main()
{
T = read();
while(T --){
n = read(); p = read();
for(int i = 1; i <= n; i ++){
m[i] = read(); scanf("%s", s);
a = matche(s[0])*16+matche(s[1]);
b = matche(s[2])*16+matche(s[3]);
c = matche(s[4])*16+matche(s[5]);
pa[i] = pa[i-1]+a; pb[i] = pb[i-1]+b; pc[i] = pc[i-1]+c;
if(m[i]==1) pos[i] = i;
else pos[i] = pos[i-1];
}
while(p --){
l = read(); r = read();
if(pos[r]>l) l = pos[r];
sa = pa[r]-pa[l-1], sb = pb[r]-pb[l-1], sc = pc[r]-pc[l-1];
sa = min(sa, 255), sb = min(sb, 255), sc = min(sc, 255);
printf("%c%c", h[sa/16], h[sa%16]);
printf("%c%c", h[sb/16], h[sb%16]);
printf("%c%c", h[sc/16], h[sc%16]);
puts("");
}
}
return 0;
}
标签:Layers,十六进制,Photoshop,16,int,mp2,sc,sb,sa 来源: https://blog.csdn.net/Satur9/article/details/119150144