boj problem 1340 简单题 费时很久 long long型 输入输出时 "%lld“ 是LL而不是11. 先快排 然后计算就可以
2011-03-23 00:19
387 查看
地址:http://acm.scs.bupt.cn/onlinejudge/showproblem.php?problem_id=1340
The Noisy PartySubmit: 209 Accepted:50Time Limit: 1000MS Memory Limit: 65536K
Description
Nitaa is holding a party at his house. But unfortunately Nitaa has received a noise complaint from his neighbor, Arsenal4, stating that his friends in his room are making too much noise.
Nitaa's N friends (1 <= N <= 10,000) all sit at various locations on a long one-dimensional pasture. The friends are very chatty people. Every pair of friends simultaneously carries on a conversation (so every friend is simultaneously talking with all of the N-1 other friends). When friend i talks with friend j, the volume of this talk must be equal to the distance between i and j, in order for j to be able to hear the conversation at all. Please help Nitaa compute the total volume of sound being generated by all N*(N-1) simultaneous conversation. That is the total volume of conversation between all pairs of friends.
Input
* Line 1: N
* Lines 2..N+1: The location of each friend (in the range 0..1,000,000,000).
Output
Only one line that shows the total volume of the noise.
Sample Input
5
1
5
3
2
4
Sample Output
40
Source
#include<iostream>
#include<math.h>
using namespace std;
int quicksort(long long *a,int low,int high)
{
long long temp;
int mid;
mid=(low+high)/2;
temp=a[mid];
a[mid]=a[low];
a[low]=temp;
while(low<high)
{
while(low<high && a[high]>temp)
high--;
if(low<high)
a[low++]=a[high];
while(low<high && a[low]<temp)
low++;
if(low<high)
a[high--]=a[low];
}
a[low]=temp;
return low;
}
void qsort(long long *a,int low,int high)
{
int temp;
if(low<high)
{
temp=quicksort(a,low,high);
qsort(a,low,temp-1);
qsort(a,temp+1,high);
}
}
int main()
{
long long a[10001],b[10001];
int N, i;
long long sum=0;
scanf( "%d", &N );
for( i=1; i<=N; i++)
cin>>a[i];
//scanf("%lld",a+i);
qsort(a,1,N);
b[1]=0; b[2]=a[2]-a[1];
sum=b[1]+b[2];
for(i=3;i<=N;i++)
{
b[i]=(a[i]-a[i-1])*(i-1)+b[i-1];
sum +=b [i];
}
sum=sum*2;
cout<<sum<<endl;
// printf( "%lld/n", sum*2 );
}
The Noisy PartySubmit: 209 Accepted:50Time Limit: 1000MS Memory Limit: 65536K
Description
Nitaa is holding a party at his house. But unfortunately Nitaa has received a noise complaint from his neighbor, Arsenal4, stating that his friends in his room are making too much noise.
Nitaa's N friends (1 <= N <= 10,000) all sit at various locations on a long one-dimensional pasture. The friends are very chatty people. Every pair of friends simultaneously carries on a conversation (so every friend is simultaneously talking with all of the N-1 other friends). When friend i talks with friend j, the volume of this talk must be equal to the distance between i and j, in order for j to be able to hear the conversation at all. Please help Nitaa compute the total volume of sound being generated by all N*(N-1) simultaneous conversation. That is the total volume of conversation between all pairs of friends.
Input
* Line 1: N
* Lines 2..N+1: The location of each friend (in the range 0..1,000,000,000).
Output
Only one line that shows the total volume of the noise.
Sample Input
5
1
5
3
2
4
Sample Output
40
Source
#include<iostream>
#include<math.h>
using namespace std;
int quicksort(long long *a,int low,int high)
{
long long temp;
int mid;
mid=(low+high)/2;
temp=a[mid];
a[mid]=a[low];
a[low]=temp;
while(low<high)
{
while(low<high && a[high]>temp)
high--;
if(low<high)
a[low++]=a[high];
while(low<high && a[low]<temp)
low++;
if(low<high)
a[high--]=a[low];
}
a[low]=temp;
return low;
}
void qsort(long long *a,int low,int high)
{
int temp;
if(low<high)
{
temp=quicksort(a,low,high);
qsort(a,low,temp-1);
qsort(a,temp+1,high);
}
}
int main()
{
long long a[10001],b[10001];
int N, i;
long long sum=0;
scanf( "%d", &N );
for( i=1; i<=N; i++)
cin>>a[i];
//scanf("%lld",a+i);
qsort(a,1,N);
b[1]=0; b[2]=a[2]-a[1];
sum=b[1]+b[2];
for(i=3;i<=N;i++)
{
b[i]=(a[i]-a[i-1])*(i-1)+b[i-1];
sum +=b [i];
}
sum=sum*2;
cout<<sum<<endl;
// printf( "%lld/n", sum*2 );
}
相关文章推荐
- 黑马程序员之C#编程基础学习笔记:提示用户输入用户名,然后再提示输入密码,如果用户名是"admin"并且密码是“888888”,则提示正确,否则提示错误,如果用户名不是admin还提示用户用户名不存在
- boj 1328 简单问题 不过要考虑输入大小 long long可以 不过VC++不能编译
- 解决SQL备份时,提示"不是多簇媒体集的一部分,可以使用BACKUP WITH FORMAT 来构造新的媒体集."
- LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面
- 一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制?
- linux mail命令用法浅析【用最简单的测试一下就可以了如 "mail 真实的邮箱账号"具体操作见下一篇文章】
- GCC出现warning: integer constant is too large for 'long' type"
- C语言 实现一个简单的Shell (支持管道和"cd")
- 你可能不知道位图,但是它真的很有用,特殊情况可以使时间复杂度降低不是一两个档次那么简单...
- boj 1511 简单科学计算器 代码非常繁杂 思路不是特别好`
- "Installing Software" has encountered a problem---pydev on ubuntu
- "ScriptManager不是已知元素 可能存在编译错误"
- 【量化小讲堂-Python&Pandas系列11】法码三因子选股模型,有多少人可以跑赢
- SSIS 之 Exception: Failed to retrieve long data for column "TS_Description"
- 关于"引用"的简单例子
- 重启一下机子就出现电脑下面提示 "windows7内部版本7600此windows副本不是正版"
- 解决"线程间操作无效: 从不是创建控件的线程访问它"
- Sql server中mybatis分页提示 'ROWNUMBER' 不是可以识别的内置函数名称
- 随机计算TFIDF作为权重,然后利用余弦距离进行聚类,用的是简单k-means算法。
- 简单编程(三)编写一个程序,要求用户输入两个整数,然后计算并输出他们的和、差、积、商。