Dating with girls(1)(二分+map+set)
2015-10-28 18:10
381 查看
Dating with girls(1)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3954 Accepted Submission(s): 1228[align=left]Problem Description[/align]
Everyone in the HDU knows that the number of boys is larger than the number of girls. But now, every boy wants to date with pretty girls. The girls like to date with the boys with higher IQ. In order to test the boys ' IQ, The girls make a problem, and the boys who can solve the problem correctly and cost less time can date with them. The problem is that : give you n positive integers and an integer k. You need to calculate how many different solutions the equation x + y = k has . x and y must be among the given n integers. Two solutions are different if x0 != x1 or y0 != y1. Now smart Acmers, solving the problem as soon as possible. So you can dating with pretty girls. How wonderful!
[align=left]Input[/align]
The first line contain an integer T. Then T cases followed. Each case begins with two integers n(2 <= n <= 100000) , k(0 <= k < 2^31). And then the next line contain n integers.
[align=left]Output[/align]
For each cases,output the numbers of solutions to the equation.
[align=left]Sample Input[/align]
2
5 4
1 2 3 4 5
8 8
1 4 5 7 8 9 2 6
[align=left]Sample Output[/align]
3
5
题解:取两个数使得x+y=k;因为就两个数,所以用二分,set,map均可,若取多个的和是k就要考虑动态规划了,前面做过,上代码:
二分:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> using namespace std; #define mem(x,y) memset(x,y,sizeof(x)) const int INF=0x3f3f3f3f; const double PI=acos(-1.0); const int MAXN=100010; int m[MAXN],k; bool erfen(int l,int r,int x){ int mid; while(l<=r){ mid=(l+r)>>1; if(x+m[mid]==k){ // printf("%d %d\n",x,m[mid]); return true; } if(x+m[mid]>=k)r=mid-1; else l=mid+1; } return false; } int main(){ int T,n; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++)scanf("%d",m+i); m[0]=-INF; sort(m,m+n+1); int cnt=0; for(int i=1;i<=n;i++){ if(m[i]>k||m[i]==m[i-1])continue; if(erfen(1,n,m[i]))cnt++; } printf("%d\n",cnt); } return 0; }
map:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<map> const int INF=0x3f3f3f3f; using namespace std; const int MAXN=100010; map<int,int>mp; int m[MAXN]; int main(){ int T,n,k; scanf("%d",&T); while(T--){ mp.clear(); scanf("%d%d",&n,&k); m[0]=-INF; for(int i=1;i<=n;i++){ scanf("%d",m+i); if(!mp[m[i]]) mp[m[i]]=1; else i--,n--; // cout<<m[i]<<mp[m[i]]<<endl; } int cnt=0; for(int i=1;i<=n;i++){ if(m[i]>k)continue; if(mp[k-m[i]])cnt++; } printf("%d\n",cnt); } return 0; }
set:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<set> const int INF=0x3f3f3f3f; using namespace std; const int MAXN=100010; set<int>st; int m[MAXN]; int main(){ int T,n,k,temp; scanf("%d",&T); while(T--){ st.clear(); scanf("%d%d",&n,&k); for(int i=0;i<n;i++){ scanf("%d",&temp); st.insert(temp); } set<int>::iterator iter; int cnt=0; for(iter=st.begin();iter!=st.end();iter++) if(st.count(k-*iter))cnt++; printf("%d\n",cnt); } return 0; }
相关文章推荐
- iOS delegate 的使用
- OnKeyListener键盘事件验证银行帐号
- 二维数组的输出问题
- Spring事务异常rollback-only
- EAS BOS批量保存或更新数据
- cocos2dx 3.x中ActionTimeLine无法使用setLastFrameCallFunc的解决方案
- POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊
- Jquery easyui总结
- The method getDispatcherType() is undefined for the type HttpServletRequest
- Spring各jar包的作用
- POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊
- EAS BOS根据编码规则生成编码
- hdoj Color the ball 1556 (线段树&&树状数组 区间值更新)
- bootstrap display属性
- SURF算法详解及同SIFT算法的比较
- 服务器导出服务器时间转换浏览器端时区
- notification 初识
- 用jquery的方式使用handlebars模板
- PID 整定方法与原理-说明了“I”的用法
- DP-HDU-2859-Phalanx