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 multi virtual desktop keyboard shortcut
windows 10 multi virtual desktop keyboard shortcut windows 10 multi desktop keyboard shortcut https: ...
- MySQL系列:视图基本操作(3)
1. 视图简介 1.1 视图定义 视图是一种虚拟的表,是从数据库中一个或多个表中导出来的表. 视图可以从已存在的视图的基础上定义. 数据库中只存放视图的定义,并没有存放视图中的数据,数据存放在原来的表 ...
- vue-cli:渲染过程理解2(vue init webpack方式创建)
main.js: 入口文件 import Vue from 'vue' //引入node_modules中的vue import App from './App' //引入当前路径(src)下的App ...
- border-color的深入理解
.className{ width:100px;height:100px; border:100px solid; border-color: red green blue orange; } 最终的 ...
- Deploy .NET Core with Docker
Creating a .NET Core project If you already have an existing .NET Core project you are more than wel ...
- mybatis,主键返回指的是返回到传入的对象中
- 当页面是动态时 如果后台存储id可以通过查询后台方式获取对象;当后台没有存储时候 只有通过前端标记了 例如标记数量为10 我们根据传递过来的10循环取值
当页面是动态时 如果后台存储id可以通过查询后台方式获取对象;当后台没有存储时候 只有通过前端标记了 例如标记数量为10 我们根据传递过来的10循环取值
- Nginx 缓存针对打开的文件句柄与原文件信息
L:108 open_file_cache syntax: open_file_cache off; open_file_cache max=N[inactive=time](inactive表示 ...
- vs + babelua + cocos2d-x
https://blog.csdn.net/dugaoda/article/details/60467037 https://blog.csdn.net/taotanty/article/detail ...
- Codeforces963C Cutting Rectangle 【数学】
错了一个小地方调了一晚上.... 题目大意: 给出最多2E+5种不同的矩形,每种有它的长h和宽v还有数量d,现在你要构造大矩形,使得在上面沿着平行于长或宽的边划刀,切出来的矩形正好是给出的所有矩形.问 ...