Problem E: 穷游中国在统题 优先队列 + 模拟
http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=4
Problem E: 穷游中国在统题
Description
Input
Output
Sample Input
Sample Output
HINT
考虑下这个列子吧
2 4 1 3 5
和
2 4 1 5 3
首先它的数字不是1--n的,而且可能重复。
那么就离散化吧。把他们离散到1--n中。注意,如果数字是2、1、5、4、2
那么离散后的结果是2、1、5、4、3,总是要把她们离散到1---n
至于怎么离散,如果离散成2、1、5、4、2很容易,就是排序 + lowerbound
那么用book[val]表示这个数字出现了多少次,加上权值就可以了。
至于为什么我要离散成这样。就是因为其实这题每次都是要把最小的数字归位。
比如样例1,一开始我肯定是要让最小的那个数字,就是1,归位的吧,所以区间数字的数量最小都是3.
然后归位的时候发现途中有一个4,所以区间数字的数量要更改为4.(如果a[4]是5,那么就要变成5)
然后就在剩下的数字中,(现在只身下5)去找一个最小的数字归位。
其实就是模拟题,只不过每次必须的任务是把最小的数字归位。
离散成这样的目的是相对大小很明显,知道需要比较到那一位数字。
复杂度是O(nlogn)的
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int b[maxn];
int a[maxn];
int book[maxn];
bool vis[maxn];
struct node {
int val, pos;
bool operator < (const struct node & rhs) const {
if (val != rhs.val) return val > rhs.val;
else return pos > rhs.pos;
}
};
priority_queue<struct node>que;
void work() {
while (!que.empty()) que.pop();
memset(book, , sizeof book);
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &b[i]);
a[i] = b[i];
}
sort(b + , b + + n);
for (int i = ; i <= n; ++i) {
a[i] = lower_bound(b + , b + + n, a[i]) - b;
book[a[i]]++;
a[i] += book[a[i]] - ;
}
// for (int i = 1; i <= n; ++i) {
// printf("%d ", a[i]);
// }
// printf("\n");
memset(vis, false, sizeof vis);
for (int i = ; i <= n; ++i) {
struct node t;
t.pos = i;
t.val = a[i];
que.push(t);
assert(vis[a[i]] == false);
vis[a[i]] = true;
}
int ans = ;
int lef = -inf;
int be = ;
while (!que.empty()) {
struct node t = que.top();
que.pop();
if (t.pos <= lef) continue;
lef = t.pos;
int mx = -inf;
while (true) {
int tmx = mx;
for (int i = be; i <= lef; ++i) {
mx = max(a[i], mx);
}
if (tmx == mx) {
be = mx + ;
lef = mx;
break;
}
be = lef + ;
lef = mx;
}
ans++;
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) {
work();
if (t) printf("\n");
}
return ;
}
Problem E: 穷游中国在统题 优先队列 + 模拟的更多相关文章
- 蚂蜂窝VS穷游最世界-自由行类App分析
很多其它内容请关注博客: http://www.china10s.com/blog/? p=150 一.产品概述 体验环境: 机型:iPhone 6 型号:64G版 系统:iOS9.2 蚂蜂窝APP版 ...
- C. Tourist Problem 2021.3.29 晚vj拉题 cf 1600 纯数学题
拉题链接 https://vjudge.net/contest/430219#overview 原题链接 https://codeforces.com/problemset/problem/340 ...
- poj 1005:I Think I Need a Houseboat(水题,模拟)
I Think I Need a Houseboat Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 85149 Acce ...
- POJ 3468 A Simple Problem with Integers(线段树水题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 135904 ...
- The Chinese Postman Problem HIT - 2739(有向图中国邮路问题)
无向图的问题,如果每个点的度数为偶数,则就是欧拉回路,而对于一个点只有两种情况,奇数和偶数,那么就把都为奇数的一对点 连一条 边权为原图中这两点最短路的值 的边 是不是就好了 无向图中国邮路问 ...
- HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题
首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m & ...
- HDU2426:Interesting Housing Problem(还没过,貌似入门题)
#include <iostream> #include <queue> #include <stdio.h> #include <string.h> ...
- 【莫比乌斯反演+容斥】BZOJ2301-[HAOI2011]Problem b(成为权限狗的第一题纪念!)
[题目大意] 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. [思路] “怎么又是你系列……”思路 ...
- sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)
The Android University ACM Team Selection Contest Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里 ...
随机推荐
- Spring在3.1版本后的bean获取方法的改变
xml配置不变,如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...
- NameNode备份策略以及恢复方法
一.dits和fsimage 首先要提到两个文件edits和fsimage,下面来说说他们是做什么的. 集群中的名称节点(NameNode)会把文件系统的变化以追加保存到日志文件edits中 ...
- BigDecimal快速使用
double类型最多支持16位有效数字,且最大值只支持10^308次方,大一点的数字会变为科学计数法,小数精度不够等有一系列不方便的问题: 引进BigDecimal解决此类麻烦,弊端,BigDecim ...
- Vijos P1389婚礼上的小杉
背景 小杉的幻想来到了经典日剧<求婚大作战>的场景里……他正在婚礼上看幻灯片,一边看着可爱的新娘长泽雅美,一边想,如果能再来一次就好了(-.-干嘛幻想这么郁闷的场景……). 小杉身为新一代 ...
- js生成随机编码并赋值给input文本框
效果图如下: 页面代码: <div class="form-item form-width-in fr"> <label>产 品 编 码</label ...
- Java 通过 HTTP 下载文件
1. [代码]Download.java package core.spider; import java.io.*;import java.net.*;import java.util.*; / ...
- android广播接收器
Android程序创建广播接收器继承BroadcastReceiver Android广播接收器需要在AndroidManifest.xml文件中声明: <recevie android:nam ...
- impdp+network link 跳过expdp直接导入目标库
impdp命令特殊用途,可以将数据库的一个用户迁移到另一台机器上的数据库的用户中.如果目标用户不存在,还可以对应的创建该用户. 快速的把A库上的用户迁移到B库上. 下面就来看一下命令格式: B库下执 ...
- Centos Missing Library: QtWebKit.so.4
/******************************************************************** * Centos Missing Library: QtWe ...
- 如何生成Android的keystore文件
步骤 1 找到本机电脑上jdk安装的目录,使用cmd命令打开命令窗口,输入cd jdk目录(替换成你的jdk的bin目录),进入到jdk的bin目录,接下来你才可以使用jdk的命令进行操作 步骤 ...