Gym 101911F “Tickets”
题意:
给你一个由六位数字组成的门票编码x,并定义F(x) = | 前三位加和 - 后三位加和|;
求出给定的门票编码 x 之前并且 F(i) < F(x) 的 i 的总个数。
题解:
为方便描述,先定义一个虚拟的数组 a[ i ][ j ] : 表示前 i 个数中,经过 F() 函数映射成数 j 的整数的总个数;
首先,我先将门票编码转化成整数,然后按升序排列。
从 0 开始便利所有的整数 num,对于某个整数 num ,判断当前的 num 是否 == 某一门票编码x,如果等于,
那么在x之前并且经F()映射后值小于F(x)的整数的总个数为 Σ(a[ i ][ 0 ] + a[ i ][ 1 ] + a[ i ][ F(x)-1 ]),其中 0 ≤ i < x ;
当然,为了提高求和效率,我使用了树状数组来存储答案,具体细节看代码;
AC代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define lowbit(x) (x&-x)
const int maxn=2e5+; int n;
struct Date
{
int val;
int id;
int ans;
}_date[maxn];
struct BIT
{
int bit[];
void Add(int t)
{
while(t < )
{
bit[t]++;
t += lowbit(t);
}
}
int Sum(int t)
{
int sum=;
while(t > )
{
sum += bit[t];
t -= lowbit(t);
}
return sum;
}
}_bit;
int Trans(char *s)
{
return (s[]-'')*+(s[]-'')*+
(s[]-'')*+(s[]-'')*+
(s[]-'')*+(s[]-'');
}
int F(int val)
{
int sum=;
int index=;
while(index <= && val)
{
sum += val%;
val /= ;
index++;
}
while(val)
{
sum -= val%;
val /= ;
}
return abs(sum);
} bool cmp1(Date _a,Date _b)
{
return _a.val < _b.val;
}
bool cmp2(Date _a,Date _b)
{
return _a.id < _b.id;
} void Solve()
{
sort(_date+,_date+n+,cmp1);
int index=;
int zero=; for(int i=;index <= n;++i)
{
int x=F(i);
int val=_date[index].val; //处理某一门票编码出现多次的情况
while(val == i && index <= n)
{
int t=F(val);//将val映射为F(val)
_date[index].ans=_bit.Sum(t-)+(t > ? zero:);
index++;
val=_date[index].val;
} //因为树状数组不能处理0,所以0单独用变量zero记录
if(x == )
zero++;
else
_bit.Add(x);
}
sort(_date+,_date+n+,cmp2); for(int i=;i <= n;++i)
printf("%d\n",_date[i].ans);
}
int main()
{
scanf("%d",&n);
for(int i=;i <= n;++i)
{
char s[];
scanf("%s",s);
_date[i].val=Trans(s);//将字符串 s 转化为整数
_date[i].id=i; }
Solve(); return ;
}
Gym 101911F “Tickets”的更多相关文章
- Codeforces Gym 100418J Lucky tickets 数位DP
Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- Gym 100418J Lucky tickets(数位dp)
题意:给定一个n.求区间[1, n]之间的全部的a的个数.a满足: a能整除 把a表示自身二进制以后1的个数 思路:题意非常绕.... 数位dp,对于全部可能的1的个数我们都dfs一次 对于某一个可 ...
- Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset
Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
- Gym 102056L - Eventual … Journey - [分类讨论][The 2018 ICPC Asia-East Continent Final Problem L]
题目链接:https://codeforces.com/gym/102056/problem/L LCR is really an incredible being. Thinking so, sit ...
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
随机推荐
- Windows 10 & change DNS
Windows 10 & change DNS https://www.windowscentral.com/how-change-your-pcs-dns-settings-windows- ...
- DAY01、计算机组成及操作系统
一.编程与编程的目的: 1.什么是语言?什么是编程语言? 语言是一事物与另一事物之间沟通的介质 编程语言就是程序员与计算机之间沟通的介质 2.什么是编程? 程序员把自己想要让计算机做的事用编程语言表达 ...
- oracle数据库备份和恢复
参考地址:https://www.cnblogs.com/1175429393wljblog/p/9529334.html Oracle数据导入导出imp/exp 在cmd的dos命令提示符下执行,而 ...
- 二、kubernetes
一.kubernetes(简称k8s) 集群示意图 Kubernetes工作模式server-client,Kubenetes Master提供集中化管理Minions.部署1台Kubernetes ...
- Lodop“对象不支持SET__LICENSES属性或方法”SET__LICENSES is not a function”
Lodop中的方法如果书写错误,就会报错:“对象不支持XXX属性或方法”调试JS会报错”SET__LICENSES is not a function” LODOP.SET_LICENSES是加注册语 ...
- webpack始出来
一直想好好整理一下webpack,现在就整理吧. 总结自己的实际搭建的整理情况,我还是要先对自己说一句,以后给文件夹起名字的时候不要用一些特殊的关键字,比如我在做这个demo的时候,我用的文件夹名称叫 ...
- 树&图 记录
A - Lake Counting POJ - 2386 最最最最最基础的dfs 挂这道题为了提高AC率(糖水不等式 B - Paint it really, really dark gray Cod ...
- ceph 重启,停止,开始
systemctl restart ceph-mon@mon-node3 systemctl stop ceph-mon@mon-node3 systemctl start ceph-mon@mon- ...
- SpringBoot整合ssm
1.创建工程 使用idea可以快速创建SpringBoot的工程 这里选择常用的类库,SpringBoot将各种框架类库都进行了封装,可以减少pom文件中的引用配置: 比如Spring和Mybatis ...
- Flask 构建微电影视频网站(三)
搭建后台页面 视图函数位于admin文件夹下, app/admin/views.py 管理员登录页面搭建 视图函数 @admin.route('/') def index(): return '后台主 ...