链接:https://ac.nowcoder.com/acm/contest/5203/B
来源:牛客网

题目描述

Listening to the music is relax, but for obsessive(强迫症), it may be unbearable. HH is an obsessive, he only start to listen to music at 12:00:00, and he will never stop unless the song he is listening ends at integral points (both minute and second are 0 ), that is, he can stop listen at 13:00:00 or 14:00:00,but he can’t stop at 13:01:03 or 13:01:00, since 13:01:03 and 13:01:00 are not an integer hour time. Now give you the length of some songs, tell HH whether it’s possible to choose some songs so he can stop listen at an integral point, or tell him it’s impossible. Every song can be chosen at most once.
输入描述:
The first line contains an positive integer T(1≤T≤60), represents there are T test cases. For each test case: The first line contains an integer n(1≤n≤105), indicating there are n songs. The second line contains n integers a1,a2…an (1≤ai≤109 ), the ith integer ai indicates the ith song lasts ai seconds.
输出描述:
For each test case, output one line “YES” (without quotes) if HH is possible to stop listen at an integral point, and “NO” (without quotes) otherwise.

示例1

输入
复制

3
3
2000 1000 3000
3
2000 3000 1600
2
5400 1800

输出
复制

NO
YES
YES

说明

In the first example it’s impossible to stop at an integral point.In the second example if we choose the first and the third songs, they cost 3600 seconds in total, so HH can stop at 13:00:00In the third example if we choose the first and the second songs, they cost 7200 seconds in total, so HH can stop at 14:00:00

题目大意就是给你n个数,问是否可以从中选取若干个数使得他们的和为3600.
抽屉原理的应用:一个由n个数组成的数列 一定能找出若干个连续的数使它们之和能被n整除。
所以 n >= 3600 时输出 YES
证明:n个数记为a[1],a[2],…a[n].设置一个数组sum,其存储信息为sum[i] = a[1] + a[2] + …a[i];
情况一:存在一个k(1 <= k <= n),使得sum[k] % n == 0,那么就得证;
情况二:对于任意的k(1 <= k <= n),都有sum[k] % n != 0,那么余数的种类只有 1 到 n-1 总共 n-1 种情况,但是有 n 个 sum,由抽屉原理
可知必然有两个sum(假设为sum[i],sum[j],j > i)的余数相同。因此 (sum[j] - sum[i]) % n = (sum[j] % n - sum[i] % n) = 0;
所以 n >= 3600 时输出 YES
然后数据范围就压缩到了3600,用二进制拆分之后直接按照01背包来做就好。
具体思路解析
牛客算法周周练2 B Music Problem 题解

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#define ls (p<<1)
#define rs (p<<1|1)
//#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
//#define int __int128
using namespace std;
typedef long long ll;//全用ll可能会MLE或者直接WA,试着改成int看会不会A
const ll N=1e5+7;
const ll INF=1e10+9;
const ll mod=3600;
const double EPS=1e-10;//-10次方约等于趋近为0
const double Pi=3.1415926535897;ll n,m,dp[2][mod],a[N];
ll tot[mod],t;
int main()
{scanf("%lld",&t);while(t--){memset(tot,0,sizeof tot);ll n;scanf("%lld",&n);over(i,1,n){scanf("%lld",&a[i]);a[i]%=mod;tot[a[i]]++;}if(tot[0]){puts("YES");continue;}n=0;over(i,1,3600-1){ll now=1;while(tot[i]>=now){a[++n]=(now*i)%mod;tot[i]-=now;now<<=1;}if(tot[i]){a[++n]=(tot[i]*i)%mod;}}memset(dp,0,sizeof dp);dp[0][0]=1;over(i,1,n){if(dp[0][0]>1)break;over(j,0,3600-1)dp[1][j]+=dp[0][((j-a[i])%mod+mod)%mod];over(j,0,3600-1)dp[0][j]+=dp[1][j],dp[1][j]=0;}if(dp[0][0]>1)puts("YES");else puts("NO");}return 0;
}

