CF-500div2-A/B/C
There is a beautiful garden of stones in Innopolis.
Its most beautiful place is the nn piles with stones numbered from 11 to nn.
EJOI participants have visited this place twice.
When they first visited it, the number of stones in piles was x1,x2,…,xnx1,x2,…,xn, correspondingly. One of the participants wrote down this sequence in a notebook.
They visited it again the following day, and the number of stones in piles was equal to y1,y2,…,yny1,y2,…,yn. One of the participants also wrote it down in a notebook.
It is well known that every member of the EJOI jury during the night either sits in the room 108108 or comes to the place with stones. Each jury member who comes there either takes one stone for himself or moves one stone from one pile to another. We can assume that there is an unlimited number of jury members. No one except the jury goes to the place with stones at night.
Participants want to know whether their notes can be correct or they are sure to have made a mistake.
The first line of the input file contains a single integer nn, the number of piles with stones in the garden (1≤n≤501≤n≤50).
The second line contains nn integers separated by spaces x1,x2,…,xnx1,x2,…,xn, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the first time (0≤xi≤10000≤xi≤1000).
The third line contains nn integers separated by spaces y1,y2,…,yny1,y2,…,yn, the number of stones in piles recorded in the notebook when the participants came to the place with stones for the second time (0≤yi≤10000≤yi≤1000).
If the records can be consistent output "Yes", otherwise output "No" (quotes for clarity).
5
1 2 3 4 5
2 1 4 3 5
Yes
5
1 1 1 1 1
1 0 1 0 1
Yes
3
2 3 9
1 7 9
No
In the first example, the following could have happened during the night: one of the jury members moved one stone from the second pile to the first pile, and the other jury member moved one stone from the fourth pile to the third pile.
In the second example, the jury took stones from the second and fourth piles.
It can be proved that it is impossible for the jury members to move and took stones to convert the first array into the second array.
只要判断一下石头是否增加了就OK。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define inf 0x3f3f3f3f 5 #define pb push_back 6 #define pii pair<int,int> 7 8 int main(){ 9 int n1,n2,n,i,j; 10 while(scanf("%d",&n)!=EOF){ 11 int s1=0,s2=0,a; 12 for(i=1;i<=n;++i){ 13 scanf("%d",&a); 14 s1+=a; 15 } 16 for(i=1;i<=n;++i){ 17 scanf("%d",&a); 18 s2+=a; 19 } 20 s2<=s1?puts("Yes"):puts("No"); 21 } 22 return 0; 23 }
There is an array with n elements a1, a2, ..., an and the number x.
In one operation you can select some i (1 ≤ i ≤ n) and replace element ai with ai & x, where & denotes the bitwise and operation.
You want the array to have at least two equal elements after applying some operations (possibly, none). In other words, there should be at least two distinct indices i ≠ j such that ai = aj. Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply.
The first line contains integers n and x (2 ≤ n ≤ 100 000, 1 ≤ x ≤ 100 000), number of elements in the array and the number to and with.
The second line contains n integers ai (1 ≤ ai ≤ 100 000), the elements of the array.
Print a single integer denoting the minimal number of operations to do, or -1, if it is impossible.
4 3
1 2 3 7
1
2 228
1 1
0
3 7
1 2 3
-1
In the first example one can apply the operation to the last element of the array. That replaces 7 with 3, so we achieve the goal in one move.
In the second example the array already has two equal elements.
In the third example applying the operation won't change the array at all, so it is impossible to make some pair of elements equal.
问至少经过几次ai=ai&x的操作能使得数组a中有一个元素至少出现了两次。
如果可以的话只有可能是0/1/2分别判断一下就好了。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define inf 0x3f3f3f3f 5 #define pb push_back 6 #define pii pair<int,int> 7 bool vis1[100010],vis2[100010]; 8 int a[100010]; 9 int main(){ 10 int n,x,i; 11 while(cin>>n>>x){ 12 memset(vis1,0,sizeof(vis1)); 13 memset(vis2,0,sizeof(vis2)); 14 bool odd=0; 15 int ans=0; 16 for(i=1;i<=n;++i){ 17 scanf("%d",a+i); 18 if(vis1[a[i]]){ 19 odd=1; 20 } 21 if(vis1[a[i]&x]||vis2[a[i]]){ 22 ans=1; 23 } 24 vis1[a[i]]=1; 25 vis2[a[i]&x]=1; 26 } 27 if(odd){cout<<0<<endl;continue;} 28 if(ans){cout<<ans<<endl;continue;} 29 memset(vis1,0,sizeof(vis1)); 30 for(i=1;i<=n;++i){ 31 if(vis1[a[i]&x]){ 32 ans=2; 33 break; 34 } 35 vis1[a[i]&x]=1; 36 } 37 ans?cout<<ans<<endl:cout<<-1<<endl; 38 } 39 return 0; 40 }
Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes.
Strictly speaking, it makes a photo of all points with coordinates (x,y)(x,y), such that x1≤x≤x2x1≤x≤x2 and y1≤y≤y2y1≤y≤y2, where (x1,y1)(x1,y1) and (x2,y2)(x2,y2) are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero.
After taking the photo, Pavel wrote down coordinates of nn of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky.
Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points.
Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle.
The first line of the input contains an only integer nn (1≤n≤1000001≤n≤100000), the number of points in Pavel's records.
The second line contains 2⋅n2⋅n integers a1a1, a2a2, ..., a2⋅na2⋅n (1≤ai≤1091≤ai≤109), coordinates, written by Pavel in some order.
Print the only integer, the minimal area of the rectangle which could have contained all points from Pavel's records.
4
4 1 3 2 3 2 1 3
1
3
5 8 5 5 7 5
0
In the first sample stars in Pavel's records can be (1,3)(1,3), (1,3)(1,3), (2,3)(2,3), (2,4)(2,4). In this case, the minimal area of the rectangle, which contains all these points is 11
(rectangle with corners at (1,3)(1,3) and (2,4)(2,4)).
给出2n个数自由两两组合为n个坐标,找到一个矩形能包含这n个点,求这个矩形的最小面积。
先对数组排序,然后考虑枚举一下最小和最大的横坐标,观察后发现当a0视作最左的时候,
把an-1视作最右是最优解,且此时纵坐标的差最小就是a2n-1-an,然后依次递推把x2,x3...视作最左x时候的情况,
会发现此时最优解总是(a2n-1-a0)*(ai+n-1-ai)。扫一遍就好了。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define inf 0x3f3f3f3f 5 #define pb push_back 6 #define pii pair<int,int> 7 LL a[100010*2]; 8 int main(){ 9 int n,i,j,k; 10 11 while(scanf("%d",&n)!=EOF){ 12 int nn=n*2; 13 for(i=0;i<nn;++i) scanf("%lld",a+i); 14 sort(a,a+nn); 15 LL ans=(a[n-1]-a[0])*(a[nn-1]-a[n]); 16 for(i=1;i<n;++i) 17 ans=min(ans,(a[nn-1]-a[0])*(a[i+n-1]-a[i])); 18 cout<<ans<<endl; 19 } 20 21 return 0; 22 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 赶AI大潮:在VSCode中使用DeepSeek及近百种模型的极简方法
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
2017-07-30 HDU 2795 线段树单点更新
2017-07-30 bzoj 1087 状压dp