题目链接:

http://codeforces.com/problemset/problem/421/D

D. Bug in Code

time limit per test 1 second
memory limit per test 256 megabytes
#### 问题描述
> Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the n coders on the meeting said: 'I know for sure that either x or y did it!'
>
> The head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders' opinions. That's why the head wants to make such a choice that at least p of n coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects?
>
> Note that even if some coder was chosen as a suspect, he can agree with the head's choice if he named the other chosen coder at the meeting.
#### 输入
> The first line contains integers n and p (3 ≤ n ≤ 3·105; 0 ≤ p ≤ n) — the number of coders in the F company and the minimum number of agreed people.
>
> Each of the next n lines contains two integers xi, yi (1 ≤ xi, yi ≤ n) — the numbers of coders named by the i-th coder. It is guaranteed that xi ≠ i,  yi ≠ i,  xi ≠ yi.
#### 输出
> Print a single integer — the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets (1, 2) and (2, 1) are considered identical.
#### 样例
> **sample input**
> 4 2
> 2 3
> 1 4
> 1 4
> 2 1
>
> **sample output**
> 6

题意

对于特定的两个人(u,v)如果有至少p个人认为其中的一个是buger,那么这两个人就有可能被叫到办公室。现在叫你求所有满足条件的(u,v)。

我们用agr[i]表示质疑i的人数,mp[u,v]记录同时质疑u、v的人数。那么对于(u,v)只要满足agr[u]+agr[v]-mp[u,v]>=p,就有可能被叫到办公室。

为了快速计算结果,这里我们做个转换、ans=sigma(agr[u]+agr[v]>=k)-sigma(agr[u]+agr[v]-mp[u,v]<k)对于agr数组,我们排个序,就能O(n)时间统计出sigma(agr[u]+agr[v]>=k)。而sigma(agr[u]+agr[v]-mp[u,v]<k)只要扫一遍mp里面的元素就可以了。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#define X first
#define Y second
#define mkp make_pair
using namespace std; const int maxn = 3e5 + 10;
typedef __int64 LL;
int n, m; int agr[maxn];
map<pair<int, int>, int> mp;
map<pair<int, int>, int>::iterator iter; int main() {
memset(agr, 0, sizeof(agr));
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
int u, v;
scanf("%d%d", &u, &v);
if (u > v) swap(u, v);
agr[u]++; agr[v]++;
mp[mkp(u, v)]++;
}
LL ans = 0;
for (iter = mp.begin(); iter != mp.end(); iter++) {
int u = (iter->X).X, v = (iter->X).Y, cnt = iter->Y;
if (agr[u] + agr[v] >= m&&agr[u] + agr[v] - cnt < m) ans--;
}
sort(agr + 1, agr + n + 1);
//for (int i = 1; i <= n; i++) printf("%d ", agr[i]);
//printf("\n");
int st = 1, ed = n;
for (; st < n; st++) {
while (ed > st&&agr[st] + agr[ed] >= m) ed--;
ans += min(n - ed,n-st);
}
printf("%I64d\n", ans);
return 0;
}

杂七杂八

这一题最关键的地方就是建模吧!把问题转换成求agr[u]+agr[v]-mp[u,v]。从而利用一些算法和套路来解决实际问题。