牛客算法周周练2 B Music Problem(DP,抽屉原理,二进制拆分)相关推荐

  1. 牛客网-小周的曲射炮

    牛客网-小周的曲射炮(公式推导) 题目描述 小周最近在玩一款二战游戏,他因而对曲射炮的轨迹产生了很大的兴趣,但是在尝试计算后,小周发现这个问题并不是那么简单,他因而来请教你这位计算机高手,请你来帮帮他 ...

  2. 牛客算法周周练11 A.切题之路 签到题

    链接:https://ac.nowcoder.com/acm/contest/6046/A 来源:牛客网 题目描述 众所周知,SD省有一个可爱的妹子,叫做rqy(rqy天下第一可爱!不接受反驳,抱走r ...

  3. 牛客算法周周练11A - 切题之路(阅读理解)

    链接:https://ac.nowcoder.com/acm/contest/6046/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...

  4. 牛客算法周周练8 「金」点石成金 暴力

    链接:https://ac.nowcoder.com/acm/contest/5803/B 来源:牛客网 题目描述 赛时提示:魔法值和财富值初始为0 帕秋莉掌握了一种金属性魔法 她决定去捡一些石头,施 ...

  5. [牛客算法总结]:青蛙跳台阶

    标签: 递归.记忆化搜索.动态规划 题目: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个 n 级的台阶总共有多少种跳法(先后次序不同算不同的结果). 数据范围:1 \leq n \l ...

  6. 牛客算法笔记 彩色宝石项链

    链接:https://www.nowcoder.com/questionTerminal/321bf2986bde4d799735dc9b493e0065 来源:牛客网有一条彩色宝石项链,是由很多种不 ...

  7. 2020年牛客算法入门课练习赛1

    第k小数 链接:https://ac.nowcoder.com/acm/contest/12144/A 来源:牛客网 题目描述 给你一个长度为n的序列,求序列中第k小数的多少. 输入描述: 多组输入, ...

  8. 牛客假日团队赛10 L 乘积最大 (dp,大数)

    链接:https://ac.nowcoder.com/acm/contest/1072/L?&headNav=acm&headNav=acm 来源:牛客网 乘积最大 时间限制:C/C+ ...

  9. 牛客假日团队赛5 K 金币馅饼 (DP 基础题)

    链接:https://ac.nowcoder.com/acm/contest/984/K 来源:牛客网 金币馅饼 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

最新文章

  1. OpenCV 笔记(09)— 常用的数据结构和函数(Vec、Point、Scalar、Size、Rect、cvtColor)
  2. java se 7u67_Linux下安装jdk-7u67-linux-x64.rpm
  3. Spring Data JPA_多表关联查询中应该注意的问题
  4. JZOJ 3786. 【NOI2015模拟8.19】图
  5. OpenCV图像监视:在Visual Studio调试器中查看内存中图像
  6. 我的cookie读写
  7. MySQL 5.7 新特性详解
  8. 一个能极大提高生产率的Chrome新建标签页扩展
  9. 关于目录操作walk
  10. 3分钟入门python_3分钟带你了解世界第一语言Python 入门上手也这么简单!
  11. pthreads v3下一些坑和需要注意的地方
  12. 第七章实验报告(数组实验)
  13. 2021-09-08173. 二叉搜索树迭代器 栈
  14. vba html 教程 pdf,Word VBA教程:CanvasShapes集合
  15. 带外壳版本4G LTE模块,包括华为ME909系列、移远EC20系列、移远EC200T系列
  16. 微信推送封面尺寸_【新媒体干货】微信公众号封面图设计规范试行版
  17. 猴子吃桃问题的函数递归解决方案
  18. 32位和64位版本的Office异同点
  19. C++程序的存储空间布局
  20. Paper Reading Notes

热门文章

  1. 编写同时在PyTorch和Tensorflow上工作的代码
  2. 干货 | 使用FFT变换自动去除图像中严重的网纹
  3. 看看Spring的源码(一)——Bean加载过程
  4. Android开发常用框架汇总
  5. 让textarea完全显示文章并且不滚动、不可拖拽、不可编辑
  6. Oracle CDC配置案例
  7. 移动分发端 基础统计指标经典业务代码节选--留存用户统计
  8. 在Ubuntu14.04安装F.lux
  9. Postfix(一):CentOS 下安装postfix
  10. ubuntu php session 删除,session_unset()和session_destroy()用法分析