CF1066D Boxes Packing

传送门

这题为什么要用二分呢?/huaji

首先可以\(O(n)\)预处理出从某个物品\(i\)开始放,只放一个盒子,能放的最后物品的位置\(j\),只要用两个指针维护左右端点,每次移动一下左端点同时尽量把右端点右移救星了

然后我们要放的所有物品是原来的一个后缀,所以要从后往前放,但是直接贪心放是错的.考虑构建一棵树,根据前面对每个\(i\)预处理出的\(j\),连\((j+1,i)\)的单向边,然后从\(n+1\)开始dfs,记\(n+1\)的深度为0,那么我们不能访问深度大于\(m\)的点.记能访问到最小的点为\(y\),答案为\(n-y+1\)

#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register
#define db double
#define eps (1e-5)

using namespace std;
const int N=200000+10;
il LL rd()
{
    LL x=0,w=1;char ch=0;
    while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*w;
}
int to[N],nt[N],hd[N],tot=1;
il void add(int x,int y){++tot,to[tot]=y,nt[tot]=hd[x],hd[x]=tot;}
int n,m,k,a[N],ans,tt=-1;
il void dfs(int x)
{
  ++tt;
  ans=max(ans,n-x+1);
  if(tt<m) for(int i=hd[x];i;i=nt[i]) dfs(to[i]);
  --tt;
}

int main()
{
  n=rd(),m=rd(),k=rd();
  for(int i=1;i<=n;i++) a[i]=rd();
  for(int i=1,su=a[1],j=1;i<=n;i++)
    {
      while(j<n&&su+a[j+1]<=k) su+=a[++j];
      add(j+1,i),su-=a[i];
    }
  dfs(n+1);
  printf("%d\n",ans);
  return 0;
}

原文地址:https://www.cnblogs.com/smyjr/p/9822262.html

时间: 2024-11-16 03:04:52

CF1066D Boxes Packing的相关文章

CF1066D Boxes Packing(二分答案)

题意描述: 你有$n$个物品,每个物品大小为$a_i$,$m$个盒子,每个盒子的容积为$k$.$Maksim$先生想把物品装入盒子中.对于每个物品,如果能被放入当前的盒子中,则放入当前盒子,否则换一个新的盒子放入.为了放入尽可能多数量的物品,$Maksim$先生会从左开始扔掉一部分物品.现在,$Maksim$先生想知道,他最多能放入盒子多少物品 输入输出格式: 输入格式: 第一行,三个整数$n$,$m$,$k$,$(1≤n,m≤2 \times 10^5$,$ 1≤k≤10^9)$含义参见上文

D. Boxes Packing

链接 [http://codeforces.com/contest/1066/problem/D] sb题 代码 #include<bits/stdc++.h> using namespace std; #define ll long long ll a[200010]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll i,j,k,n,m; cin>>n>>m>>k;

Educational Codeforces Round 34 (Rated for Div. 2) ABC

A. Hungry Student Problem Ivan's classes at the university have just finished, and now he wants to go to the local CFK cafe and eat some fried chicken. CFK sells chicken chunks in small and large portions. A small portion contains 3 chunks; a large o

uva 12657 - Boxes in a Line(AC和TLE的区别,为什么说STL慢..)

用STL中的list写的,TLE #include<cstdio> #include<iostream> #include<cstring> #include<list> #include<algorithm> using namespace std; list<int> l; list<int>::iterator it1,it2,it3,it4,it5,it; void work(int a,int a1=1,int

[LeetCode] Remove Boxes 移除盒子

Given several boxes with different colors represented by different positive numbers. You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (composed of k boxes

UVa 12657 Boxes in a Line(应用双链表)

Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to th

UVa - 103 - Stacking Boxes

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

poj 1475 Pushing Boxes

http://poj.org/problem?id=1475 Pushing Boxes Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4662   Accepted: 1608   Special Judge Description Imagine you are standing inside a two-dimensional maze composed of square cells which may or

HDU 1475 Pushing Boxes

Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ID: 147564-bit integer IO format: %lld      Java class name: Main Special Judge Imagine you are standing inside a two-dimensional maze composed of sq