【8.30校内测试】【找规律模拟】【DP】【二分+贪心】

对于和规律或者数学有关的题真的束手无策啊QAQ
首先发现两个性质:
1、不管中间怎么碰撞,所有蚂蚁的相对位置不会改变,即后面的蚂蚁不会超过前面的蚂蚁或者落后更后面的蚂蚁。
2、因为所有蚂蚁速度一样,不管标号的话两只蚂蚁的碰撞相当于直接互相穿过,所以最初有多少蚂蚁方向向左,最后就有多少蚂蚁从左落下,向右同理。
总结一下又可以发现,比如有$cntl$只蚂蚁最初向左,$cntr$只蚂蚁最初向右,那么最后就是原位置的左边连续$cntl$只从左落下,原位置右边连续$cntr$只从右落下。我们将所有方向向左和向右的蚂蚁落下的时间分别排序,和原序列上一一对应即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std; int n, b[], cntl, cntr;
LL a[], L;
double ans[], l[], r[]; int main ( ) {
freopen ( "ant.in", "r", stdin );
freopen ( "ant.out", "w", stdout );
scanf ( "%I64d%d", &L, &n );
for ( int i = ; i <= n; i ++ ) scanf ( "%I64d", &a[i] );
for ( int i = ; i <= n; i ++ ) scanf ( "%d", &b[i] );
for ( int i = ; i <= n; i ++ )
if ( !b[i] )
l[++cntl] = a[i];
else r[++cntr] = L - a[i];
sort ( l + , l + + cntl );
sort ( r + , r + + cntr );
for ( int i = ; i <= cntl; i ++ )
ans[i] = l[i];
for ( int i = ; i <= cntr; i ++ )
ans[n - i + ] = r[i];
for ( int i = ; i <= n; i ++ )
printf ( "%.2lf ", ans[i] );
return ;
}
见8.20校内测试,题目转换一下就一模一样了。

