↓ 跳过正文
  1. Posts/

codeforces #345 div 2 C. Watchmen (容斥)

·421 字·1 分钟

题目链接 题意:求曼哈顿距离和平方根距离相等的点的对数? 思路:化简发现是绝对值乘积等于0,容斥搞搞。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年03月07日 星期一 18时43分02秒
  4File Name :code/C.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=2E6+7;
 34
 35struct node
 36{
 37    int x,y;
 38
 39    bool operator < (node b)const
 40    {
 41	if (x==b.x) return y<b.y;
 42	return x<b.x;
 43    }
 44}p[N];
 45
 46
 47
 48bool cmp( node a,node b)
 49{
 50    return a.y<b.y;
 51}
 52LL cal( LL x)
 53{
 54    LL res = x*(x+1)/2;
 55    return res;
 56}
 57int n;
 58LL a,b,c;
 59LL cnta,cntb,cntc;
 60int main()
 61{
 62	#ifndef  ONLINE_JUDGE
 63	freopen("code/in.txt","r",stdin);
 64  #endif
 65
 66	ios::sync_with_stdio(false);
 67	cin>>n;
 68	for ( int i = 1 ;i  <= n ; i++) cin>>p[i].x>>p[i].y;
 69
 70	sort(p+1,p+n+1);
 71
 72	a=b=c=0LL;
 73	cnta=cntb=cntc=0LL;
 74
 75
 76	for ( int i = 1 ; i <= n-1 ; i++)
 77	{
 78	    if (p[i].x==p[i+1].x)
 79	    {
 80		cnta++;
 81	    }
 82	    else
 83	    {
 84		a+=cal(cnta);
 85		cnta = 0 ;
 86	    }
 87	}
 88	a +=cal(cnta);
 89
 90
 91	for ( int i = 1 ; i <= n-1 ; i++)
 92	{
 93	    if (p[i].x==p[i+1].x&&p[i].y==p[i+1].y)
 94	    {
 95		cntc++;
 96	    }
 97	    else
 98	    {
 99		c +=cal(cntc);
100		cntc = 0 ;
101	    }
102	}
103
104	c+=cal(cntc);
105
106
107
108	sort(p+1,p+n+1,cmp);
109	for ( int i =  1 ; i  <= n-1 ; i ++)
110	{
111	    if (p[i].y==p[i+1].y)
112	    {
113		cntb++;
114	    }
115	    else
116	    {
117		b +=cal(cntb);
118		cntb = 0 ;
119	    }
120	}
121	b +=cal(cntb);
122
123	LL ans=0LL;
124	ans = a+b-c;
125	cout<<ans<<endl;
126
127  #ifndef ONLINE_JUDGE
128  fclose(stdin);
129  #endif
130    return 0;
131}

相关文章

hdu 5213 lucky (莫队算法)

·1153 字·3 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=5213 题意:n个数,m个查询,每个查询由4个数l1,r1,l2,r2构成,询问分别从[l1,r1]和[l2,r2]中各取一个数,和为给定的常数k的方案数。 思路:首先分别由两个区间取数不好搞,我们可以用容斥原理对区间变换。这是这道题最关键的一步。

codeforces #345 div 2 B. Beautiful Paintings (暴力)

·371 字·1 分钟
题目链接 题意:给出一个数列,按照最好的策略排序使得a[i+1]>a[i]的对数尽可能多,问最多的对数是多少。 思路:类似计数排序? 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年03月07日 星期一 17时06分48秒 4File Name :code/cf/#345/B.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=1E3+7; 34int n ; 35int a[N]; 36int cnt[N]; 37int num[N]; 38int sum[N]; 39int main() 40{ 41 #ifndef ONLINE_JUDGE 42 freopen("code/in.txt","r",stdin); 43 #endif 44 45 cin>>n; 46 ms(cnt,0); 47 ms(num,0); 48 ms(sum,0); 49 for ( int i = 1 ; i <= n ; i++) 50 { 51 cin>>a[i]; 52 cnt[a[i]]++; 53 } 54 if (n==1) 55 { 56 puts("0"); 57 return 0 ; 58 } 59 if (n==2) 60 { 61 if (a[1]==a[2]) 62 { 63 puts("0"); 64 } 65 else 66 { 67 puts("1"); 68 } 69 return 0; 70 } 71 72 int mx = -1; 73 for ( int i = 1 ; i <= 1000 ; i++) 74 { 75 num[cnt[i]]++; 76 mx = max(mx,cnt[i]); 77 } 78 79 for ( int i = mx ; i >= 1 ; i --) 80 { 81 sum[i] = sum[i+1]+num[i]; 82 } 83 84 int ans = 0 ; 85 for ( int i = 1 ; i <= mx ; i++) 86 { 87 ans +=sum[i]-1; 88// cout<<"sum[i]:"<<sum[i]<<endl; 89 } 90 cout<<ans<<endl; 91 92 93 94 #ifndef ONLINE_JUDGE 95 fclose(stdin); 96 #endif 97 return 0; 98}