Problem B: 故障电灯(light)
- 考虑对电灯进行差分:若第i个电灯和第i + 1个电灯状态不同,则在第i个位置上放一个球
这样我们就放置了不超过2n个球,且必然是偶数个
于是问题转化为:有m个球,每一步可以把一个球平移奇质数个位置,两个球位于相同位置则同时被消除,计算至
少多少步能消除所有球 - 然后我们发现, 假如两个东西距离为奇质数cost为1, 偶数cost为二(哥德巴赫猜想), 其余奇数的话cost为3
- 然后发现一种贪心方法, 是尽量匹配cost为1的, 然后分奇偶性各自用2覆盖,看看最后剩下的那个直接判断即可
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define ll long long
#define mmp make_pair
#define M
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
struct Edge{
int v, c, nxt;
}e[200010];
int head[10010], tot = 1;
void build(int u, int v, int c)
{
e[++tot] = (Edge){v, c, head[u]};
head[u] = tot;
return ;
}
void insert(int u, int v, int c)
{
build(u, v, c);
build(v, u, 0);
return ;
}
int dep[10010];
int S, T;
queue<int> q;
bool bfs()
{
memset(dep, 0, sizeof(dep));
dep[S] = 1;
q.push(S);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = head[u]; i; i = e[i].nxt)
if(!dep[e[i].v]&&e[i].c)
{
dep[e[i].v] = dep[u] + 1;
q.push(e[i].v);
}
}
if(dep[T])return true;
return false;
}
int cur[10010];
int dfs(int u, int flow)
{
if(u == T)return flow;
for(int &i = cur[u]; i; i = e[i].nxt)
if(e[i].c&&dep[e[i].v] == dep[u] + 1)
{
int d = dfs(e[i].v, min(e[i].c, flow));
if(d){
e[i].c -= d;
e[i^1].c += d;
return d;
}
}
return 0;
}
int dinic()
{
int ans = 0;
while(bfs())
{
for(int i = S; i <= T; i++)
cur[i] = head[i];
int d;
while(d = dfs(S, 1e9))ans += d;
}
return ans;
}
int prime[1000010], cnt;
bool vis[10000010];
int x[1010];
int posx[1010], posy[1010];
int cnt1, cnt2;
void push(int x)
{
if(x&1)posx[++cnt1] = x;
else posy[++cnt2] = x;
}
int main(){
int N = 10000000;
vis[1] = true;
for(int i = 2; i <= N; i++)
{
if(!vis[i])prime[++cnt] = i;
for(int j = 1; j <= cnt&&i * prime[j] <= N; j++)
{
vis[i * prime[j]] = true;
if(i%prime[j] == 0)break;
}
}
vis[2] = true;
int t = read();
while(t--)
{
memset(head, 0, sizeof(head));
cnt1 = cnt2 = 0; tot = 1;
int n = read();
for(int i = 1; i <= n; i++)
{
x[i] = read();
if(i == 1||x[i - 1] != x[i] - 1)push(x[i]);
if(i> 1&&x[i - 1] != x[i] - 1)push(x[i - 1] + 1);
}
push(x[n] + 1);
S = 0, T = cnt1 + cnt2 + 1;
for(int i = 1; i <= cnt1; i++)
insert(S, i, 1);
for(int i = 1; i <= cnt1; i++)
for(int j = 1; j <= cnt2; j++)
if(!vis[max(posx[i] - posy[j], posy[j] - posx[i])])
insert(i, cnt1 + j, 1);
for(int i = 1; i <= cnt2; i++)
insert(cnt1 + i, T, 1);
int ans = dinic();
printf("%d\n", ans + (cnt1 - ans)/2 * 2 + (cnt2 - ans)/2 * 2 + (cnt1 - ans)%2 * 3);
}
return 0;
}
Problem B: 故障电灯(light)的更多相关文章
- Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...
- 设计模式(15)-Facade Pattern
http://www.cnblogs.com/zhenyulu/articles/55992.html 一. 门面(Facade)模式 外部与一个子系统的通信必须通过一个统一的门面(Facade)对象 ...
- 门面(Facade)模式(转)
转载:http://www.cnblogs.com/skywang/articles/1375447.html 外部与一个子系统的通信必须通过一个统一的门面(Facade)对象进行,这就是门面模式. ...
- java并发 使用ScheduledExecutor的温室控制器--thinking in java 21.7.5
package org.rui.thread.newc; import java.text.DateFormat; import java.text.SimpleDateFormat; import ...
- hdoj Pipe&&南阳oj管道问题&&poj1039(计算几何问题...枚举)
Pipe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- js原生之一个面向对象的应用
function IElectricalEquipment() { } IElectricalEquipment.prototype = { poweron: fu ...
- 自学Zabbix3.8.2-可视化Visualisation-maps网络地图
自学Zabbix3.8.2-可视化Visualisation-maps网络地图 可以简单的理解为动态网络拓扑图,可以针对业务来配置zabbix map,通过map可以了解应用的整体状况:服务器是否异常 ...
- 011.Zabbix的拓扑创建
一 Map简介 Map的作用是将各种设备用网络拓扑图的方式展示,在Zabbix中,拓扑的展示通过手动方式添加. 二 Map的添加 2.1 添加Map的背景图 #在添加Map之前可谓Map添加一个背景图 ...
- (58)zabbix网络拓扑图配置network map
zabbix网络地图介绍 “zabbix network map”可以简单的理解为动态网络拓扑图,可以针对业务来配置zabbix map, 通过map可以了解应用的整体状况:服务器是否异常.网络是否有 ...
随机推荐
- CentOS 7 / RHEL 7:修改OpenSSH 默认端口
1.备份sshd_config cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.orig.$(date +%F) 2.vi /etc/ssh/sshd_ ...
- HDU - 6080 :度度熊保护村庄 (凸包,floyd最小环)(VJ1900题达成)
pro:二维平面上,给定N个村庄.M个士兵驻守,把村庄围住,现在我们想留下更多的士兵休息,使得剩下的士兵任然满足围住村庄.N,M<500: sol:即是要找一个最小的环,环把村庄围住. 由于是环 ...
- Python全栈之路----常用模块----序列化(json&pickle&shelve)模块详解
把内存数据转成字符,叫序列化:把字符转成内存数据类型,叫反序列化. Json模块 Json模块提供了四个功能:序列化:dumps.dump:反序列化:loads.load. import json d ...
- apply,call以及bind的区别
每个函数都包含两个非继承而来的方法:apply()和 call(). 这两个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内 this 对象的值. 一.apply() apply()方法 ...
- 实验三 CC2530平台上CC2530平台上定时器组件的
实验三 CC2530平台上CC2530平台上定时器组件的TinyOS编程 实验目的: 加深和巩固学生对于TinyOS编程方法的理解和掌握 让学生初步掌握CC2530定时器的PWM功能,及其TinyOS ...
- FragmentXlistview
package com.example.lenovo.tablayout; /** * Created by lenovo on 2018/7/18. */ import android.os.Asy ...
- LeetCode 922. Sort Array By Parity II C++ 解题报告
922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...
- Leetcode 两数之和 (散列表)
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- Python基础:三、Python的解释器
当我们编写python代码的时候,我们得到的是一个包含python代码的以.py为拓展名的文本文件,要运行代码,就需要python解释器去执行.py文件. 由于整个python语言从规范到解释器都是开 ...
- 截取所有的winform runtime error
https://stackify.com/csharp-catch-all-exceptions/ AppDomain.CurrentDomain.FirstChanceException += (s ...