题目:点击打开链接
分析:找与点x距离最远和最近的距离是多少?
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<string.h>
using namespace std;
int a[100005];
int n,minn,maxn;
int main()
{
while(cin>>n){
for(int i=1;i<=n;i++)
cin>>a[i];
cout<<a[2]-a[1]<<' '<<a[n]-a[1]<<endl;
for(int i=2;i<n;i++){
maxn=max((a[n]-a[i]),(a[i]-a[1]));
minn=min((a[i+1]-a[i]),(a[i]-a[i-1]));
printf("%d %d\n",minn,maxn);
}
cout<<a[n]-a[n-1]<<' '<<a[n]-a[1]<<endl;
}
return 0;
}
567B - Berland National Library
分析:求图书馆的最大容纳量,用一个集合表示现在在图书馆观众的人数,如果是+,则加入集合,如果是 -,如果在集合中没有出现过,说明以前就存在,那么maxn++,如果已经存在了,那么从集合中删除来维持现在图书馆中的数量。
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<string.h>
#include<set>
using namespace std;
int a[102],n;
bool in[102];
int main()
{
char c;
int x;
while(cin>>n){
set<int>lib;
int maxn=0;
for(int i=0;i<n;i++){
cin>>c>>x;
if(c=='+'){
lib.insert(x);
int d=lib.size();
maxn=max(maxn,d);
}
else{
if(lib.find(x)==lib.end())maxn++;
else lib.erase(x);
int d=lib.size();
maxn=max(maxn,d);
}
}
cout<<maxn<<endl;
}
return 0;
}
分析:这题需要注意数据的范围( - 109 ≤ ai ≤ 109),虽然a[i]不会超过int,但是a[i]*k会爆int,所以直接用LL a[2e5+5]就可以,因为这个搞了3个小时,谨记。。。
Let's solve this problem for fixed middle element of progression. This means that if we fix element ai then the progression must consist of ai / k and ai·k elements. It could not be possible, for example, if ai is not divisible by k ().
For fixed middle element one could find the number of sequences by counting how many ai / k elements are placed left from fixed element and how many ai·k are placed right from it, and then multiplying this numbers. To do this, one could use two associative arrays Al and Ar, where for each key x will be stored count of occurences of x placed left (or right respectively) from current element. This could be done with map structure.
Sum of values calculated as described above will give the answer to the problem.
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
#define LL long long
LL a[200005],n,k,maxk,num;
map<LL,int>lmp,rmp;
void solve()
{
lmp[a[1]]++;
for(int i=n;i>=2;i--)
rmp[a[i]]++;
long long ans=0;
for(int i=2;i<n;i++)
{
rmp[a[i]]--;
if(a[i]%k==0){
ans+=(long long)lmp[a[i]/k]*rmp[a[i]*k];
// cout<<lmp[a[i]/k]<<' '<<rmp[a[i]*k]<<endl;
}
lmp[a[i]]++;
}
cout<<ans<<endl;
}
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
scanf("%I64d",&a[i]);
solve();
return 0;
}
/*
3 110000
1 110000 -784901888
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
#define LL long long
int a[200005],n,k,maxk,num;
map<LL,int>lmp,rmp;
void solve()
{
lmp[a[1]]++;
for(int i=n;i>=2;i--){
rmp[a[i]]++;
cout<<rmp[a[i]]<<' '<<a[i]<<endl;
}
long long ans=0;
for(int i=2;i<n;i++)
{
rmp[a[i]]--;
if(a[i]%k==0){
// cout<<(LL)a[i]*k<<endl;
ans+=(long long)lmp[a[i]/k]*rmp[(LL)a[i]*k]; //如果是int,就在这里处理一下
// cout<<lmp[a[i]/k]<<' '<<rmp[a[i]*k]<<endl;
}
lmp[a[i]]++;
}
cout<<ans<<endl;
}
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
solve();
return 0;
}