link:http://codeforces.com/problemset/problem/340/C

开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊!

后来明白了。

首先考虑第一个数字,就是和0想减的内个。那么剩下的n-1个数字有(n-1)!个排列方式。所以呢,在n!个式子里面,第一个位置的和就是:a1 * (n-1)! + a2 * (n-1)! + ...... + an * (n-1)!;

然后考虑其它位置:对于ai , aj . 并且相邻。那么剩下 n - 2 个数字,这些数字有 (n-2)!个排列方式,然后把相邻的 ai aj 插入,有 n-1 种插入方式,所以呢,一共有 (n-1) * (n-2)! = (n-1)! 个方式,其它位置的和就是:(n-1)! * fabs(ai - aj) 【i != j, 1 <= i, j <= n】

关键是第二个式子怎么求呢?好像很难得样子。可以找规律啊。比如,a1, a2 , a3, a4.

因为有绝对值,我可以先排序,先算正的,在 *2就行了。假设a1 > a2 > a3 > a4

设:

以a2结尾的ai - aj 的和是S2.

以a3……和是S3

。a4…………S4

那么:

S2 = a1 - a2

S3 = a1 - a3 + a2 - a3 = (a1 - a2 + a2 - a3) + a2 - a3 = S2 + 2 *(a2 - a3)

S4 = a1 - a4 + a2 - a4 + a3 - a4 = (a1 - a3 + a3 - a4) + (a2 - a3 + a3 - a4) + (a3 - a4) = S3 + 3 * (a3 - a4)

我们要求的就是S2 + S3 + S4。

没发现Si 可以递推么~

 /*
  *       Filename:  tourist.cpp
  *        Created:  09/01/2013 09:14:49 AM
  *         Author:  liuxueyang (lxy), zypz457@sina.com
  *   Organization:  Hunnan University
  */

 #include <iostream>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <cmath>
 #include <cctype>
 #include <algorithm>
 #include <queue>
 #include <deque>
 #include <queue>
 #include <list>
 #include <map>
 #include <set>
 #include <vector>
 #include <utility>
 #include <functional>
 #include <fstream>
 #include <iomanip>
 #include <sstream>
 #include <numeric>
 #include <cassert>
 #include <ctime>
 #include <iterator>
 const int INF = 0x3f3f3f3f;
 ;
 using namespace std;
 #define LL long long
 ];
 int main ( int argc, char *argv[] )
 {
 #ifndef ONLINE_JUDGE
     freopen("in.txt","r",stdin);
 #endif
     ios::sync_with_stdio(false);
     int n;
     while (~scanf("%d", &n)) {
         LL katusya = , tatusya = , safe = ;
         ; i < n; scanf("%d",a+i), katusya+=a[i], ++i);
         sort(a, a + n);
         ; i < n; ++i)  {
             safe += (LL)i * (a[i]-a[i-]);
             tatusya += safe;
         }
         tatusya *= ;
         tatusya += katusya;
         LL touch = __gcd(tatusya, (LL)n);
         printf("%lld %lld\n", tatusya/touch, n/touch);
     }
         return EXIT_SUCCESS;
 }                /* ----------  end of function main  ---------- */

比赛没做出来,但是觉得这道题目很好!

用动态规划的思想,很巧妙啊。

嗨,中村。

codeforces 340C Tourist Problem的更多相关文章

  1. codeforces 340C Tourist Problem(公式题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Tourist Problem Iahub is a big fan of tou ...

  2. codeforces 340C Tourist Problem(简单数学题)

    题意:固定起点是0,给出一个序列表示n个点,所有点都在一条直线上,其中每个元素代表了从起点到这个点所走的距离.已知路过某个点不算到达这个点,则从起点出发,到达所有点的方案有许多种.求所有方案走的总路程 ...

  3. CodeForces - 340 C - Tourist Problem

    先上题目: A - Tourist Problem Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  4. Codeforces Round #198 (Div. 2) C. Tourist Problem

    C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)

    C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. C. Tourist Problem

    http://codeforces.com/problemset/problem/340/C 赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容 ...

  7. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...

  8. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  9. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

随机推荐

  1. Redis - 作为 LRU 缓存

    一.简介 LRU 实际上是被唯一支持的数据移除方法,同时也是 memcached 默认支持的缓存算法. 二.配置内存大小 在 redis.conf 文件中使用 maxmemory 指令能够配置内存大小 ...

  2. Delphi TTable 组件

    TTable 是 TDataSet 的派生类,它是基于 BDE 数据库引擎的数据集组件,也是一个较简单的数据组件,可以直接从数据库中获取数据表的数据,只需设置连接的数据库属性(Database) 和所 ...

  3. C语言输出字符串

    在VS2012中,使用gets_s()方法,其中第二个参数可以用sizeof(...)代替.例子代码如下: #include <stdio.h> int main( ) { ]; gets ...

  4. MVP 个人理解2

    根据我的理解,画了个图 这次又看了下 较复杂点的例子. 往往一个项目有多个部份,我们可以按功能分成几个activity, 每个activity有自己的view和数据model,因此也有自己的逻辑 pr ...

  5. 解决Selenium与firefox浏览器版本不兼容问题

    因为在用java打开firefox浏览器的时候报错 org.openqa.selenium.firefox.NotConnectedException: Unable to connect to ho ...

  6. Educational Codeforces Round 14 E.Xor-sequences

    题目链接 分析:K很大,以我现有的极弱的知识储备,大概应该是快速幂了...怎么考虑这个快速幂呢,用到了dp的思想.定义表示从到的合法路径数.那么递推式就是.每次进行这样一次计算,那么序列的长度就会增加 ...

  7. SSM框架学习之高并发秒杀业务--笔记4-- web层

    在前面几节中已经完成了service层和dao层,到目前为止只是后端的设计与编写,这节就要设计到前端的设计了.下面开始总结下这个秒杀业务前端有哪些要点: 1. 前端页面的流程 首先是列表页,点某个商品 ...

  8. Windows Store App 全球化:在XAML元素中引用字符串资源

    在应用程序中可以通过XAML元素和后台代码两种方式引用资源文件中的字符串资源.本小节先讲述如何在XAML元素中引用字符串资源的相关知识点. 在XAML元素中可以通过使用x:Uid属性来引用资源文件中的 ...

  9. NGINX location 在配置中的优先级

    location表达式类型 ~ 表示执行一个正则匹配,区分大小写 ~* 表示执行一个正则匹配,不区分大小写 ^~ 表示普通字符匹配.使用前缀匹配.如果匹配成功,则不再匹配其他location. = 进 ...

  10. ajax转换成json参数

    //提交表单 $('#submit').click(function(){ var datas = $("#iform").serializeJson(); datas.actio ...