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 ...
随机推荐
- @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableAutoConfiguration 作用:Spring Boot会自动根据你jar包的依赖来自动配置项目. 例如当你项目下面有HSQLDB的依赖时,Spring Boot会创建默认的内存 ...
- Yii2写日志总结
方法一 批量文件配置写入日志: 1. 首先在config.php配置文件中配置log模块 如下: 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, ...
- Java HashMap的put操作(Java1.6)
https://www.cnblogs.com/skywang12345/p/3310835.html // 存储数据的Entry数组,长度是2的幂. // HashMap是采用拉链法实现的,每一个E ...
- DotNetty 实现 Modbus TCP 系列 (一) 报文类
本文已收录至:开源 DotNetty 实现的 Modbus TCP/IP 协议 Modbus TCP/IP 报文 报文最大长度为 260 byte (ADU = 7 byte MBAP Header ...
- no module named 'win32api'问题
运行scrapy时,报错no module named 'win32api' 解决办法: https://github.com/mhammond/pywin32/releases 下载对于python ...
- git 出现错误时
Your local changes to the following files would be overwritten by merge: 解决办法 如果希望保留生产服务器上所做的改动,仅仅并入 ...
- Android EditView 获取焦点 不弹出软键盘
很简单的做法: 找到AndroidManifest.xml文件 然后在对应的activity中增加android:windowSoftInputMode="adjustPan" & ...
- Java json转model
前面有一篇关于 json的转换类的工具:http://blog.csdn.net/hanjun0612/article/details/77891569 但是有一个情况. 由于java需要属性小写开 ...
- linux下后台启动springboot项目
linux下后台启动springboot项目 我们知道启动springboot的项目有三种方式: 运行主方法启动 使用命令 mvn spring-boot:run”在命令行启动该应用 运行“mvn p ...
- BZOJ 4326 运输计划
二分答案+树链剖分+树上差分 我们假设x是最小的花费,可以想到给定x,所有运输计划中花费大于x的计划必须经过虫洞,且最长的一条的花费减去虫洞所在边的花费要小于等于x 那么对于x,虫洞所在的位置肯定是确 ...