C(二分答案)
mid越大,操作次数越少。
求操作次数,用向上取整,(判断a[i]-mid是否大于0,)
scanf,printf防超时
l,r设置,r可以略大,l可以略小(>0)
模拟时注意有哪些限制条件,列出来以正确设置变量
#include <iostream> #include <cmath> using namespace std; int n,d; #define int long long typedef long long ll; const int N = 1e5 + 10; int a[N],k,maxn; int res; bool check(int mid){ ll cnt = 0; for(int i = 0;i < n;i++){ {//模拟中存在什么限制条件。 if(a[i] - mid > 0) d = a[i] - mid; else d = 0; cnt += (d + k - 1 -1) / (k - 1);\//分子+分母-1/分母 }//if(a[i]-mid>0) cnt+=(int)ceil(double(a[i]-mid)/(k-1)); //int宏定义成long long了 } // cout << "cnt = " << cnt << endl; if(cnt <= mid) return true; else return false; } signed main(){ while(cin >> n){ maxn = 0; for(int i = 0;i < n;i++){ scanf("%lld",&a[i]); maxn = max(a[i],maxn); } // if(sum == n * a[0]) {cout << a[0] << endl; return 0;} cin >> k; if(k == 1 ){cout << maxn << endl; continue;} ll l = 1, r= maxn+1; while(l < r){ ll mid = (l + r) >> 1; if(check(mid)) r = mid,res = mid; else l = mid + 1; // cout << "mid =" << mid << "l = " << l << "r=" << r << endl; } cout << res << endl; } return 0; }
D(贪心)
目前做的贪心思想普遍简单,不要想得太复杂
#include <iostream> #include <algorithm> typedef long long ll; using namespace std; const int N = 1e5; struct cow{ ll strength,weight; }a[N]; bool cmp(cow a,cow b){ return a.strength+a.weight > b.strength+ b.weight; } int main(){ int n; ll cnt = 0; cin >> n; for(int i = 0;i < n;i++){ cin >> a[i].weight >> a[i].strength; cnt += a[i].weight; } sort(a,a+n,cmp); ll maxn = -999999999999; for(int i = 0;i < n;i++){ cnt -= a[i].weight; maxn = max(maxn,cnt - a[i].strength); } cout << maxn ; return 0; }
总结
做的好:很快想出了大体思路
做的不好:细节几乎完全没有注意
花费了大量时间死磕。
反思:
1、卡题时直接判错,去模仿题解纠正
2、要学会找题解
3、哪怕摆烂几天都无所谓,但反思中的内容要严格执行