↓ Skip to main content
  1. Posts/

poj 2456 Aggressive cows (二分)

·347 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:给出n个x轴上的坐标点,选取其中c个,问c个之中任意两个点的最小距离最大是多少。

思路:二分距离check合法性。

大水题。。。因为想把三分艹掉。。。三分的题又多和二分挂在一起。。。顺便就写了。。。。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :Mon 19 Sep 2016 10:57:54 PM CST
 4File Name :code/poj/2456.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <set>
13#include <map>
14#include <string>
15#include <cmath>
16#include <cstdlib>
17#include <ctime>
18#define fst first
19#define sec second
20#define lson l,m,rt<<1
21#define rson m+1,r,rt<<1|1
22#define ms(a,x) memset(a,x,sizeof(a))
23typedef long long LL;
24#define pi pair < int ,int >
25#define MP make_pair
26using namespace std;
27const double eps = 1E-8;
28const int dx4[4]={1,0,0,-1};
29const int dy4[4]={0,-1,1,0};
30const int inf = 0x3f3f3f3f;
31const int N=1E5+7;
32int x[N];
33int n,c;
34bool check( int k)
35{
36    int cnt = 1;
37    int cur = x[1];
38    for ( int i = 2 ; i <= n ; i++)
39    {
40	if (x[i]-cur>=k)
41	{
42	    cnt++;
43	    cur = x[i];
44	}
45    }
46    return cnt>=c;
47}
48int bin()
49{
50    int l = 1;
51    int r = x[n]-x[1];
52    while (l<=r)
53    {
54	int mid = (l+r)>>1;
55	if (!check(mid))
56	{
57	    r = mid-1;
58	}
59	else
60	    l = mid+1;
61    }
62    return l-1;
63}
64int main()
65{
66	#ifndef  ONLINE_JUDGE
67	freopen("code/in.txt","r",stdin);
68  #endif
69	scanf("%d%d",&n,&c);
70	for ( int i = 1 ; i <= n ; i++) scanf("%d",&x[i]);
71	sort(x+1,x+n+1);
72	int ans = bin();
73	printf("%d\n",ans);
74  #ifndef ONLINE_JUDGE
75  fclose(stdin);
76  #endif
77    return 0;
78}

Related

seerc 2014 Circle of digits (二分+后缀数组)

·979 words·2 mins
题目链接 题意:把一个长度为 n 的只由数字构成的串分成 k 个不为空的子串,使得最大的串最小(大小是指串所对应的十进制数的大小)。 思路:由于长度为 x 的串肯定大于长度为 x-1 的串,因此很容易想到,我们要尽可能使得 k 组串的长度平均(避免出现某一个串的长度非常大的情况)。

codeforces 123 D. String (后缀数组+两次二分得到区间+rmq)

·1380 words·3 mins
题目链接 题意:定义一个函数 F。 For example: F(babbabbababbab, babb) = 6. The list of pairs is as follows: (1, 4), (4, 7), (9, 12) Its continuous sequences are: (1, 4) (4, 7) (9, 12) (1, 4), (4, 7) (4, 7), (9, 12) (1, 4), (4, 7), (9, 12) 二分。 题目描述得很烂,看例子吧,大概就是:如果字符串 y 在字符串 x 中出现 n 次,那么 F(x,y)=n*(n+1)/2

whust 2016 warm up G ||codeforces 689C. Mike and Chocolate Thieves

·290 words·1 min
cf689C 题意:给出一个m。。问恰好使得不超过某个n的a*b^3(a,b是正整数)的方案数为m的n是多少。。。 思路:暴力+二分。。。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年07月18日 星期一 15时58分55秒 4File Name :code/2016whust/G.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33LL m,n; 34LL ans; 35LL cal( LL x) 36{ 37 LL res = 0LL; 38 for ( LL i =2 ; i * i * i <= x ; i++) 39 res+=x/(i*i*i); 40 return res; 41} 42int main() 43{ 44 #ifndef ONLINE_JUDGE 45 freopen("code/in.txt","r",stdin); 46 #endif 47 48 cin>>m; 49 n = 0 ; 50 51 LL cur = 1LL<<60; 52// cout<<"cur:"<<cur<<endl; 53 while (cur!=0LL) 54 { 55 LL tmp = cal(n+cur); 56 if (tmp<m) n +=cur; 57 58 cur >>=1LL; 59 } 60 n++; 61// cout<<" n:"<<n<<endl; 62 if (cal(n)!=m) ans = -1; 63 else ans = n; 64 65 cout<<ans<<endl; 66 67 68 #ifndef ONLINE_JUDGE 69 fclose(stdin); 70 #endif 71 return 0; 72}

BZOJ 1614: [Usaco2007 Jan]Telephone Lines架设电话线 (二分+spfa)

·1493 words·3 mins
1614: [Usaco2007 Jan]Telephone Lines架设电话线 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1325 Solved: 570 [Submit][Status][Discuss] Description # Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务。于是,FJ必须为此向电信公司支付一定的费用。 FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废弃的电话线杆,任意两根电话线杆间都没有电话线相连。一共P(1 <= P <= 10,000)对电话线杆间可以拉电话线,其余的那些由于隔得太远而无法被连接。 第i对电话线杆的两个端点分别为A_i、B_i,它们间的距离为 L_i (1 <= L_i <= 1,000,000)。数据中保证每对{A_i,B_i}最多只出现1次。编号为1的电话线杆已经接入了全国的电话网络,整个农场的电话线全都连到了编号为N的电话线杆上。也就是说,FJ的任务仅仅是找一条将1号和N号电话线杆连起来的路径,其余的电话线杆并不一定要连入电话网络。 经过谈判,电信公司最终同意免费为FJ连结K(0 <= K < N)对由FJ指定的电话线杆。对于此外的那些电话线,FJ需要为它们付的费用,等于其中最长的电话线的长度(每根电话线仅连结一对电话线杆)。如果需要连结的电话线杆不超过 K对,那么FJ的总支出为0。 请你计算一下,FJ最少需要在电话线上花多少钱。