【微软2017年预科生计划在线编程笔试第二场 A】Queen Attack
2017-08-11 20:15
597 查看
题目:
http://www.mamicode.com/info-detail-1752576.html
http://blog.csdn.net/harlow_cheng/article/details/70767023
https://hihocoder.com/discuss/question/4374
c++答案:
#include<bits/stdc++.h> using namespace std; int n; struct X { int x,y; }s[100010]; bool cmp1(X a, X b) { return a.x<b.x; } bool cmp2(X a, X b) { return a.y<b.y; } bool cmp3(X a, X b) { return a.x+a.y<b.x+b.y; } bool cmp4(X a, X b) { return a.y-a.x<b.y-b.x; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&s[i].x,&s[i].y); long long sum=0; sort(s+1,s+1+n,cmp1); long long a=1; for(int i=2;i<=n;i++) { if(s[i].x==s[i-1].x) a++; else { sum=sum+a*(a-1)/2; a=1; } } sum=sum+a*(a-1)/2; sort(s+1,s+1+n,cmp2); a=1; for(int i=2;i<=n;i++) { if(s[i].y==s[i-1].y) a++; else { sum=sum+a*(a-1)/2; a=1; } } sum=sum+a*(a-1)/2; sort(s+1,s+1+n,cmp3); a=1; for(int i=2;i<=n;i++) { if(s[i].y+s[i].x==s[i-1].y+s[i-1].x) a++; else { sum=sum+a*(a-1)/2; a=1; } } sum=sum+a*(a-1)/2; sort(s+1,s+1+n,cmp4); a=1; for(int i=2;i<=n;i++) { if(s[i].y-s[i].x==s[i-1].y-s[i-1].x) a++; else { sum=sum+a*(a-1)/2; a=1; } } sum=sum+a*(a-1)/2; printf("%lld\n",sum); return 0; }
http://www.mamicode.com/info-detail-1752576.html
#include <bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define mp make_pair #define pb push_back #define fi first #define se second #define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii; typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1}; const int dy[9] = {0,0,0,-1,1,-1,1,-1,1}; const double pi = acos(-1.0); const int N = 2e4+100; int n; map <int,int> dic1,dic2,dic3,dic4; LL ans = 0; int main() { //freopen("F:\\rush.txt","r",stdin); ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use cin >> n; rep1(i,1,n) { int x,y; cin >> x >> y; ans+=dic1[x],ans+=dic2[y],ans+=dic3[x-y],ans+=dic4[x+y]; dic1[x]++,dic2[y]++,dic3[x-y]++,dic4[x+y]++; } cout << ans << endl; return 0; }
http://blog.csdn.net/harlow_cheng/article/details/70767023
python答案:
#!/usr/bin/env python # encoding: utf-8 """ @author: Aprilvkuo @file: test.py @time: 17-4-6 下午10:58 """ def count(dic): sum = 0 for (key,value) in dic.items(): sum += (value*(value-1))/2 return sum def fuc(): num = int(raw_input()) dic_x ={} dic_y ={} dic_abs={} dic_add={} for i in range(num): x,y = map(int,raw_input().split()) if dic_x.has_key(x): dic_x[x] += 1 else: dic_x[x] = 1 if dic_y.has_key(y): dic_y[y] += 1 else: dic_y[y] = 1 if dic_abs.has_key((x-y)): dic_abs[(x-y)] += 1 else: dic_abs[(x-y)] = 1 if dic_add.has_key(x+y): dic_add[x+y] += 1 else: dic_add[x+y] = 1 cnt = 0 cnt += count(dic_x) cnt += count(dic_y) cnt += count(dic_abs) cnt += count(dic_add) return cnt if __name__ == '__main__': print fuc()
https://hihocoder.com/discuss/question/4374
相关文章推荐
- 微软2017年预科生计划在线编程笔试第二场-#1497 : Queen Attack
- 【微软2017年预科生计划在线编程笔试第二场 A】Queen Attack
- Queen Attack -- 微软2017年预科生计划在线编程笔试第二场
- 微软2017年预科生计划在线编程笔试第二场-#1499 : A Box of Coins
- 微软2017年预科生计划在线编程笔试第二场B题Diligent Robots
- 【微软2017年预科生计划在线编程笔试第二场 B】Diligent Robots
- 微软2017年预科生计划在线编程笔试第二场-#1498 : Diligent Robots
- 微软2017年预科生计划在线编程笔试第二场-#1500 : EL SUENO
- 微软2017年预科生计划在线编程笔试
- [hihocoder1489][微软2017年预科生计划在线编程笔试][Legendary Items]
- 微软2017年预科生计划在线编程笔试 题目1 : Legendary Items
- 微软2017年预科生计划在线编程笔试 A Legendary Items
- 【微软2017年预科生计划在线编程笔试 B】Tree Restoration
- 微软2017年预科生计划在线编程笔试,附解题思路
- 【微软2017年预科生计划在线编程笔试 A】Legendary Items
- 微软2017年预科生计划在线编程笔试-#1489 : Legendary Items
- 微软2017年预科生计划在线编程笔试-#1491 : Monster Killing
- 微软2017年预科生计划在线编程笔试-#1490 : Tree Restoration
- 微软2017年预科生计划在线编程笔试-#1492 : Parentheses Sequence
- 微软2017年预科生计划在线编程笔试题Legendary Items