数据比较水,写的$O(nlog_nlog_h)$完全够了。
二分最小值,$check$的时候贪心修改区间,我用的线段树,判断一下就好了。实际上差分复杂度更优。写线段树的时候无聊写了区间求和??
#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std; int n, K;
LL T, a[]; LL TR[], tag[], vc[]; void update ( int nd ) {
TR[nd] = TR[nd << ] + TR[nd << | ];
} void push_down ( int nd, int l, int r ) {
if ( tag[nd] ) {
int mid = ( l + r ) >> ;
TR[nd << ] += tag[nd] * ( mid - l + );
TR[nd << | ] += tag[nd] * ( r - mid );
tag[nd << ] += tag[nd];
tag[nd << | ] += tag[nd];
tag[nd] = ;
}
} void build ( int nd, int l, int r ) {
TR[nd] = ; tag[nd] = ;
if ( l == r ) {
TR[nd] = a[vc[l]];
return ;
}
int mid = ( l + r ) >> ;
build ( nd << , l, mid );
build ( nd << | , mid + , r );
update ( nd );
} void add ( int nd, int l, int r, int L, int R, LL d ) {
if ( l >= L && r <= R ) {
TR[nd] += ( r - l + ) * d;
tag[nd] += d;
return ;
}
push_down ( nd, l, r );
int mid = ( l + r ) >> ;
if ( L <= mid ) add ( nd << , l, mid, L, R, d );
if ( R > mid ) add ( nd << | , mid + , r, L, R, d );
update ( nd );
} LL query ( int nd, int l, int r, int pos ) {
if ( l == r ) return TR[nd];
push_down ( nd, l, r );
int mid = ( l + r ) >> ;
if ( pos <= mid ) return query ( nd << , l, mid, pos );
else return query ( nd << | , mid + , r, pos );
} bool check ( LL mid ) {
int tot = ; LL sum = ;
for ( int i = ; i <= n; i ++ )
if ( a[i] < mid ) vc[++tot] = i;
build ( , , tot );
vc[++tot] = 0x7f7f7f7f7f7f7f;
for ( int i = ; i < tot; i ++ ) {
LL now = query ( , , tot - , i );
if ( now >= mid ) continue;
if ( mid - now + sum > T ) { sum = T + ; break; }
int to = vc[i] + K - ;
int pos = upper_bound ( vc + , vc + + tot, to ) - vc - ;
add ( , , tot - , i, pos, mid - now );
sum += mid - now;
}
if ( sum <= T ) return ;
return ;
} LL MI = 0x3f3f3f3f, MA;
LL erfen ( ) {
LL l = MI, r = MA + T, ans;
while ( l <= r ) {
int mid = ( l + r ) >> ;
if ( check ( mid ) ) l = mid + , ans = mid;
else r = mid - ;
}
return ans;
} int main ( ) {
freopen ( "watering.in", "r", stdin );
freopen ( "watering.out", "w", stdout );
scanf ( "%d%d%I64d", &n, &K, &T );
for ( int i = ; i <= n; i ++ ) scanf ( "%I64d", &a[i] ), MI = min ( MI, a[i] ), MA = max ( MA, a[i] );
LL ans = erfen ( );
printf ( "%I64d", ans );
return ;
}
【8.30校内测试】【找规律模拟】【DP】【二分+贪心】的更多相关文章
- 找规律/数位DP HDOJ 4722 Good Numbers
题目传送门 /* 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () http://www.cnblogs.com/crazyapple/p/3315436.html 数 ...
- Codeforces D. Little Elephant and Interval(思维找规律数位dp)
题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...
- 洛谷 P1014 Cantor表【蛇皮矩阵/找规律/模拟】
题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … ...
- 2017年icpc西安网络赛 Maximum Flow (找规律+数位dp)
题目 https://nanti.jisuanke.com/t/17118 题意 有n个点0,1,2...n-1,对于一个点对(i,j)满足i<j,那么连一条边,边权为i xor j,求0到n- ...
- Codeforces Round #260 (Div. 2) A , B , C 标记,找规律 , dp
A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- hdu 1998 奇数阶魔方(找规律+模拟)
应该不算太水吧. 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 ...
- 【8.17校内测试】【模拟】【set】【网络流】
为什么每次想的最久的题得的分数最低!!!qwqwq 再也不在noip上尝试A*叻!! 模拟题,先把能消的消掉,双指针从两端向中间扫描,如果头尾合并可以消,就把它消掉,最后判断一下.因为消完过后num保 ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
- Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)
Vasya has a string s of length n. He decides to make the following modification to the string: Pick ...
随机推荐
- Chrome浏览器任意修改网页内容
在Chrome浏览器按F12,打开开发者工具,切换到console选项卡: 在下面的输入行输入下面的命令回车: document.body.contentEditable="true&quo ...
- lintcode 66.67.68 二叉树遍历(前序、中序、后序)
AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode le ...
- tp修改的写法
- Coursera在线学习---第六节.构建机器学习系统
备: High bias(高偏差) 模型会欠拟合 High variance(高方差) 模型会过拟合 正则化参数λ过大造成高偏差,λ过小造成高方差 一.利用训练好的模型做数据预测时,如果效果不好 ...
- perl6 登录phpmyadmin
use HTTP::UserAgent; my $ua = HTTP::UserAgent.new; my $url = 'http://localhost/phpMyAdmin/index.php' ...
- Opencv 配置VS2012
开始接触图像处理有一段时间了,经过前期的调研,和相关入门知识的学习,开始接触一些图像处理应用的工具.Opencv是一个图像处理的开源库,由于其开放的协议架构,国内外很多科研机构和团队都在基于openc ...
- PXC加入新节点避免SST时grastate.dat文件内容的修改问题
PXC加入新节点避免SST时grastate.dat文件内容的修改问题 在主从同步完成并关闭实例后,需要修改grastate.dat中的seqno:到底应该填已经执行过最后的XID号(Executed ...
- 使用navicat for sqlserver 把excel中的数据导入到sqlserver数据库
以前记得使用excel向mysql中导入过数据,今天使用excel向sqlserver2005导入了数据,在此把做法记录一下 第一步:准备excel数据,在这个excel中有3个sheet,每个she ...
- 在浏览器中输入www.baidu.com后执行的全过程
链接 http 请求过程——当我们在浏览器输入 www.baidu.com,然后回车之后的详解. 1)域名解析(域名 www.baidu.com变为 ip 地址). 1.浏览器搜索自己的DNS缓存(维 ...
- 剑指offer算法题
数组中只出现一次的数字(一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字): 解法在于位运算中的异或,直接异或可以得到这两个数的异或,按照最后的有效数字位可以 ...