问题标题: 酷町堂:1364:打地鼠 RE 0 pts 求助

0
0
已解决
刘旭晨
刘旭晨
初级守护
初级守护

My code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#define N 10010
#define F(a) for(int i=1;i<=a;i++)
#define Fj(a) for(int j=1;j<=a;j++)
#define Ff(a, b) for(int i = a; i <= b; i++)
#define FD(a ,b) for(int i = a; i >= b; i--)
inline int read()
{
    int ans = 0;
    char ch = getchar();
    while (!isdigit(ch) && ch != '-') ch = getchar();
    bool sign = false;
    if (ch == '-') sign = true, ch = getchar();
    while (ch >= '0' && ch <= '9') ans = ans * 10 + (ch - '0'), ch = getchar();
    return (sign == true ? -ans : ans);
}
typedef long long ll;
using namespace std;
string a;
int n, k;
bool check(int x)
{
    int sum = 0;
    for (int i = 1; i <= n; i++)
        if (a[i] == '1') sum ++, i += x - 1;
    return (sum > k);
}
int main()
{
    n = read(), k = read();
    ll left, right;
    cin >> a;
    while (left < right)
    {
        ll mid = (left + right) >> 1;
        if (check(mid)) left = mid + 1;
        else right = mid;
    }
    cout << left << "\n";
    return 0;
}

 


0
已采纳
方晨顺
方晨顺
中级守护
中级守护
#include<iostream>
#include<string>
using namespace std;
int n,k;
string s;
int check(int x)//求解转判定
{
    int cnt=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='1')
        {
            i=i+x-1;
            cnt++;
        }
    } 
    return cnt;
}
int main()
{
    cin>>n>>k; 
    cin>>s;
    int l=0,r=n-1;
    while(l<r)//二分
    {
        int mid=(l+r)/2;
        if(check(mid)>k)
        {
            l=mid+1;
        }
        else
        {
            r=mid;
        }
    }
    cout<<l;
    return 0;
}

那个循环从0到n-1吧,字符串从0开始啊,你就不怕被邢老师看到?

0
我要回答