F. Color Rows and Columns
1.B. Rudolf and 1212.C. Rudolf and the Ugly String3.A. Rudolf and the Ticket4.D. Rudolf and the Ball Game5.G. Rudolf and Subway6.C. Choose the Different Ones!7.B. Following the String8.E. Klever Permutation9.D. Find the Different Ones!10.B. YetnotherrokenKeoard11.C. Removal of Unattractive Pairs12.D. Jumping Through Segments13.E. Good Triples14.F. Shift and Reverse15.C. Nikita and LCM16.B. Prefiquence17.E. Vlad and a Pair of Numbers18.D. XOR Construction19.C. Non-coprime Split20.D. Divide and Equalize21.E. Block Sequence22.D. In Love23.C. Theofanis' Nightmare24.D. Yet Another Monster Fight25.C. Count Triangles26.E. Count Paths27.Collecting Numbers II28.C. Even Subarrays29.C. Perform Operations to Maximize Score
30.F. Color Rows and Columns
31.C. To Become Max题解
本质:贪心+dp
首先当我们面对一个矩形时,肯定是不停的枚举其最小边使得score上涨。
为什么面对多个矩形不行呢?我们可以注意观察到最后一组样例的答案是 35 而非36。
那么此时我们知晓了每个矩形 得到score 分的操作数 设为cost [ n ][ score ]。
接下来问题就简化为了在n个矩形中选出最优的 k 分。
很明显我们考虑dp。
dp [ i ] [ score ] 的含义为从 i ... n 的矩形中得到 score 分所需的操作数。
Ps:dp表可以从二维优化到一维。
code
#include<bits/stdc++.h> using namespace std; const int N=1005; int cost[N][205]; int dp[105]; struct node{ int a,b; }; node rect[N]; int n,k; void Init(){ for (int i=1;i<=n;i++){ int a=rect[i].a,b=rect[i].b; cost[i][a+b]=a*b,cost[i][a+b-1]=a*b; int score=0,cos=0; while (a>1 || b>1){ if (a>b) swap(a,b); cos+=a; score++; b-=1; cost[i][score]=cos; // cout<<a<<" "<<b<<endl; } } } void solve(){ cin>>n>>k; for (int i=1;i<=n;i++){ cin>>rect[i].a>>rect[i].b; } Init(); for (int i=0;i<=k;i++) dp[i]=100000; int a=rect[n].a,b=rect[n].b; for (int z=min(k,a+b);z>=0;z--) dp[z]=cost[n][z]; for (int i=n-1;i>=1;i--){ a=rect[i].a,b=rect[i].b; // cout<<i<<" "<<a<<" "<<b<<endl; for (int j=k;j>=0;j--){ for (int z=a+b;z>=0;z--){ dp[j]=min(dp[j],dp[max(0,j-z)]+cost[i][z]); } } } cout<<(dp[k]==100000 ? -1 : dp[k])<<endl; } int main(){ int t; cin>>t; while (t--){ solve(); } return 0; }
合集:
codeforces
, dp
分类:
刷题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架