https://www.luogu.com.cn/problem/P1182
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
//template<typename DataType>
//DataType qmi(DataType m, int k)
//{
// DataType res = 1, t = m;
// while (k)
// {
// if (k&1) res = res * t;
// t = t * t;
// k >>= 1;
// }
// return res;
//}
int qmi(int m, int k)
{
int res = 1, t = m;
while (k)
{
if (k&1) res = res * t;
t = t * t;
k >>= 1;
}
return res;
}
int read(){
int x = 0,f = 1;
char c = getchar();
while (c<'0'||c>'9') {
if (c=='-') {
f = -1;
}
c = getchar();
}
while (c>='0'&&c<='9') {
x = x*10+c-'0';
c = getchar();
}
return x*f;
}
#define fi(a,b) for(int i = a; i < b; i++)
#define fie(a,b) for(int i = a; i <= b; i++)
#define fj(a,b) for(int j = a; j >= b; j--)
struct priceAndCnt{
int price,cnt;
};
void quickSort(priceAndCnt *a,int left,int right){
int i,j;
priceAndCnt temp,t;
temp = a[(left+right)/2];//基准值
i = left;
j = right;
while(i<=j){
while (a[j].price > temp.price) {
j--;
}
while (a[i].price < temp.price) {
i++;
}
if (i<=j) {
t = a[i];
a[i] = a[j];
a[j] = t;
//继续下一步
i++;
j--;
}
}
if(left<j)quickSort(a,left, j);//继续分治
if(i<right)quickSort(a,i, right);//继续分治
}
int n,m;//看题目啊
int lef,rig,mid;//看上面的解释啊
int total,tim;//这个不用管,后面会提到的
bool Judge(int mid,int* a)
{
fi(0,n){//这里大家应该看得出,是在用贪心来看一看这个最大值是否能为x
if(total+a[i] <= mid){
total+=a[i];
}
else {
total=a[i];
tim++;
}
}
return tim>=m;//返回结果}
}
int main()
{
n = read();
m = read();
int a[n];//这个要慎用,c99里面才可以的
fi(0, n){
a[i] = read();
rig += a[i];//每次都把right累积起来,变成所有数之和
lef = lef>a[i] ? lef : a[i];//left就是所有数中最大的那个
//似乎left和right之前解释过了哦
}
while (lef <= rig){//非递归式二分正常向写法,可理解为一般框架
int mid = (lef + rig) / 2;//这再看不出是啥意思可以退群了
total = 0;
tim = 0;
if (Judge(mid,a)){//带入judge函数判断当前解是不是可行解
lef = mid + 1;
} else {
rig = mid - 1;
}
}
cout<<lef<<endl;//输出
return 0;
}
/*
5 3
4 2 4 5 1
============
6
*/
网友评论