Editorial Codeforces Round #Pi

来源:互联网 发布:聚宝盆返奖统计软件 编辑:程序博客网 时间:2024/06/07 14:28

题目:点击打开链接

567A - Lineland Mail

 分析:找与点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;}

567C - Geometric Progression


分析:这题需要注意数据的范围( - 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 elementai then the progression must consist ofai / k andai·k elements. It could not be possible, for example, ifai is not divisible byk ().

For fixed middle element one could find the number of sequences by counting how manyai / k elements are placed left from fixed element and how manyai·k are placed right from it, and then multiplying this numbers. To do this, one could use two associative arraysAl andAr, where for each keyx 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 longLL 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 1100001 110000 -784901888#include<iostream>#include<stdio.h>#include<string>#include<algorithm>#include<string.h>#include<map>using namespace std;#define LL long longint 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;}







0 0
原创粉丝点击