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 ... 
随机推荐
- 【JZOJ3424】粉刷匠
			description 赫克托是一个魁梧的粉刷匠,而且非常喜欢思考= = 现在,神庙里有N根排列成一直线的石柱,从1到N标号,长老要求用油漆将这些石柱重新粉刷一遍.赫克托有K桶颜色各不相同的油漆,第i ... 
- SQL Server SQLGetData()
			{ /* 语法 C++ SQLRETURN SQLGetData( SQLHSTMT StatementHandle, SQLUSMALLINT Col_or_Param_Num, SQLSMALLI ... 
- BIO、NIO、AIO入门认识
			同步.异步.阻塞.非阻塞概念理解. 同步: 比如在执行某个逻辑业务,在没有得到结果之前一直处于等待阻塞状态,得到结果后才继续执行 异步: 比如在执行某个逻辑业务,在没有得到结果可以去干其他的事情,等待 ... 
- R语言中的线性判别分析_r语言 线性判别分析
			R语言中的线性判别分析_r语言 线性判别分析 在R语言中,线性判别分析(Liner Discriminant Analysis,简称LDA),依靠软件包MASS中有线性判别函数lqa()来实现.该函数 ... 
- Sequelize
			连接数据库 const DB = require('sequelize') // 连接数据库 const connect = new DB('xjg', 'root', 'root', { host: ... 
- 【转载】一定要会用selenium的等待,三种等待方式必会
			转载地址:http://blog.csdn.net/huilan_same/article/details/52544521,感谢博文,学习了 原文: 发现太多人不会用等待了,博主今天实在是忍不住要给 ... 
- 使用Image作为BackgroundColor 使用
			https://www.hackingwithswift.com/example-code/uicolor/how-to-use-an-image-for-your-background-color- ... 
- ps photoshop
			PS-前端切图教程(切jpg图和切png图) 参考线显示和隐藏:ctrol+h alt+v+e或者打开标尺然后从点击标尺就能拖拽出来,删除也是拖到标尺附近就删除 显示.隐藏标尺:ctrol+R 显示网 ... 
- Qt  plugins(插件)目录
			今天在打包Qt程序时,出现了因为缺少插件,导致背景图无法显示的问题.第一次将plugins目录全部拷贝到了应用程序根目录下,还是无法运行.查阅资料,需要拷贝plugins子目录到应用程序跟目录.虽然最 ... 
- vue  简单留言本
			代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ... 
