problem Ksusha and array
describe
输入 N 个数字,你的任务是在这些数字中找到一个能够整除所有这些数字的数。
solution
换句话说,就是数组中的最小值是否能够整除所有数字。
#include <iostream>
using namespace std;
const int INF = 1e9;
const int MAXN = 1e5;
int a[MAXN + 5];
int main()
{
int n;
cin >> n;
int minnum = INF;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
minnum = min(minnum,a[i]);
}
for(int i = 1;i <= n;i++)
{
if(a[i] % minnum != 0)
{
cout << -1;
return 0;
}
}
cout << minnum;
return 0;
}
problem two bags of potatoes
describe
一个小朋友有两袋土豆( x,y≥1)且两袋土豆总和 ≤n 且总和能被 k 整除。给出 y,k,n,求所有可能的 x。如果没有这样的 x 值,输出 -1;否则,一行不重复地输出所有可能的 x 值,两两之间用一个空格隔开,并按照升序排列。
solution
数学公式的推导,暴力超时
#include<iostream>
using namespace std;
int y,n,k,ans;
bool flag = false;//看是否输出-1
int main()
{
cin >> y >> k >> n;
ans = k * (y / k + 1);//ans就是x最小时x+y的值了
while(ans <= n)
{
cout << ans - y << " ";
flag = true;
ans += k;
}
if(!flag) cout << "-1";//判断是否输出
return 0;
}
problem big segment
describe
找到一条可以覆盖所有线段的线段,输出其编号;若没有,输出−1。
#include<bits/stdc++.h>
using namespace std;
int n,maxn=INT_MIN,minn=INT_MAX;
struct node{
int l,r;
}a[100005];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].l,&a[i].r);
maxn=max(maxn,a[i].r),minn=min(minn,a[i].l
}
for(int i=1;i<=n;i++){
if(a[i].l==minn&&a[i].r==maxn){
printf("%d",i);
return 0;
}
}
printf("-1");
return 0;
}
problem second_price auction
describe
给你n个数,求出其中最大值的编号和第二大的值。 (n个数都不相同)
#include<bits/stdc++.h>
using namespace std;
struct node{
int t,s;
}a[10001];
int n;
int cmp(node x,node y){
return x.s>y.s;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].s;
a[i].t=i;
}
sort(a+1,a+1+n,cmp);
cout<<a[1].t<<" "<<a[2].s;
return 0;
}
problem valera and plates
describe
Valera有m个干净的碗和k个干净的盘子,在接下来的n天中,他每天要吃一道菜。菜分两种,第一种必须用一个干净的碗,第二种可以用一个干净的碗或一个干净的盘子,用完后碗或盘子就会变脏,再次使用就要清洗,现在给定每天吃哪种菜,请你安排每天使用碗还是盘子,使得清洗的次数最少
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef long long ll;
int a[N],b[N];
#define x first
#define y second
int main() {
int n,m,k;
cin>>n>>m>>k; //day b plate
//first b second b or p
int re=0,ree=0;
for(int i=1;i<=n;i++)
{
int x;
cin >> x;
if(x==1) re++;
if(x==2) ree++;
}
int p=0,num=0;
if(re>m) num=re-m; //clean b
else p=m-re; //remain
//小于不用洗 give the two type dish
if(ree>k)
{
ree-=k;
if(ree>p) num+=ree-p;
}
cout<<num<<endl;
return 0;
}