nowcoder牛客wannafly挑战赛20
A---染色
签到题,设最终颜色为x,一次操作就需要把一个不是x的点变为x,所以最终颜色为x时需要操作 总结点个数-颜色为x的节点个数,然后枚举所有颜色就行了
#include <iostream>
#include <string.h>
#include <cstdio>
#include <vector>
#include <map>
#include <math.h>
#include <string>
#include <algorithm>
#include <time.h> #define SIGMA_SIZE 26
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#define foe(i, a, b) for(int i=a; i<=b; i++)
#define fo(i, a, b) for(int i = a; i < b; i++);
#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a,LL b) { return a>b?a:b; }
inline LL LMin(LL a,LL b) { return a>b?b:a; }
inline LL lgcd( LL a, LL b ) { return b==?a:lgcd(b,a%b); }
inline LL llcm( LL a, LL b ) { return a/lgcd(a,b)*b; } //a*b = gcd*lcm
inline int Max(int a,int b) { return a>b?a:b; }
inline int Min(int a,int b) { return a>b?b:a; }
inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 1e6+;
const int maxn = 1e5+; int N, ct;
int cnt[maxn];
LL num[maxn];
LL all;
struct col {
int id;
LL val;
col() {}
}color[maxn]; bool cmp(const col& a, const col& b)
{
return a.val < b.val;
} void read()
{
memset(cnt, , sizeof(cnt));
memset(num, , sizeof(num));
all = ; foe(i, , N)
{
scanf("%lld", &color[i].val);
all += color[i].val;
color[i].id = i;
}
sort(color+, color++N, cmp); int x, y;
foe(i, , N-)
{
scanf("%d %d", &x, &y);
}
} int main()
{
while (~scanf("%d", &N))
{
read(); LL tmp = color[].val;
num[] = color[].val;
cnt[] = ;
ct = ;
foe(i, , N)
{
if (color[i].val == tmp)
{
cnt[ct]++;
continue;
} tmp = color[i].val;
num[++ct] = tmp;
cnt[ct] = ;
} LL mmin = INF;
foe(i, , ct)
{
tmp = all;
tmp -= num[i]*cnt[i];
if (tmp >= mmin) continue;
tmp += (N-cnt[i])*num[i];
mmin = LMin(tmp, mmin);
}
printf("%lld\n", mmin);
}
return ;
}
B---背包
我觉得这道题有点问题...先按价值排个序,然后考虑中位数两边最小的体积是多少呢,用优先队列从1~N扫一遍,再从N~1扫一遍,就可以记录下每个点往左和往右的所有物品中,取体积最小的M/2个物品的体积总和是多少,分别记为sum1[i],sum2[i],然后分奇偶讨论一下就行了(还是觉得数据有点问题,)
#include <iostream>
#include <string.h>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <math.h>
#include <string>
#include <algorithm>
#include <time.h> #define SIGMA_SIZE 26
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#define foe(i, a, b) for(int i=a; i<=b; i++)
#define fo(i, a, b) for(int i = a; i < b; i++);
#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a, LL b) { return a>b ? a : b; }
inline LL LMin(LL a, LL b) { return a>b ? b : a; }
inline LL lgcd(LL a, LL b) { return b == ? a : lgcd(b, a%b); }
inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; } //a*b = gcd*lcm
inline int Max(int a, int b) { return a>b ? a : b; }
inline int Min(int a, int b) { return a>b ? b : a; }
inline int gcd(int a, int b) { return b == ? a : gcd(b, a%b); }
inline int lcm(int a, int b) { return a / gcd(a, b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 1e6 + ;
const int maxn = 1e5 + ; //优先队列默认从大到排列
priority_queue<LL> qq;
int V, N, M;
LL sum;
LL sum1[maxn], sum2[maxn];
struct node {
LL val, cost;
}pp[maxn]; bool cmp(const node& a, const node& b)
{
return a.val == b.val ? a.cost<b.cost : a.val<b.val;
} void init()
{
foe(i, , N)
scanf("%lld %lld", &pp[i].val, &pp[i].cost);
sort(pp + , pp + + N, cmp);
} void solve(int i, int mid, LL ss[])
{
ss[i] = sum; sum += pp[i].cost;
qq.push(pp[i].cost); if (qq.size() > mid) {
sum -= qq.top();
qq.pop();
}
} int main()
{
cin >> V >> N >> M; init(); sum = ;
int mid = M / ;
//正向扫一遍
for (int i = ; i <= N; i++)
solve(i, mid, sum1); sum = ; while (!qq.empty()) qq.pop();
//反向扫一遍
for (int i = N; i >= ; i--)
solve(i, mid, sum2); if (M % )
{
for (int i = N - mid; i >= mid + ; i--)
if (sum1[i] + sum2[i] + pp[i].cost <= V)
{
printf("%lld\n", pp[i].val);
break;
}
}
else
{
for (int i = N - mid + ; i >= mid; i--)
if (sum1[i] + sum2[i - ] <= V)
{
printf("%lld\n", (pp[i].val + pp[i - ].val) / );
break;
} } return ;
}
nowcoder牛客wannafly挑战赛20的更多相关文章
- 牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树)
牛客wannafly 挑战赛14 B 前缀查询(trie树上dfs序+线段树) 链接:https://ac.nowcoder.com/acm/problem/15706 现在需要您来帮忙维护这个名册, ...
- 牛客~~wannafly挑战赛19~A 队列
链接:https://www.nowcoder.com/acm/contest/131/A来源:牛客网 题目描述 ZZT 创造了一个队列 Q.这个队列包含了 N 个元素,队列中的第 i 个元素用 Qi ...
- 牛客Wannafly挑战赛23 B.游戏
游戏 题目描述 小N和小O在玩游戏.他们面前放了n堆石子,第i堆石子一开始有ci颗石头.他们轮流从某堆石子中取石子,不能不取.最后无法操作的人就输了这个游戏.但他们觉得这样玩太无聊了,更新了一下规则. ...
- 牛客 Wannafly 挑战赛26D 禁书目录 排列组合 概率期望
原文链接https://www.cnblogs.com/zhouzhendong/p/9781060.html 题目传送门 - NowCoder Wannafly 26D 题意 放一放这一题原先的题面 ...
- 牛客Wannafly挑战赛26E 蚂蚁开会(树链剖分+线段树)
传送门 题面描述 一颗n个节点的树,m次操作,有点权(该节点蚂蚁个数)和边权(相邻节点的距离). 三种操作: 操作1:1 i x将节点i的点权修改为x.(1 <= i <= n; 1 &l ...
- 牛客Wannafly挑战赛13-BJxc军训-费马小定理、分式取模、快速幂
参考:https://blog.csdn.net/qq_40513946/article/details/79839320 传送门:https://www.nowcoder.com/acm/conte ...
- 【牛客Wannafly挑战赛12】 题解
传送门:https://www.nowcoder.com/acm/contest/79#question 说是比赛题解,其实我只会前三题: 后面的一定补 T1 题意,在一个长度为n的时间内,问如何选择 ...
- 牛客Wannafly挑战赛11E 白兔的刁难
传送门 如果大力推单位根反演就可以获得一个 \(k^2logn\) 的好方法 \[ans_{t}=\frac{1}{k}\sum_{i=0}^{k-1}(w_k^{-t})^i(w_k^i+1)^n\ ...
- 牛客Wannafly挑战赛23F 计数(循环卷积+拉格朗日插值/单位根反演)
传送门 直接的想法就是设 \(x^k\) 为边权,矩阵树定理一波后取出 \(x^{nk}\) 的系数即可 也就是求出模 \(x^k\) 意义下的循环卷积的常数项 考虑插值出最后多项式,类比 \(DFT ...
随机推荐
- NPM一Node包管理和分发工具
NPM 全称 Node Package Manager Node包管理和分发工具,可以把NPM理解为前端的Maven 我们通过npm可以很方便地下载js库,管理前端工程 最近版本的node.js已经集 ...
- 0925CSP-S模拟测试赛后总结
献上了自己的第二次爆零. 最近考试持续低迷.受同桌影响是一方面,自己的状态不行也是一方面,根本还是实力不行. 昨天T1是签到题.然而并没有发现这个事实.并不会打…… 无意围观同桌秒切T1,秒过样例,长 ...
- python定时任务模块APScheduler
一.简单任务 定义一个函数,然后定义一个scheduler类型,添加一个job,然后执行,就可以了 5秒整倍数,就执行这个函数 # coding:utf-8 from apscheduler.sche ...
- 学习servlet之路--javax-servlet不存在
我在编译一个servlet菜鸟教程下提供的包含有javax.servlet包的java文件,变异出错, import java.io.*;import javax.servlet.*;import j ...
- Astar伪代码
while(OPEN!=NULL) { 从OPEN表中取估价值f最小的节点n; if(n节点==目标节点){ break; } for(当前节点n 的每个子节点X) { 算X的估价值; if(X in ...
- vue打包后index.html界面报错
vue项目完成后,打包放到服务器上,打开index.html页面时发现一片空白并且报错 很明显是js和css引用不到. 解决办法: 修改vue项目config文件夹下面的index.js,将asset ...
- vue+element的el-menu组件实现路由跳转及当前项的设置
<el-menu router :default-active="$route.path" class="el-menu-vertical-demo" @ ...
- selenium基础(警告框的处理)
selenium基础(警告框的处理) 在webdriver中处理JavaScript所产生的的警告框有三种类型 alert confirm prompt 划转到警告框的方法是:driver.switc ...
- UMP系统架构 Mnesia
- 【左偏树】[LuoguP1456] Monkey King
多...多组数据... awsl 死命的MLE,原来是忘记清空数组了.... 左偏树模板? 对于每一个操作,我们把两个节点$x,y$的祖先$fx,fy$找到,然后把他们的左右儿子分别合并 最后把$v[ ...