CodeForces 738D Sea Battle

抽屉原理。

先统计最多有$sum$个船可以放,假设打了$sum-a$枪都没打中$a$个船中的任意一个,那么再打$1$枪必中。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c))
    {
        x = x * 10 + c - ‘0‘;
        c = getchar();
    }
}

int n,a,b,k;
char s[200010];
struct X
{
    int L;
    int cnt;
}t[200010];
int sz,cnt;
int sum;

int main()
{
    cin>>n>>a>>b>>k;
    cin>>s;

    for(int i=0;s[i];)
    {
        if(s[i]==‘0‘)
        {
            int j;
            for(j=i;s[j];j++)
            {
                if(s[j]==‘0‘) continue;
                break;
            }
            j--;

            t[sz].L=i;
            t[sz].cnt=j-i+1;
            sz++;
            i=j+1;
        }
        else i++;
    }

    for(int i=0;i<sz;i++) sum=sum+t[i].cnt/b;

    int now=0;

    printf("%d\n",sum-a+1);

    for(int i=0;i<sz;i++)
    {
        for(int j=1;j<=t[i].cnt/b;j++)
        {
            printf("%d ",t[i].L+j*b);
            now++;
            if(now==sum-a+1) break;
        }
        if(now==sum-a+1) break;
    }

    return 0;
}
时间: 2024-08-14 02:36:34

CodeForces 738D Sea Battle的相关文章

POJ 1856 Sea Battle(DFS)

题意  图中每个矩形'#'连通块代表一艘船  若一艘船与另一艘有边相邻或有角相邻  那么认为这两艘船相撞   若图中有船相撞  输出bad  否则输出图中有多少艘船 可以把图的周围全包上一圈'.'  遍历图中每个点  可知当图中存在一下四种结构中的一个时  必有船相撞   输出并退出循环  否则则dfs这个点  若图中不存在这些结构   就可以输出连通块数量即轮船数了 #include<cstdio> #include<cstring> using namespace std; c

POJ 1856 Sea Battle(BFS).

~~~~ 题意: 给你一个R*C的图,求其由图中连通'#"所组成的矩形的个数. 注意:If the ships were placed correctly (i.e., there are only rectangles that do not touch each other even with a corner), print the sentence "There are S ships." where S is the number of ships. Otherwi

POJ 1856 Sea Battle

题意:找出R*C的图中不想交的矩形个数,如果有一个相交矩形就输出Bad placement. 不然就输出能放多少船 不相交是说对于一个矩形('#'组成)周围一圈都是由'.'包围,所以我们只用求出以某个点为起点的最大和最小的x,y的值,并且在搜索时'#'的个数rec,如果rec=(maxx-minx+1)*(maxy-miny+1)成立,则这个矩形合法 #include <iostream> #include<stdio.h> #include<string> #incl

POJ 1856 Sea Battle(dfs)

Description During the Summit, the armed forces will be highly active. The police will monitor Prague streets, the army will guard buildings, the Czech air space will be full of American F-16s. Moreover, the ships and battle cruisers will be sent to

CodeForces - 567D One-Dimensional Battle Ships

E - One-Dimensional Battle Ships CodeForces - 567D Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 × n table). At the beginning of the game Alice puts

51 nod 1521 一维战舰 时间复杂度O(n),同 Codeforces 567D. One-Dimensional Battle Ships 有详细注释

题目:51nod: 题目Codeforces: 题目注意到两个战舰不能挨在一起就可以了. // 每一段 struct node{ int left; // 段的左端点 int right; // 段的右端点 int length; // 段长度 int ship; // 段最大容纳战舰数 }arr[200005]; 每一段可容纳战舰数: ship*a + (ship - 1) <= length;   -->   ship = (length+1) / (a+1);(舍去小数部分) 构造出这么

codeforces 567D.One-Dimensional Battle Ships 解题报告

题目链接:http://codeforces.com/problemset/problem/567/D 题目意思:给出 1 * n 的 field,编号从左至右依次为 1,2,...,n.问射 m 枪之后(第 i 次射中编号 xi,则 xi 这一点是不能放置船只的!),能不能将 k 只 1 * a 的小船放到这些没有经过被射中编号的 field 中 .由于Alice 每次 shoot 的时候都会说 miss 的,即没有打中,你需要判断第几次shoot 使得整个field 不能放置 k 只 小船,

Codeforces 1045D Interstellar battle

Interstellar battle 对于每个联通块的贡献我们把它放到深度最低的那个点上面去. 那么一个点有贡献就是它的父亲被摧毁了并且自己没有被摧毁. 然后把所有概率加起来就是期望了. #include<bits/stdc++.h> using namespace std; const int N = (int)1e5 + 7; int n; double p[N]; double ans; double sum[N]; int pa[N]; vector<int> G[N];

codeforces 738D

题意:一行1*n的格子放船只,船数为a,船的长度为b,每格为0或1,1表示该格并不是船只的一部分,找出最少的格子数使得射击这些格子至少能打中一艘船. 思路:船的长度为b,即每段连续的长度为b的0的串都可以放下一只船,射击点为最后一个格子时最优,找出所有的可以放船的位置res个,选择其中的res-a+1个位置. #include<bits/stdc++.h> using namespace std; char str[200005]; int op[200005]={0}; int main()