↓ 跳过正文
  1. Posts/

codeforces croc 2016 C. Enduring Exodus

·475 字·1 分钟

题目链接 题意:给出n和k,给出一个长度为n的字符串表示房间的占用情况(0表示没占用,1表示已占用),从n个房间中找出k+1个,使得k+1中的k个距离k+1个中的1个的距离和最小。

思路:只需要考虑没被占用的位置。所以用pos[]数组记录0的位置。 找到第一个能住下的位置后向前平移即可。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年03月19日 星期六 00时21分28秒
  4File Name :code/cf/croc2016/C.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;
 33const int N=1E5+7;
 34int ban[N];
 35int k,n;
 36int sum[N];
 37int pos0[N];
 38string st;
 39
 40int dis(int x,int l,int r)
 41{
 42    return max(pos0[x]-pos0[l],pos0[r]-pos0[x]);
 43}
 44int main()
 45{
 46	#ifndef  ONLINE_JUDGE
 47	freopen("code/in.txt","r",stdin);
 48  #endif
 49	cin>>n>>k;
 50	cin>>st;
 51	ms(ban,0);
 52	ms(pos0,0);
 53	k++;
 54	int len = st.length();
 55	int cnt0 =  0;
 56	for ( int i = 0 ;  i < len ; i++)
 57	{
 58	    if (st[i]=='0')
 59	    {
 60		ban[i+1] = 0;
 61		cnt0++;
 62		pos0[cnt0] = i ;
 63	    }
 64	    else
 65	    {
 66		ban[i+1] = 1;
 67	    }
 68	}
 69
 70	sum[0] = 0 ;
 71
 72	for ( int i = 1 ; i <= n ; i++)
 73	{
 74	    sum[i] = sum[i-1] + ban[i];
 75	}
 76	int ans = inf;
 77	int l =  1 ;  int r = k;
 78	int mid =  0;
 79	for ( int i = 1 ; i <= r ; i++)
 80	{
 81	    if (dis(i,l,r)<dis(mid,l,r))
 82	    {
 83		mid =  i;
 84	    }
 85	}
 86
 87	ans = dis(mid,l,r);
 88	for ( ; r <= cnt0 ; r++,l++)
 89	{
 90	    while (dis(mid+1,l,r)<dis(mid,l,r)) mid++;
 91	    ans = min(ans,dis(mid,l,r));
 92	}
 93	cout<<ans<<endl;
 94
 95
 96
 97
 98
 99  #ifndef ONLINE_JUDGE
100  fclose(stdin);
101  #endif
102    return 0;
103}

相关文章

codeforces 519 C. A and B and Team Training

·271 字·1 分钟
http://codeforces.com/problemset/problem/519/C 题意:两种组队方式,3人一组,1个大牛+2个蒟蒻或者1个蒟蒻+2个大牛。给定大牛和蒟蒻的个数。问最多能组多少队。 思路:线性规划。设两种队分别有x,y个即可。 突然发现这题以前做过。。。比当时的代码简单了一些。还不错。

codeforces #edu 1 A tricky sum

·493 字·1 分钟
题意:求1+2+..+n的和。。但是对于是2的整数幂的项数。。符号是-。。 思路:可以先当做正数。(n+1)*n/2; 然后减去二倍的2的整数次幂的项的和。 坑点: 妈蛋第三次了。。。我想求小于等于n的最大是2的几次幂。。。取整的时候用int又会迷之错误。。。为什么说是迷之错误。。因为我WA的点的数据拿下来在本地跑是没有问题的。。。一交上去就错。。。不明觉厉。。。下次遇到double类型是数一点要小心小心再小心。。。第一次遇到是pow的返回类型是double,然后答案莫名奇妙的差1.第二次是#334 div2 的A题。。一道傻逼算分数的题我WA了一个小时。。。第三次是这个。。向下取整不要用(int)的强制转换。。而用floor吧。。233

codeforces #334 div 2 C. Alternative Thinking

·517 字·2 分钟
题意:给定一个01串。要进行一次变换:选一段连续的非空的字串,将这段串的0和1反转(0变成1,1变成0) 然后问能得到的最长的0,1交替的序列的长度是多少(不一定连续)

codeforces #332 div 2 C. Day at the Beach

·561 字·2 分钟
C. Day at the Beach time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles. At the end of the day there were n castles built by friends. Castles are numbered from 1 to n, and the height of the i-th castle is equal to h__i. When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that condition h__i ≤ h__i + 1 holds for all i from 1 to n - 1.

codeforces #332 div 2 B. Spongebob and Joke

·651 字·2 分钟
B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick’s personal stuff and found a sequence _a_1, _a_2, …, a__m of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence _f_1, _f_2, …, f__n of length n and for each number a__i got number b__i = f__a__i. To finish the prank he erased the initial sequence a__i.