Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
zhendewokusi committed Jan 5, 2024
1 parent 8757a0f commit a1a744d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
5 changes: 4 additions & 1 deletion source/_posts/epoll如何实现IO多路复用.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,7 @@ void my_data_free_callback(struct rcu_head *head) {
kfree(data);
}

```
```
[sk_data_ready](https://elixir.bootlin.com/linux/v2.6.39.4/source/net/ipv4/tcp_input.c#L5401)
[sock_def_readable](https://elixir.bootlin.com/linux/v2.6.39.4/source/net/core/sock.c#L1904)
49 changes: 48 additions & 1 deletion source/_posts/中位数贪心.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,52 @@ a0 a1 a2 a3 ... an
如果取得数字在a0到a1之间如果该数字加一依旧在a0到a1之间,那么总距离就会减少 n - 1 - 1。如果是右侧(an-1 到 an之间)也是同理。
以此类推,到达中位数的时候就是最短距离。(平均数也可以用类似的方法推导)。[简单证明](https://www.cnblogs.com/xidian-mao/p/7819928.html)

难绷的是我想到了思路,但是代码一直写不出来,很难过,还是太菜了。
难绷的是我想到了思路,但是代码一直写不出来,还是太菜了。最终代码还是抄的灵神代码(不得不说,写的比我那上不了台面的代码优雅的多)

```c
vector<int> palin;
auto init = [] {
for(int base = 1;base <= 10000;base*=10) {
// 奇数
for(int i = base;i < base * 10;i++) {
int num = i;
for(int m = i / 10;m;m /= 10) {
num = num * 10 + m % 10;
}
palin.push_back(num);
}
// 偶数
if(base <= 1000){
for(int i = base;i < base * 10;i++) {
int num = i;
for(int m = i;m;m /= 10) {
num = num * 10 + m % 10;
}
palin.push_back(num);
}
}
}
palin.push_back(1'000'000'001);
return 0;
}();
class Solution {
public:
long long minimumCost(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n = nums.size();

auto distance = [&](int i) -> long long {
long long sum = 0;
int target = palin[i];
for(auto& e : nums) {
sum += abs(e - target);
}
return sum;
};

int i = lower_bound(palin.begin(),palin.end(),nums[(n-1)/2]) - palin.begin();
if(palin[i] <= nums[n / 2]) return distance(i);
return min(distance(i),distance(i - 1));
}
};
```

0 comments on commit a1a744d

Please sign in to comment.