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 ;
}

最新文章

  1. 【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之缓存融合技术和主要后台进程(四)
  2. CSS实现内容超过长度后以省略号显示
  3. SSM集成(一):Mybatis3测试
  4. python 列表生成式
  5. 逻辑操作符---Lua: and,or,not 对比 C++:&amp;&amp;,||,!
  6. leetcode 169
  7. php -- strstr()字符串匹配函数(备忘)
  8. CSS 阴影怎么写?
  9. iptable原理
  10. JVM剖析
  11. Windows服务承载WCF
  12. SQL Server的数据加密简介
  13. Servlet 中为多项选择题判分---String类的indexOf()方法妙用
  14. java第十二次作业
  15. Python之matplotlib学习(一)
  16. cocos2D v3.4 在TileMap中开启高清显示
  17. 简单用数组模拟顺序栈(c++)
  18. BeanCopyUtil
  19. 『TensorFlow』读书笔记_AlexNet
  20. document.createRange剪贴板API

热门文章

  1. Fence Obstacle Course
  2. BZOJ 1084 (SCOI 2005) 最大子矩阵
  3. JAVA基础_反射获取泛型参数类型
  4. Mysql优化系列之索引性能
  5. npm安装vuex及防止页面刷新数据丢失
  6. Java中关于注释、标识符、变量、常量、数据类型、类型转换、转移字符以及数值型的表现形式的详解
  7. java笔试之放苹果
  8. Linux vi和vim编辑器(1)
  9. 2019-8-31-win2d-通过-CanvasActiveLayer-画出透明度和裁剪
  10. QSerialPort类