AtCoder Beginner Contest 159 E - Dividing Chocolate【枚举】

题目链接

数据范围:

\(1≤H≤10\)

\(1≤W≤1000\)

\(1≤K≤H×W\)

分析:

先观察数据,发现行数特别小,那么我们就可以枚举行的分法,对于每一种分法,求出列的划分数,取最小。

先用二维前缀和,预处理整个图。

复杂度:\(O(2^H*H*W)\)

代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1010;
char s[15][N];
int num[15][N];
int main()
{
    int h,w,k,ans=0;
    scanf("%d%d%d",&h,&w,&k);
    for(int i=1;i<=h;i++)
        scanf("%s",s[i]+1);
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=w;j++)
            num[i][j]=num[i-1][j]+num[i][j-1]-num[i-1][j-1]+s[i][j]-‘0‘;
    }
    if(h==1)
    {
        int st=0;
        for(int i=1;i<=w;i++)
        {
            if(num[1][i]-num[1][st]==k&&i<w)
            {
                ans++;
                st=i;
            }
            printf("%d\n",ans);
            return 0;
        }
    }
    ans=h*w;
    for(int i=0;i<(1<<(h-1));i++)//行的划分种类数
    {
        int c=0,r;
        int res=__builtin_popcount(i);
        bool g=0;
        for(int j=1;j<=w;j++)//枚举列
        {
            bool f=0;
            r=0;
            for(int u=1;u<=h;u++)
            {
                if((1&(i>>(u-1)))||u==h)
                {
                    int t=num[u][j]+num[r][c]-num[r][j]-num[u][c];
                    r=u;
                    if(t>k)
                        f=1;
                }
            }
            if(f&&j==c+1)
            {
                g=1;
                break;
            }
            if(f)
            {
                res++;
                c=j-1;
            }
        }
        if(!g)
            ans=min(res,ans);
    }
    printf("%d\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/1024-xzx/p/12554604.html

时间: 2024-11-02 11:47:44

AtCoder Beginner Contest 159 E - Dividing Chocolate【枚举】的相关文章

AtCoder Beginner Contest 159

传送门 A - The Number of Even Pairs #include <bits/stdc++.h> #define ll long long using namespace std; int main() { //freopen("in.txt","r",stdin); int n,m; scanf("%d%d",&n,&m); printf("%d\n",n*(n-1)/2+m*(

【ATcoder】AtCoder Beginner Contest 159题解

官方题解 落谷链接 ATC链接 A - The Number of Even Pairs 题意 给你两个数$n, m$代表有$n$个偶数,$m$个奇数.让你输出$n$个偶数$m$个奇数从中任选两个数(没有顺序)相加结果为偶数的个数. 题解 相加为偶数只有偶加偶和奇加奇两种情况,其实就是在$n$个数中取两个($C\binom{2}{n}$),在$m$个数中取两个($C\binom{2}{m}$). 时间复杂度$O(1)$ 1 #include <iostream> 2 using namespa

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 115 题解

题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit : 1024MB Score : 100 points Problem Statement In some other world, today is December D-th. Write a program that prints Christmas if D=25, Christmas E