Damn Couples ZOJ - 3161
题目大意
N个人,M组关系,每次选一种关系,如果两个人相邻,则任意删除其中一个,否则不变。问最坏情况下最多能剩多少人。
分析
为了留的人最多,我们可以先将原来不相邻的关系全部说完,这样我们只需要考虑以怎样的顺序说剩下的那些即可。由于剩下的关系可能被分成一段一段的,所以我们只需要预处理在一个均相连的区间内有k个人最坏情况最多剩下多少人即可。经过考虑我们发现从中间将这一组关系分成两段可以使结果最优,所以我们就得到了递推式子,详见代码。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define sp cout<<"---------------------------------------------------"<<endl;
int id[],cnt;
struct node {
int x,y;
}d[];
inline bool cmp(const node a,const node b){
return a.x<b.x;
}
int main(){
int n,m,i,j,k=,x,y;
id[]=,id[]=,id[]=,id[]=;
for(i=;i<=;i++)
if(i%)id[i]=id[(i-)/-]+id[(i-)/+]+;
else id[i]=id[i/-]+id[i/]+;
while(scanf("%d%d",&n,&m)!=EOF){
cnt=;
for(i=;i<=m;i++){
scanf("%d%d",&x,&y);
if(y<x)swap(x,y);
if(y-x!=)continue;
d[++cnt].x=x,d[cnt].y=y;
}
sort(d+,d+cnt+,cmp);
int ans=n,len=;
d[].y=d[].x;
for(i=;i<=cnt;i++){
if(d[i].x!=d[i-].y)ans-=id[len],len=;
else len++;
}
ans-=id[len];
cout<<ans<<endl;
}
return ;
}
Damn Couples ZOJ - 3161的更多相关文章
- ZOJ 3161 Damn Couples 动态规划 难度:2
Damn Couples Time Limit: 1 Second Memory Limit: 32768 KB As mentioned in the problem "Coup ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
- ZOJ Problem Set - 1049 I Think I Need a Houseboat
这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...
- ZOJ Problem Set - 1006 Do the Untwist
今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...
- ZOJ Problem Set - 1001 A + B Problem
ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...
- zoj 1788 Quad Trees
zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...
随机推荐
- xml获取指定节点的路径
引用自http://www.w3school.com.cn/xpath/xpath_syntax.asp XPath 语法 Previous Page Next Page XPath 使用路径表达式来 ...
- spring学习-4
bean的作用域 使用bean的scope属性来配置bean的作用域 scope="singleton":默认是单例模式即容器初始化创建bean实例,在整个容器的生命周期内只创建这 ...
- 线程存储(Thread Specific Data)
线程中特有的线程存储, Thread Specific Data .线程存储有什么用了?他是什么意思了? 大家都知道,在多线程程序中,所有线程共享程序中的变量.现在有一全局变量,所有线程都可以使用它, ...
- AJAX的最小单元
$(function(){ $('#send').click(function(){ $.ajax({ type: "GET", url: "test.json" ...
- 【LeetCode】004. Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- C/C++变量命名规则,个人习惯总结【转载】
C_C++变量命名规则 原文地址:http://blog.sina.com.cn/s/blog_8a7012cf01017h9p.html 变量命名规则是为了增强代码的可读性和容易维护性.以下为C++ ...
- 洛谷【P1886】滑动窗口
浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://www.luogu.org/problemnew/show/P1886 ...
- 第十篇 javascript基础语法
1)基本概念 a) 语句(statement)是为了完成某种任务而进行的操作,语句以分号结尾,一个分号就表示一个语句结束.:如:var a = 1; b) 表达式(expression),指一个为了得 ...
- FPGA 竞争与冒险
一,概念 在数字电路设计时,无论是组合.时序,还是FPGA电路中,都需要考虑竞争冒险现象(Race and Competition). 竞争:由于信号在传输和处理过程中经过不同的逻辑门.触发器或逻辑单 ...
- 锁存器 Latch v.s. 触发器 Flip-Flop
转载 http://guqian110.github.io/pages/2014/09/23/latch_versus_flip_flop.html 根据 Wiki: Flip-flop (elec ...