Coder-Strike 2014 - Finals (online edition, Div. 1) C. Bug in Code

2024-08-24 12:08

本文主要是介绍Coder-Strike 2014 - Finals (online edition, Div. 1) C. Bug in Code,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

水题一道,但是卡了一个早上

我本着数据结构去做的,知道用树状数组就是没有想出来维护的方法....

用树状数组维护,提名 i 次的人有getsum个

注意一点的是,如果怀疑的人是 1 2,然后有一个人是同时提名这两个的,那么这个人就会被算两次

因此我每次用树状数组统计的时候,将同时和第i个人提名的各个人数减1,再统计,然后再恢复(这是一种很麻烦的做法,我是渣渣别介意

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define eps 1e-9
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62
#define pb(x) push_back(x)using namespace std;typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;template<class T>
inline bool read(T &n)
{T x = 0, tmp = 1;char c = getchar();while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();if(c == EOF) return false;if(c == '-') c = getchar(), tmp = -1;while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();n = x*tmp;return true;
}
template <class T>
inline void write(T n)
{if(n < 0){putchar('-');n = -n;}int len = 0,data[20];while(n){data[len++] = n%10;n /= 10;}if(!len) data[len++] = 0;while(len--) putchar(data[len]+48);
}
//-----------------------------------#define lowbit(x) ((i)&(-i))const int MAXN = 300010;
int n, p;
int num[MAXN], c[MAXN];
vi e[MAXN];ll getsum(int i)
{ll res = 0;for(i++; i; i -= lowbit(i))res += c[i];return res;
}void update(int i, int x)
{for(i++; i < MAXN; i += lowbit(i))c[i] += x;
}int main()
{read(n),read(p);for(int i=0; i<n; i++){int a, b;read(a), read(b);e[a].pb(b);e[b].pb(a);}int cnt=0;ll ans=0;for(int i=1; i<=n; i++){num[i]=e[i].size();update(num[i], 1);}for(int i = 1; i <= n; i++){for(int j=0; j<e[i].size(); j++){update(num[e[i][j]], -1);update(--num[e[i][j]], 1);}if(num[i] >= p)ans+=n-1;elseans+=n-1-getsum(p-num[i]-1)+(num[i] <= p-num[i]-1);for(int j=0; j<e[i].size(); j++){update(num[e[i][j]], -1);update(++num[e[i][j]], 1);}}write(ans/2),putchar('\n');return 0;
}

下面的代码是S菊苣的解答

他说用的是容斥原理,思路和上面一样的

#include<bits/stdc++.h>
using namespace std;
map<pair<int,int>,int> mp;
const int MAXN=300005;
int x[MAXN], y[MAXN];
int cnt[MAXN], cnt2[MAXN];
int main(){int n, p;cin>>n>>p;mp.clear();for(int i=0; i<n; i++){scanf("%d%d",&x[i],&y[i]);--x[i]; --y[i];cnt[x[i]]++; cnt[y[i]]++;cnt2[x[i]]++; cnt2[y[i]]++;if(x[i]>y[i]) swap(x[i],y[i]);mp[make_pair(x[i],y[i])]++;}sort(cnt,cnt+n);long long ans=0;for(int i=n-1; i>=0; i--){int c=lower_bound(cnt,cnt+i,p-cnt[i])-cnt;ans+=i-c;}for(map<pair<int,int>,int>::iterator it=mp.begin(); it!=mp.end(); it++){int f=it->first.first;int s=it->first.second;int d=it->second;if(cnt2[f]+cnt2[s]-d<p&&cnt2[f]+cnt2[s]>=p) ans--;}cout<<ans<<endl;return 0;
}


这篇关于Coder-Strike 2014 - Finals (online edition, Div. 1) C. Bug in Code的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1102437

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

ZOJ Monthly, August 2014小记

最近太忙太忙,只能抽时间写几道简单题。不过我倒是明白要想水平提高不看题解是最好的了。 A  我只能死找规律了,无法证明 int a[50002][2] ;vector< vector<int> > gmax , gmin ;int main(){int n , i , j , k , cmax , cmin ;while(cin>>n){/* g

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

2014 Multi-University Training Contest 8小记

1002 计算几何 最大的速度才可能拥有无限的面积。 最大的速度的点 求凸包, 凸包上的点( 注意不是端点 ) 才拥有无限的面积 注意 :  凸包上如果有重点则不满足。 另外最大的速度为0也不行的。 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct poin

2014 Multi-University Training Contest 7小记

1003   数学 , 先暴力再解方程。 在b进制下是个2 , 3 位数的 大概是10000进制以上 。这部分解方程 2-10000 直接暴力 typedef long long LL ;LL n ;int ok(int b){LL m = n ;int c ;while(m){c = m % b ;if(c == 3 || c == 4 || c == 5 ||

2014 Multi-University Training Contest 6小记

1003  贪心 对于111...10....000 这样的序列,  a 为1的个数,b为0的个数,易得当 x= a / (a + b) 时 f最小。 讲串分成若干段  1..10..0   ,  1..10..0 ,  要满足x非递减 。  对于 xi > xi+1  这样的合并 即可。 const int maxn = 100008 ;struct Node{int

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

Debugging Lua Project created in Cocos Code IDE creates “Waiting for debugger to connect” in Win-7

转自 I Installed Cocos Code IDE and created a new Lua Project. When Debugging the Project(F11) the game window pops up and gives me the message waiting for debugger to connect and then freezes. Also a

LLVM入门2:如何基于自己的代码生成IR-LLVM IR code generation实例介绍

概述 本节将通过一个简单的例子来介绍如何生成llvm IR,以Kaleidoscope IR中的例子为例,我们基于LLVM接口构建一个简单的编译器,实现简单的语句解析并转化为LLVM IR,生成对应的LLVM IR部分,代码如下,文件名为toy.cpp,先给出代码,后面会详细介绍每一步分代码: #include "llvm/ADT/APFloat.h"#include "llvm/ADT/S