newcoder假日团队赛8 H-Costume Party
链接:https://ac.nowcoder.com/acm/contest/1069/H
时间限制:C/C++ 1秒,其他语言2秒
64bit IO Format: %lld
题目描述
输入描述:
* Line 1: Two space-separated integers: N and S
* Lines 2..N+1: Line i+1 contains a single integer: Li
输出描述:
* Line 1: A single integer representing the number of pairs of cows FJ can choose. Note that the order of the two cows does not matter.
输入
4 6
3
5
2
1
输出
4
说明
The four pairs are as follows: cow 1 and cow 3; cow 1 and cow 4; cow 2 and cow 4; and finally cow 3 and cow 4.
算法思想
采用哈希的办法先用计数数组把当前状态的分布情况存储下来
压缩状态从n^2到m
从上界的一半开始 往左边移动基准
对于每一个基准 i ,在 i ~ limit - i 范围内(除去当前的元素)的元素都可以与当前元素配对
每次可以匹配的元素个数用前缀和的形式不断更新
时间复杂度O(m)
1 #include <bits/stdc++.h>
2 #include <iostream>
3 #include <cstring>
4 #include <stack>
5 #include <cstdlib>
6 #include <queue>
7 #include <cmath>
8 #include <cstdio>
9 #include <algorithm>
10 #include <string>
11 #include <vector>
12 #include <list>
13 #include <iterator>
14 #include <set>
15 #include <map>
16 #include <utility>
17 #include <iomanip>
18 #include <ctime>
19 #include <sstream>
20 #include <bitset>
21 #include <deque>
22 #include <limits>
23 #include <numeric>
24 #include <functional>
25
26 #define gc getchar()
27 #define mem(a) memset(a,0,sizeof(a))
28 #define mod 1000000007
29 #define sort(a,n,int) sort(a,a+n,less<int>())
30 #define fread() freopen("in.in","r",stdin)
31 #define fwrite() freopen("out.out","w",stdout)
32 using namespace std;
33
34 typedef long long ll;
35 typedef char ch;
36 typedef double db;
37
38 const int maxn=1e5+10;
39 const double pi=acos(-1.0);
40 const int N=1e5+10;
41
42
43 ll l[1000005] = {0};
44 int main()
45 {
46 ll n = 0 , s = 0 , L = 0;
47 cin >> n >> s;
48 for(int i = 1;i<=n;i++)
49 {
50 cin >> L;
51 l[L]++;
52 }
53 int counter = 0;
54 if(s % 2)
55 {
56 int right = 0;
57 for(ll i = s/2;i>=1;i--)
58 {
59 right += l[s-i] +l[i] -1;
60 counter += l[i] * right -l[i]*(l[i]-1)/2;
61 right += 1;
62 }
63 }
64 else
65 {
66 if(l[s/2] > 1)
67 counter += l[s/2]*(l[s/2]-1)/2;
68 int right = l[s/2];
69 for(ll i = s/2-1;i>=1;i--)
70 {
71 right += l[s-i] +l[i] -1;
72 counter += l[i] * right -l[i]*(l[i]-1)/2;
73 //cout<<i<<' '<<right<<' '<<counter<<endl;
74 right += 1;
75 }
76 }
77 cout << counter << endl;
78 return 0;
79 }
队友用二分的做法 时间复杂度O(n*logn)
甚至很迷的是这道题暴力居然都过了 适当优化的暴力做法
1 #include <iostream>
2 #include <cstdio>
3 #include <algorithm>
4
5 using namespace std;
6
7 const int N = 3e5;
8 int a[N], b[1000010] = {0};
9 long long s[1000010];
10 int main()
11 {
12 int n, k, m = 0;
13 cin>>n>>k;
14 for(int i = 0; i < n; i ++ ) {
15 scanf("%d", &a[i]);
16 m = max(m, a[i]);
17 b[a[i]]++;
18 }//读入
19 sort(a,a+n);
20 int half = k / 2;
21 int count = 0;
22 for(int i = 0; i < n; i ++ ) {
23 if(a[i] <= half) count ++;
24 else break;
25 }
26 long long ans = count * (count - 1) / 2;
27 s[0] = 0;
28 for(int i = 1; i <= k; i ++ ) s[i] = s[i - 1] + b[i];
29 for(int i = 0; a[i] < half; i += b[a[i]] ){
30 long long cnt = s[k - a[i]] - s[half];
31 ans += cnt * b[a[i]];
32 }
33 if(k%2 != 0) ans += b[half] * b[half + 1];
34 //ans += b[half] * (b[half] - 1) / 2;
35 printf("%lld",ans);
36 return 0;
37 }
newcoder假日团队赛8 H-Costume Party的更多相关文章
- 牛客假日团队赛2 H.奶牛排序
链接: https://ac.nowcoder.com/acm/contest/924/H 题意: 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾 ...
- 牛客假日团队赛1 A.蹄球锦标赛
链接: https://ac.nowcoder.com/acm/contest/918/A 题意: 为了准备即将到来的蹄球锦标赛,Farmer John正在训练他的N头奶牛(方便起见,编号为1-N,其 ...
- 牛客假日团队赛1 J.分组
链接: https://ac.nowcoder.com/acm/contest/918/J 题意: 在Farmer John最喜欢的节日里,他想要给他的朋友们赠送一些礼物.由于他并不擅长包装礼物,他想 ...
- 牛客假日团队赛1 I.接机
链接: https://ac.nowcoder.com/acm/contest/918/I 题意: 一场别开生面的牛吃草大会就要在Farmer John的农场举办了! 世界各地的奶牛将会到达当地的机场 ...
- 牛客假日团队赛1 G.Superbull
链接: https://ac.nowcoder.com/acm/contest/918/G 题意: Bessie and her friends are playing hoofball in the ...
- 牛客假日团队赛1 D.Promotion Counting
链接: https://ac.nowcoder.com/acm/contest/918/D 题意: Bessie the cow is helping Farmer John run the USA ...
- 牛客假日团队赛1B.便便传送门(一)
链接:https://ac.nowcoder.com/acm/contest/918/B 题意: Farmer John最讨厌的农活是运输牛粪.为了精简这个过程,他制造了一个伟大的发明:便便传送门!与 ...
- 牛客假日团队赛2 G.CountyFairEvents
链接: https://ac.nowcoder.com/acm/contest/924/G 题意: Farmer John has returned to the County Fair so he ...
- 牛客假日团队赛2 F.跳跃
链接: https://ac.nowcoder.com/acm/contest/924/F 题意: Farmer John为了满足奶牛对美的享受而安装了人工湖.矩形的人工湖分成M行N列(1 <= ...
- 牛客假日团队赛2 D.亲和数对
链接: https://ac.nowcoder.com/acm/contest/924/D 题意: 求在给定区间[start,end]内所有的亲和数对. 亲和数的定义:对于数对(A,B),如果A的除了 ...
随机推荐
- Array, Set, Map知多少?
Array,Set和Map三个作为Javascript中可迭代的集合数据类型,在编程过程中使用的频率也比较高.针对三种数据类型各自的一些特性,本文的内容将从以下几个方面来上述数据类型做一个总结. 实例 ...
- 用 AI 实现一个 GBK/GB2312 转 UTF-8 工具:轻松解决文本编码转换难题(附完整源码)
用 AI 实现一个 GBK/GB2312 转 UTF-8 工具:轻松解决文本编码转换难题 在处理历史文件或与不同系统交互时,我们经常会遇到 GBK 或 GB2312 编码的文本文件.虽然现在 UTF- ...
- .NET AI 生态关键拼图:全面解读 AI Extensions 和 Vector Extensions 如何重塑.NET开发生态
引言 关注.NET AI和.NET Vector原生开发已有半年之久了,其核心组件在历经这半年预发布期的持续迭代后,终于于5月16日和5月20日逐步发布了..在此之前,基于预发布版本撰写的文章和调试工 ...
- Docker下如何实现镜像多阶级构建?
Docker下如何实现镜像多阶级构建? 在Docker早期版本中,对于编译型语言(例如C.Java.Go)的镜像构建,我们只能将应用的编译和运行环境的准备,全部放在一个Dockerfile里面,这就导 ...
- 《Java Web程序设计——MyEclipse的安装、配置》
Java Web程序设计--MyEclipse的安装.配置 具体安装.配置过程请参考下面的博客 MyEclipse安装.配置.测试 -- 博客园 原博客中所需文件均存放于百度网盘中 如下载速度较慢,可 ...
- PS简单图片拼接
1.打开图片 首先打开需要拼接的图片两张 以这两张图片为例子 首先我们先看看这两张图片的宽高分别为多少 打开图像---->画布大小 好记录下来小姐姐宽大概17cm,高大概20cm. 再来看看提莫 ...
- LEECODE Rust学习: 一维数组的动态和
给你一个数组 nums .数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]-nums[i]) . 请返回 nums 的动态和. 示例 1: 输入:nums = [1, ...
- 报表自动生成程序:ZREPORT_GENERATOR
群里看到的,抄别人的...源自哪里就不清楚了.这里申明一下:非本人所写,如果侵权,你打群主好了. 这里说明一下:这个程序不太好用,没有SQVI友好.(以下问题可能是没用字段别名,不想试验了) 1.生成 ...
- SQL Server数据库巡检
查询所有表名 select name from sysobjects where xtype='u' select * from sys.tables 查询所有表名及对应架构 select t.[na ...
- 二、SDK编译
二.sdk编译 1.linux 1.1.一键编译 ./build.sh lunch # RK_ROOTFS_SYSTEM目前可设定三种系统:buildroot.debian. yocto export ...