newcoder假日团队赛8 H-Costume Party

链接:https://ac.nowcoder.com/acm/contest/1069/H
时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

It's Halloween! Farmer John is taking the cows to a costume party, but unfortunately he only has one costume. The costume fits precisely two cows with a length of S (1 <= S <= 1,000,000). FJ has N cows (2 <= N <= 20,000) conveniently numbered 1..N; cow i has length Li (1 <= Li <= 1,000,000). Two cows can fit into the costume if the sum of their lengths is no greater than the length of the costume. FJ wants to know how many pairs of two distinct cows will fit into the costume.

输入描述:

* 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.
示例1

输入

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 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
As an AI language model, I don't have personal experiences. However, I can provide you with an article about the party's twenty great experiences. Parties are a great way to let loose and have fun with friends and family. There are so many things you can do at a party that can make it a memorable experience. Here are twenty great experiences that can make any party an unforgettable one. 1. Have a theme: Having a theme for the party can make it more exciting and fun. It can be anything from a 70s disco theme to a Hawaiian luau theme. The possibilities are endless. 2. Decorate the space: Decorating the space can make it more festive and inviting. Balloons, streamers, and banners are just some of the items that can be used to decorate the space. 3. Play games: Playing games can be a great way to break the ice and get everyone involved. Some popular games include charades, Pictionary, and beer pong. 4. Have a photo booth: Setting up a photo booth with fun props can be a great way to capture memories and create some hilarious photos. 5. Have a karaoke session: Karaoke can be a great way to get everyone singing and dancing. It’s a great way to break the ice and get everyone involved. 6. Have a dance-off: A dance-off can be a great way to get everyone moving and grooving. It’s a great way to showcase your dance moves and have some fun. 7. Have a potluck: A potluck can be a great way to get everyone involved in the party planning. Everyone can bring their favorite dish to share with everyone else. 8. Have a piñata: A piñata can be a great way to add some excitement to the party. It’s a great way to get everyone involved and have some fun. 9. Have a movie night: A movie night can be a great way to wind down after a long day. It’s a great way to relax and enjoy some good movies with friends and family. 10. Have a bonfire: A bonfire can be a great way to enjoy the outdoors and roast marshmallows. It’s a great way to relax and enjoy some good company. 11. Have a scavenger hunt: A scavenger hunt can be a great way to get everyone involved and have some fun. It’s a great way to explore the area and find hidden treasures. 12. Have a talent show: A talent show can be a great way to showcase everyone’s talents. It’s a great way to have some fun and see what everyone is capable of. 13. Have a DIY station: A DIY station can be a great way to get creative and make something fun. It’s a great way to get everyone involved and have some fun. 14. Have a game night: A game night can be a great way to get everyone involved and have some fun. It’s a great way to play some classic board games or try out some new ones. 15. Have a costume party: A costume party can be a great way to get everyone involved and have some fun. It’s a great way to dress up and be someone else for a night. 16. Have a cocktail-making contest: A cocktail-making contest can be a great way to get everyone involved and have some fun. It’s a great way to showcase your mixology skills and have some fun. 17. Have a dessert-making contest: A dessert-making contest can be a great way to get everyone involved and have some fun. It’s a great way to showcase your baking skills and have some fun. 18. Have a paint night: A paint night can be a great way to get creative and have some fun. It’s a great way to paint something fun and enjoy some good company. 19. Have a board game tournament: A board game tournament can be a great way to get everyone involved and have some fun. It’s a great way to play some classic board games or try out some new ones. 20. Have a DIY cocktail station: A DIY cocktail station can be a great way to get creative and make something fun. It’s a great way to get everyone involved and have some fun. In conclusion, there are so many great experiences that can make any party an unforgettable one. Whether it’s playing games, having a karaoke session, or having a costume party, there are so many ways to have fun and create great memories with friends and family.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值