URAL 2067 水dp

来源:互联网 发布:漳州网络诈骗举报平台 编辑:程序博客网 时间:2024/06/06 09:25

There is a group of n children. According to a proverb, every man to his own taste. So the children value strawberries and raspberries differently. Let’s say that i-th child rates his attachment to strawberry as s i and his attachment to raspberry as r i.
According to another proverb, opposites attract. Surprisingly, those children become friends whose tastes differ.
Let’s define friendliness between two children v, u as: p( v, u) = sqrt(( s v − s u) 2 + ( r v − r u) 2)
The friendliness between three children v, u, w is the half the sum of pairwise friendlinesses: p( v, u, w) = ( p( v, u) + p( v, w) + p( u, w)) / 2
The best friends are that pair of children v, u for which v ≠ u and p( v, u) ≥ p( v, u, w) for every child w. Your goal is to find all pairs of the best friends.
Input
In the first line there is one integer n — the amount of children (2 ≤ n ≤ 2 · 10 5).
In the next n lines there are two integers in each line — s i and r i (−10 8 ≤ s i, r i ≤ 10 8).
It is guaranteed that for every two children their tastes differ. In other words, if v ≠ u then s v ≠ s u or r v ≠ r u.
Output
Output the number of pairs of best friends in the first line.
Then output those pairs. Each pair should be printed on a separate line. One pair is two numbers — the indices of children in this pair. Children are numbered in the order of input starting from 1. You can output pairs in any order. You can output indices of the pair in any order.
It is guaranteed that the amount of pairs doesn’t exceed 10 5.
Example
input output
2
2 3
7 6
1
1 2
3
5 5
2 -4
-4 2
0

这个题就权当是一个个的线段
然后按照重合部分一个一个的走
就和很久以前的dp22提的土门题一样傻逼

虽然很傻逼还是有一些需要注意的事情和一些收获

收获呢就是很多题看上去是贪心但是贪心不出来的时候

直接去搜索

搜索的时候很自然的就去记忆话了。。

然后就可以dp了

这种看上去是贪心的dp

往往都很傻逼

注意到了这一点就很容易了…

还有一些是坑
比如这个题要开long long 才能过
喵喵喵?

#include<iostream>#include<cmath>#include<algorithm>#include<map>#include<queue>#include<cstring>#include<stack>#include<cstdio>#include<vector>using namespace std;long long tu[100001];struct p{    long long  z,y,zhi,geshu;    bool operator < (const p&a)const{    return zhi<a.zhi;    }};long long  dp[100001][2];p zz[100001];int main(){    #define int long long    int n;    cin>>n;    for(int a=1;a<=n;a++)scanf("%lld",&tu[a]);    map<int,int>mp;    for(int a=1;a<=n;a++)    {        if(!mp[tu[a]])        {            mp[tu[a]]=mp.size();            zz[mp.size()].zhi=tu[a];            zz[mp.size()].z=0x3f3f3f3f;            zz[mp.size()].y=0;        }        zz[mp[tu[a]]].geshu++;        zz[mp[tu[a]]].z=min(zz[mp[tu[a]]].z,a);        zz[mp[tu[a]]].y=max(zz[mp[tu[a]]].y,a);    }    sort(zz+1,zz+mp.size()+1);/*  for(int a=1;a<=mp.size();a++)    {        cout<<zz[a].z<<" "<<zz[a].y<<" "<<endl;    }    *///  cout<<endl;    int cengshu=mp.size();    memset(dp,0x3f,sizeof(dp));/*  dp[1][0]=zz[1].z-1;    dp[1][1]=zz[1].y-1;//  cout<<dp[1][0]<<" "<<dp[1][1]<<endl;    for(int a=2;a<=cengshu;a++)    {        dp[a][0]=min(dp[a-1][0]+abs(zz[a].z-zz[a-1].z),dp[a-1][1]+abs(zz[a].z-zz[a-1].y));//      cout<<abs(zz[a].z-zz[a-1].z)<<" "<<abs(zz[a].z-zz[a-1].y)<<endl;        dp[a][1]=min(dp[a-1][0]+abs(zz[a].y-zz[a-1].z),dp[a-1][1]+abs(zz[a].y-zz[a-1].y));//      cout<<abs(zz[a].y-zz[a-1].z)<<" "<<abs(zz[a].y-zz[a-1].y)<<endl;    }*///  dp[1][0]=dp[1][1]=zz[1].y-1;    dp[1][1]=zz[1].y-1;    dp[1][0]=dp[1][1]+zz[1].y-zz[1].z;    for(int a=2;a<=cengshu;a++)    {//      int tem0z=dp[a-1][0]+zz[a].y-zz[a].z+min(abs(zz[a].z-zz[a-1].z),abs(zz[a].y-zz[a-1].z));//      int tem0y=dp[a-1][1]+zz[a].y-zz[a].z+min(abs(zz[a].z-zz[a-1].y),abs(zz[a].y-zz[a-1].y));        int tem0z=dp[a-1][0]+zz[a].y-zz[a].z+abs(zz[a].y-zz[a-1].z);        int tem0y=dp[a-1][1]+zz[a].y-zz[a].z+abs(zz[a].y-zz[a-1].y);        int tem1z=dp[a-1][0]+zz[a].y-zz[a].z+abs(zz[a].z-zz[a-1].z);        int tem1y=dp[a-1][1]+zz[a].y-zz[a].z+abs(zz[a].z-zz[a-1].y);        dp[a][0]=min(tem0z,tem0y);        dp[a][1]=min(tem1z,tem1y);//      cout<<dp[a][0]<<" "<<dp[a][1]<<endl;    }    cout<<min(dp[cengshu][0],dp[cengshu][1])+n<<endl;    return 0;}
0 0