首页 > 其他分享 >[CodeForces] F. Color Rows and Columns

[CodeForces] F. Color Rows and Columns

时间:2024-08-14 23:51:50浏览次数:15  
标签:Rows cost Color CodeForces greedy int score rectangle dp

Problem Link

 

Based on initial observation, it seems that greedily pick the smallest row / column length works. But the last example test case outputs 35 while greedy gives 36. 

 

How you should go from there:

1. check if your greedy implementation is correct. It was, in this case.

2. if no implementation bugs, then maybe greedy does not work, think dp.

3. the input constraint implies a dp of O(N * K * K) is fast enough.

 

dp[i][j]: min cost to get score j from [1, i] rectangles;

dp[i][j] = min {dp[i - 1][j - currTake] + cost[i][currTaken]}; loop on i and j, for a given score j the current rectangle i, we can take [0, j] scores from rectangle i. 

 

The remaining work is to precompute cost[i][x], the min cost of getting score x from rectangle i. 

For a given score x, it is a combination of getting score from completing a row or column. 

so we iterate over a rectangle's width and height to compute cost[i][x]. 

Don't forget to deduct the intersection between completed rows and columns as we do not need to paint these cells twice.

 

            int[][] cost = new int[n + 1][k + 1];
            for(int i = 0; i < cost.length; i++) {
                Arrays.fill(cost[i], Integer.MAX_VALUE);
                cost[i][0] = 0;
            }
            for(int i = 1; i <= n; i++) {
                for(int r = 0; r <= x[i][0]; r++) {
                    for(int c = 0; c <= x[i][1]; c++) {
                        if(r + c <= k) {
                            cost[i][r + c] = min(cost[i][r + c], r * x[i][1] + c * x[i][0] - r * c);
                        }
                    }
                }
            }

 

标签:Rows,cost,Color,CodeForces,greedy,int,score,rectangle,dp
From: https://www.cnblogs.com/lz87/p/18359992

相关文章

  • Codeforces Round 966 (Div. 3)
    A-PrimaryTask给一个数\(x\),判断其是否形如\(\overline{ab}\)满足\(a=10,b\ge2\)且无前导零。模拟判断即可。code#include<bits/stdc++.h>usingnamespacestd;constintmaxn=3e5+3;intT;stringn;voidsolve(){ cin>>n; if((n=="1"||n=="10......
  • Codeforces Round894.D
    题目:D.IceCreamBalls题目链接:https://codeforces.com/contest/1862/problem/D思路:二分找到当所有冰淇淋球类型不同的情况下,假设记位k,满足k(k-1)/2<=n;此时不一定就等于k,所以考虑到有重复类型的冰淇淋球。当有两个重复类型的球时,可做不同类型的冰淇淋有k(k-1)/2+1,若有......
  • Codeforces Round 966 (Div. 3)
    Abstract第二次打CodeForce。A.PrimaryTaskIdea签到题。意思就是说给一个字符串,要你判断一下前两位是不是10,除去前两位后后面的部分是不是一个大于等于2的数(不能有前导零)。Code#include<bits/stdc++.h>usingnamespacestd;voidsolve(){stringtext;......
  • Codeforces Round 947 (Div. 1 + Div. 2)
    [传送门](Dashboard-CodeforcesRound947(Div.1+Div.2)-Codeforces)###A.枚举一个位置,把他前面和后面反转一下判断就行。###B.找到最小的数和最小的不是它的倍数的数当作$i$和$j$,判断合不合法即可。###C.不知道怎么就模出来了操作长度一定小于等于3,然后......
  • Codeforces Round 946 (Div. 3)
    ###G.一眼看上去很想个背包,然后发现好像不大能做。考虑反悔贪心。对于当前的$c_i$,如果到现在攒的钱足够买这个,那就直接买了它。如果钱不够,就从之前的买过的里面把一个最大的给退掉,然后买这个,优先队列维护即可。```c++#include<bits/stdc++.h>#defineintlonglongu......
  • Codeforces Round 964 (Div. 4)
    ###A.```c++#include<bits/stdc++.h>usingnamespacestd;constintN=1e6+7;voidsolve(){  intx;  cin>>x;  cout<<x/10+x%10<<endl;}intmain(){  intT;  cin>>T;  while(T--)solve();......
  • 【做题记录】Codeforces Round 915 (Div. 2)/CF1905A-F
    @目录A.ConstructiveProblems(800)B.Begginer'sZelda(1100)C.LargestSubsequence(1400)D.CyclicMEX(2000)E.One-X(2400)F.FieldShouldNotBeEmpty(2600)提交记录A.ConstructiveProblems(800)注意到,对于\(n\timesn\)的矩阵,只需要把对角线全染黑即可。推广到\(......
  • Codeforces Round 903 (Div. 3) F. Minimum Maximum Distance
    https://codeforces.com/contest/1881/problem/F不难发现一件事情,我们这里最后的答案所在的点是1和3号点。我们有没有发现一个性质:就是这两个点都是红点间的路径上的,而且最后的答案就是最长的红点间的距离的长度除以二上取整。那么,我们怎么找到最长的红点间的距离呢?很显......
  • Codeforces Round 964 (Div. 4)
    CodeforcesRound964(Div.4)标题CodeForces1999A.A+BAgain?时限1second空限256megabytes题目描述给定一个两位数的正整数\(n\),求其数字之和。输入第一行包含一个整数\(t\)(\(1\leqt\leq90\))——测试用例的数量。每个测试用例的唯一一行包含一个两位数的正......
  • ProTable rowSelection 支持多选
    前言:第一次用到多选,gpt非常好用,比之前网页方便太多。 importProTablefrom'@ant-design/pro-table';importReact,{useState}from'react';constTableWithRowSelection=()=>{//使用useState钩子来保存选中的行const[selectedRows,setSelectedRows]......