【codeforces 812C】Sagheer and Nubian Market
and Codeforces
2023-09-14 09:03:50 时间
【题目链接】:http://codeforces.com/contest/812/problem/C
【题意】
给你n个物品;
你可以选购k个物品;则
每个物品有一个基础价值;
然后还有一个附加价值;
即为k*i;
这里i是这个物品的下标;(1..n中的某个整数);
然后你有预算S;
问你最多能买多少个物品ans;
并求出买ans个物品的最小花费;
【题解】
二分买的物品数量k;
然后就能获取出每个物品的价格了;
即a[i]+i*k;
放到b数组里面;
升序排;
然后选取前k个;
看看超不超预算;
不超就记录答案,并让物品数量变大;
否则变小;
貌似在算的时候会爆int;
【Number Of WA】
1
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
LL n,s,tans;
LL a[N],b[N];
bool ok(LL k){
for (LL i = 1;i <= n;i++){
b[i] = a[i]+i*k;
}
sort(b+1,b+1+n);
LL temp = 0;
rep1(i,1,k){
temp+=b[i];
if (temp>s) return false;
}
tans = temp;
return true;
}
int main(){
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n >> s;
rep1(i,1,n){
cin >> a[i];
}
int l = 0,r = n,ans = 0;
while (l <= r){
int mid = (l+r)>>1;
if (ok(mid)){
ans = mid;
l = mid+1;
}
else
r = mid-1;
}
cout << ans <<' '<<tans<<endl;
return 0;
}
相关文章
- MOB LEC10 LIDAR, Point Cloud and Iterative Closest Points
- A. Vasya and Book ( Codeforces Educational Codeforces Round 55 )
- ORA-29256: Cursor contains both regular and array defines which is illegal ORACLE 报错 故障修复 远程处理
- ORA-13768: Snapshot ID must be between string and string. ORACLE 报错 故障修复 远程处理
- FunDA(17)- 示范:异常处理与事后处理 – Exceptions handling and Finalizers详解编程语言
- Partition Array by Odd and Even详解编程语言
- MySQL中的OR与AND操作符比较(mysqlor和and)
- 条件MySQL 子句之间`AND`操作符多条件查询(mysql多个and)
- Linux TGZ: All You Need to Know About Tar and Gzip Compression!(linuxtgz)
- Maximizing Your Database with Oracle Capacity Management: Tips and Strategies(oracle容量)
- Oracle数据库中的AND查询(oracle and查询)
- MySQL中如何正确使用AND运算符(mysql中and怎么用)
- MySQL中的AND和OR使用逻辑运算符优化查询语句(mysql中and与or)