#个人赛第七场解题总结# (FZU 1888 三角形问题II && FZU 1886 音乐 )

来源:互联网 发布:资本控制舆论 知乎 编辑:程序博客网 时间:2024/06/13 05:17
A  - 三角形问题II  (计算几何)
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
SubmitStatusPracticeFZU 1888

Description

给定平面上的N 个点的坐标,现在你的任务是统计任意3个点构成的三角形的面积和的值。

Input

有多组数据

数据的第一行包含一个正整数T表示数据组数(1 <= T <= 100) 接下来T组数据。

对于每组数据,

第一行包含一个正整数N,代表平面上点的个数 。(1<= N <= 50,)

接下来N行,每行包含2个实数Xi, Yi,代表第i个点的坐标 (0.00 <= Xi,Yi <= 100.00小数点后至多2位)

数据不保证不会出现坐标相同的点。

Output

对于每组数据,首先输出”Case d: “,d为数据编号(从1开始)。只输出一个实数,表示面积和的值。(输出小数点后1位)

Sample Input

130 01 11 0

Sample Output

Case 1: 0.5

Hint

请使用double代替float以避免精度问题。
解题思路】叉积计算,注意精度
代码:
#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include <math.h>using namespace std;const double pi=acos(-1.0);const double eps=1e-6;const int maxn=105;struct node{    double x,y;} point [maxn];double Cross(node a1,node a2,node a3){    double aa= (a2.y - a1.y) * (a3.x - a1.x) - (a2.x - a1.x) * (a3.y - a1.y);    return fabs(aa/2.0);}int main(){    int T,n,m,tot=1;    cin>>T;    while(T--)    {        cin>>n;        for(int i=0; i<n; i++)        {            cin>>point[i].x>>point[i].y;        }        double sum=0;        for(int i=0; i<n; i++)            for(int j=i+1; j<n; j++)                for(int k=j+1; k<n; k++)                    sum+=Cross(point[i],point[j],point[k]);        printf("Case %d: %.1lf\n",tot++,sum);    }    return 0;}

B - 音乐   (FZU 1886 音乐 2011百度之星初赛A题  改编)
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
SubmitStatusPracticeFZU 1886
Description
chjing电脑里有很多歌,每首歌由某个歌手唱的,梁静茹,林俊杰,周杰伦啊,比较多。每次听歌,chjing总是按照播放列表上的顺序,顺序的听下来。所以经常会连续的听到一个歌手唱的歌。老听到同一个人的歌不大好,于是,chjing决定计算一个列表上所有歌的fun值,并选取fun值总和最大的播放列表。
一首歌的fun值,是这样计算的,假设这首歌在位置i,由歌手t唱的,那么在大于i并且离i最近的位置j找到一首歌,也是t唱的,这首歌的fun值就是(j-i)。如果找不到,这首歌的fun值为0.一个播放列表的fun值,就是加上所有位置上的歌曲的fun值。
Input
第一行一个整数T,表示有T个测试用例。
以后每个用例一个整数n,表示有n首歌。(0 < n <= 22)
然后n个整数,第i个整数的值为s,代表第i首歌是s唱的。为方便起见,s是一个数字,代表某个歌手。0 <= s <= 15.
Output
打印“Case h:”,h是第h个用例。
然后下一行,输出所求的fun值。
每个样例后输出一行空行。
Sample Input
231 1 261 2 2 2 3 3
Sample Output
Case 1:2Case 2:8
Hint
第一个样例:排列顺序1 2 1Fun值:2+0+0=2;第二个样例:排列顺序:3 2 1 2 3 2Fun值:4+2+0+2+0+0=8
【解题思路】:
思路一: n首歌,t个歌手,如果每一个歌手只有一首哥,就没有fun值,否则统计歌曲数目大于一的歌,或者说至少有两首哥,那么答案就是:假设n首歌,有t个歌手至少两首歌,其余
n-t首歌都是只有一首,统计t对最前和最后的差即可
换一种思路:
思路二:贪心思想 比如 1 1 2 2 3   一定是把 两个一个左一个右的放 12231 再用相同的办法继续去变化 12321 这样变化出来就一定是最优的 
第一次变化插入了3个数字移动4个单位 增加了4个好感度
第二次变化插入了1个数字移动2个单位 增加了2个好感度
其实都差不多,~~。
思路一代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <stack>#include <algorithm>using namespace std;const int maxn=100;const double pi=acos(-1.0);#define  LL  __int64int a[maxn],b[maxn];bool flag[100001];int main(){    int t,n,tt,i,j,tot=1;    cin>>t;    while(t--)    {        memset(b,0,sizeof(b));        cin>>n;        for(i=0; i<n; i++){            cin>>j;            b[j]++;        }        tt=0;        for(i=0; i<maxn; i++){            if(b[i]!=0)            {                a[tt++]=b[i];            }        }        int ss=0;        for(i=0; i<tt; i++){            if(a[i]>1)            {                ss+=n-1;                n=n-2;            }        }        printf("Case %d:\n",tot++);        printf("%d\n\n",ss);    }    return 0;}
思路二:
#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#include <math.h>using namespace std;const double pi=acos(-1.0);const double eps=1e-6;const int maxn=105;int a[maxn];bool flag[maxn];int main(){    int t,n,m,res,tot=1;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        memset(flag,false,sizeof(flag));        for(int i=0; i<n; i++)  scanf("%d",&a[i]);        sort(a,a+n);        m=n+1,res=0;        for(int i=0; i<n-1; i++)        {            if((flag[a[i]]==false)&&a[i]==a[i+1])            {                m-=2;                res+=m;                flag[a[i]]=true;            }        }        printf("Case %d:\n",tot++);        printf("%d\n\n",res);    }    return 0;}





0 0
原创粉丝点击