codeforce 421D D. Bug in Code的更多相关文章

  1. SQL Server 2017的Linked Server配置触发的bug“Exception Code = c0000005 EXCEPTION_ACCESS_VIOLATION”

    SQL Server 2017的Linked Server配置触发的bug"Exception Code    = c0000005 EXCEPTION_ACCESS_VIOLATION&q ...

  2. codeforces 421d bug in code

    题目链接:http://codeforces.com/problemset/problem/421/D 题目大意:每个人说出自己认为的背锅的两个人,最后大BOSS找两个人来背锅,要求至少符合p个人的想 ...

  3. IOS bug之Code Sign error:Provisioning profile

    刚才解决一个版本冲突的bug,记在了博客里,这让我想起了另外一个bug,当时犹豫公司的开发者账号过期了,我打开应用运行时提示Code Sign error:Provisioning profile   ...

  4. Bug in Code

    Coder-Strike 2014 - Finals (online edition, Div. 1) C:http://codeforces.com/problemset/problem/420/C ...

  5. Bug in Code CodeForces - 420C (计数,图论)

    大意: 给定$n$结点无向图, 共n条边, 有重边无自环, 求有多少点对(u,v), 满足经过u和v的边数>=p 可以用双指针先求出所有$deg_u+deg_v \ge p$的点对, 但这样会多 ...

  6. CF421D Bug in Code

    题意:n个人每人选择了另外不相同的两个人.问有多少对(x,y)使得这n个人中至少有p个选择了至少其中之一? 标程:那就不写了吧. 题解:容斥 统计Ax表示有多少个人选择了x. 一般来说有Ax+Ay&g ...

  7. Bug管理工具的使用介绍(Bugger 2016)

    1. Bugger 2016 介绍 Bugger 2016 is the version of Bugger adding support fot Team Foundation Server bug ...

  8. iOS 之 调试、解决BUG

    iOS 解决一个复杂bug 之 计分卡 iOS 调试 之 打印 iOS 错误之 NSObject .CGFloat iOS bug 之 H5 页面没有弹出提示框 iOS 日志工具 CocoaLumbe ...

  9. 如何写出没有BUG的代码

    1947年9月9日,美国海军准将 Grace Hopper 在哈佛学院计算机实验室里使用 Mark II 和 Mark III 计算机进行研究工作.她的团队跟踪到 Mark II 上的一个错误,操作人 ...

随机推荐

  1. bea weblogic workshop中文乱码

    重装系统后,weblogic 8.1 workshop中的中文字体是乱码. 可通过菜单中的 Tools -> IDE Properties -> Display, 在Window font ...

  2. UI Button

    iOS开发UI篇—Button基础 一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状 ...

  3. BF算法和KMP算法(javascript版本)

    var str="abcbababcbababcbababcabcbaba";//主串 var ts="bcabcbaba";//子串 function BF( ...

  4. Centos 7配置ntp时间同步

    1.NTP时钟同步方式说明     NTP在linux下有两种时钟同步方式,分别为直接同步和平滑同步: 1)直接同步      使用ntpdate命令进行同步,直接进行时间变更.如果服务器上存在一个1 ...

  5. 统计图表--第三方开源--MPAndroidChart(一)

    效果图1: 效果图2: MPAndroidChart是在Android平台上开源的第三方统计图表库,可以绘制样式复杂.丰富的各种统计图表,如一般常见的折线图.饼状图.柱状图.散点图.金融股票中使用的的 ...

  6. 12)Java Constructor

    Constructor        构造器Constructor不能被继承,因此不能重写Overriding,但可以被重载Overloading.     构造器用来确保每个对象都会得到初始化.当对 ...

  7. 一个自定义的C#数据库操作基础类 SqlHelper

    SqlHelper其实是我们自己编写的一个类,使用这个类目的就是让使用者更方便.更安全的对数据库的操作,既是除了在SqlHelper类以外的所有类将不用引用对数据库操作的任何类与语句,无须担心数据库的 ...

  8. android 输出.txt 文本换行问题

    // 获取当前日期和时间 Calendar cal = Calendar.getInstance(); String fileName = cal.get(Calendar.YEAR) + " ...

  9. 03-树3 Tree Traversals Again

    二叉树及其遍历 push为前序遍历序列,pop为中序遍历序列.将题目转化为已知前序.中序,求后序. 前序GLR 中序LGR 前序第一个为G,在中序中找到G,左边为左子树L,右边为右子树R. 将左右子树 ...

  10. Python多版本安装 Python2.7和Python3.5

    声明:本文仅仅在win8.1测试通过! 1.下载 Python2.7,3.5 2.依次安装Python27(c:\Python27)  Python35(c:\Python35) 3.c:\Pytho ...