SRM 149

DIV2 1000pt

题意:

  对于n个人,第i人有pi的钱。将他们分成不超过四个组,每组统一交费x,对每个人,若他拥有的钱超过x则交费,否则不交费。问最多能使这些人交多少钱。

  1<= n <= 50,0 <= pi <= 1000。

tag:greedy,think

解法:枚举所有分组情况,每组交费x为该组中最小的pi。如果分组不足四组,补足四组,每组均含一个人钱数为0的人。

Ps:感觉自己的代码写得比题解舒服.....

 #line 7 "Pricing.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; inline int MyMod( int a , int b ) { return (a%b+b)%b;} int n, ans; bool cmp(int x, int y)
{
return x > y;
} class Pricing
{
public:
int maxSales(vector <int> p){
p.PB(), p.PB(), p.PB();
n = SZ (p);
if (!n) return ;
ans = ;
sort (p.begin(), p.end(), cmp); for (int i = ; i < n; ++ i)
for (int j = i+; j < n; ++ j)
for (int k = j+; k < n; ++ k)
for (int a = k+; a < n; ++ a){
int tmp = ;
tmp = p[i] * (i+) + p[j] * (j-i);
tmp += p[k] * (k-j) + p[a] * (a-k);
ans = max (ans, tmp);
} return (ans);
}
//by plum rain
};

DIV1 500pt

题意:

  字符串匹配。给出字典和一个字符串,询问该字符串是否能用字典里的单词表示,有一种表示方法还是多种表示方法,如果只有一种,输出表示方法。

tag:dp

Ps:以后写dp一定要提前写好状态转移方程- -,错了好多次- -。还有,此题要用long long。

 #line 7 "MessageMess.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ;
const int N = ; inline int MyMod( int a , int b ) { return (a%b+b)%b;} int64 dp[N], pat[N]; class MessageMess
{
public:
string restore(vector <string> d, string s){
CLR (dp), CLR (pat);
int m = SZ (d), n = SZ (s);
for (int i = ; i < n; ++ i){
for (int j = ; j < m; ++ j){
int size = SZ (d[j]);
int pos = i - size;
if (pos < -) continue;
string tmp(s, pos+, size);
if (tmp == d[j]){
if (pos == -)
dp[i] += , pat[i] = j;
else if (dp[pos] > )
dp[i] += dp[pos], pat[i] = j;
}
}
} if (!dp[n-]) return "IMPOSSIBLE!";
if (dp[n-] != ) return "AMBIGUOUS!";
VS ans; ans.clear();
int pos = n - ;
while (pos >= ){
int num = pat[pos];
ans.PB(d[num]);
pos -= SZ (d[num]);
}
int len = SZ (ans);
string ret; ret.clear();
ret += ans[len-];
for (int i = len-; i >= ; -- i)
ret += " " + ans[i];
return (ret);
} //by plum rain
};

DIV1 1000pt

题意:

  在平面坐标系XOY中,以xi严格递增的顺序给出一些点的坐标(xi, yi),然后将点(xi, yi)与(x[i+1], y[i+1])连接,得到一条折线。给定整数p,求在这条折线上的某一线段,起点终点分别为(a, b), (a+p, b'),使这一线段平均y值最大,输出最大的平均y值。

  x, y范围[0, 10000]。

tag:think, (三分法), good

解法:

  没做出来。官方题解提供了两种方法,第一种方法似乎要用三分法,还不太懂- -。

  第二种方法是,考虑线段(a, b)——(a+p, b')在折线上移动,记该线段的平均y值为tmp。该线段从原位置移动到(a+1, c)和(a+p+1, c'),则tmp' = tmp - (f(a+1) + f(a)) / 2 + (f(a+p+1) + f(a+p)) / 2), ans = max (tmp, ans)(其中,f(a)表示折线段的在x = a时的y值,ans表示答案)。

  然后考虑到线段(a, b)——(a+1, c)和线段(a+p, b')——(a+p+1, c')的斜率可能不同,所以答案线段也有可能出现在移动途中。记两线段y值相等的点为(x1, y0) 和 (x2, y0), 则ans = max (ans, tmp' + ((y0 + c) / 2)* (a+1 - x1) - ((y0 + c') / 2) * (a+p+1 - x2))。

PS:

  返回double类型的要注意精度,并且,如果你使用topcoder的插件,在你自己的电脑上判断答案对错时,有可能因为精度问题而将你的正确答案判为错,输出一下返回值看一下是否正确就行。

 /*
* Author: plum rain
*/
#line 11 "topcoder.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; inline int MyMod( int a , int b ) {a = a % b;if (a < ) a += b; return a;} class GForce
{
public:
double avgAccel(int p, vector <int> a, vector <int> t){
int n = SZ (a);
VD spd;
spd.clear();
for (int i = ; i < n-; ++ i){
spd.PB(a[i] + 0.0);
double now = a[i] + 0.0;
double cnt = (a[i+] - a[i] + eps) / (t[i+] - t[i] + eps);
for (int j = t[i] + ; j < t[i+]; ++ j)
now += cnt, spd.PB (now);
}
spd.PB (a[n-]); double tmp = 0.0;
for (int i = ; i < p; ++ i)
tmp += (spd[i] + spd[i+]) / 2.0;
double ans = tmp;
for (int i = ; i < spd.size() - p; ++ i){
tmp -= (spd[i] + spd[i-]) / 2.0;
tmp += (spd[i+p] + spd[i+p-]) / 2.0;
ans = max (ans, tmp); double del = (spd[i] + spd[i-]) / 2.0;
double add = (spd[i+p] + spd[i+p-]) / 2.0;
double haf = tmp + (*spd[i]/8.0 + spd[i-] / 8.0) - (*spd[i+p]/8.0 + spd[i+p-]/8.0);
ans = max (ans, haf);
if(fabs(del - add) > eps){
del = (spd[i] - spd[i-]) / 2.0;
add = (spd[i+p] - spd[i+p-]) / 2.0;
double x = ((spd[i] - spd[i+p]) / 2.0) / (del - add);
if (x > eps && x < - eps){
double y = ((spd[i]*spd[i+p-] - spd[i+p]*spd[i-]) / 2.0) / (del - add);
double xy = tmp + (spd[i] + y) * x / 2.0 - (y + spd[i+p]) * x / 2.0;
ans = max (ans, xy);
}
}
} double ret = ans / (p + 0.0);
return (ret);
}
};

SRM 150

DIV2 1100pt

题意:有一个有限大小的平面和一个球,平面上有两种障碍物,第一种障碍物被球撞后会消失,第二种不会。球撞两种障碍物均会反弹,速度不变。给出平面大小和障碍物分布,问球从左上角一某一初速度开始,需要多少时间能让平面上的第一类障碍物全部消失。

解法:模拟。

tag: simulate

 #line 10 "BrickByBrick.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; inline int MyMod( int a , int b ) {a = a % b;if (a < ) a += b; return a;} //drt
//1 0 2
//0 0 0
//3 0 4
struct STA{
int x, y;
int drt;
int time;
void clr(){
x = ; y = ; time = ;
}
}; int an[][];
int xmax, ymax, tot;
int ans; inline int change(char x)
{
if (x == '.') return ;
if (x == 'B'){
++ tot;
return ;
}
if (x == '#') return ;
} void init(VS m)
{
CLR (an);
tot = ;
ymax = SZ (m);
for (int i = ; i < ymax; ++ i){
string s = m[i];
xmax = SZ (s);
for (int j = ; j < xmax; ++ j)
an[*i+][*j+] = change(s[j]);
}
} void move(int& x, int& y, int d, int& t)
{
++ t;
if (d == ) -- x, -- y;
if (d == ) ++ x, -- y;
if (d == ) -- x, ++ y;
if (d == ) ++ x, ++ y;
} void D(int& x, int& y, int& d)
{
if (!(x & )){
if (d & )
-- x, d = (d == ? : );
else
++ x, d = (d == ? : );
return ;
}
if (d < ) -- y, d = (d == ? : );
else ++ y, d = (d == ? : );
} int solve()
{
STA sta; sta.clr();
sta.drt = ;
int num = ;
while (sta.time < && sta.time < ans){
move(sta.x, sta.y, sta.drt, sta.time); int x = sta.x, y = sta.y;
int dtmp = sta.drt;
D(x, y, sta.drt);
if (x >= && x <= *xmax && y >= && y <= *ymax){
if (an[y][x] == ) sta.drt = dtmp;
if (an[y][x] == ) an[y][x] = , ++ num;
}
if (num == tot){
return sta.time;
}
}
return -;
} class BrickByBrick
{
public:
int timeToClear(vector <string> m){
init (m); ans = maxint; return (solve());
}
};

DIV1 500pt

题意:给一个用‘A’-‘Z’表示颜色的字符串s,要求将一空白字符串变成给定字符串,(每次变化能将任意个连续位置的字符均变为某个任意的字符)问最少变化多少次。比如给定ABGBA,第一次将空白字符串变为AAAAA,第二次变为ABBBA,第三次变为ABGBA,答案为3。(s.size() <= 50)

解法:设置数组,m[][][]。left位置到left+size-1位置此时颜色均为c,将他们变化为给定字符串对应部分所需要最少的变化次数为m[left][size][c]。

   状态转移方程:m[][0][] = 0,

          if (color == s[l]) m[left][size][c] = m[left+1][size-1][color];

          else m[left][size][color] = min {1 + m[left][i][color] + m[left+i][size-i][k] | 0 < i <= size, 'A' <= k <= 'Z'}

   由于要用到记忆化搜索的思想,我又想写成DP,所以感觉写成了四不像- -

tag:dp, memoization, good

 #line 10 "StripePainter.cpp"
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <set>
#include <queue>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; inline int MyMod( int a , int b ) {a = a % b;if (a < ) a += b; return a;} string s;
int m[][][]; int ask(int l, int n, char c)
{
int& a = m[l][n][(int)c]; if (!n) return ; if (a != -) return a;
a = maxint; if (c == s[l]){
a = ask (l+, n-, c);
return a;
} for (int i = ; i < n; ++ i)
for (int j = 'A'; j <= 'Z'; ++ j)
a = min (a, + ask(l, i, s[l]) + ask(l+i, n-i, c));
a = min (a, + ask(l, n, s[l]));
return a;
} class StripePainter
{
public:
int minStrokes(string stripes){
s.clear();
s = stripes;
memset (m, -, sizeof (m)); int size = SZ (s);
for (int i = ; i < size; ++ i)
for (int j = ; j + i <= size; ++ j)
for (int k = 'A'-; k <= 'Z'; ++ k)
m[i][j][k] = + ask(i, j, (char)k); return (m[][size][(int)s[]]);
}
};

SRM149 - SRM150(少SRM150-DIV1-LV3)的更多相关文章

  1. SRM144 - SRM 148(少144-DIV1-LV3,147-DIV2-LV3)

    SRM 144 DIV 1 500pt tag:组合 题意:彩票中奖.给定n, m,从1-n中选择m个数组成数列a1, a2, a3...am.对于数列{am}分别满足以下条件的概率: (1)数列所有 ...

  2. css开发经验&错误习惯

    CSS开发经验 1.尽量用class来定义样式.尽量少使用  .div1 ul li{}这样的样式下去,因为如果li里面还有<div><ul><li>这些元素的话会 ...

  3. CSS开发经验

    1.尽量用class来定义样式.尽量少使用  .div1 ul li{}这样的样式下去,因为如果li里面还有<div><ul><li>这些元素的话会造成干扰,应该给 ...

  4. TopCoder SRM500 Div1 500 分治

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-500.html SRM500 Div1 500 没想到 double 的精度居然没有爆-- 考虑以 ...

  5. redux-amrc:用更少的代码发起异步 action

    很多人说 Redux 代码多,开发效率低.其实 Redux 是可以灵活使用以及拓展的,经过充分定制的 Redux 其实写不了几行代码.今天先介绍一个很好用的 Redux 拓展-- redux-amrc ...

  6. 搞了我一下午竟然是web.config少写了一个点

    Safari手机版居然有个这么愚蠢的bug,浪费了我整个下午,使尽浑身解数,国内国外网站搜索解决方案,每一行代码读了又想想了又读如此不知道多少遍,想破脑袋也想不通到底哪里出了问题,结果竟然是web.c ...

  7. 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。

    最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...

  8. 11 个很少人知道但很有用的 Linux 命令

    Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.Linux命令和它们的转换对于Linux用户.Shell脚本程序员和管理员来说是最有 ...

  9. 让你少走弯路的搭建树莓派的Net与NodeJS运行环境

      树莓派是当前最火的嵌入计算平台没有之一,树莓派可以给我们无数的想象,树莓派的高性能.低功耗.低成本.可扩展性(最新的树莓派原生支持WIFI和蓝牙,这功能太赞了)深受大家的喜爱.虽然树莓派到目前为止 ...

随机推荐

  1. asp.net数据导出到excel表格,并设置表格样式

    1.首先在项目中添加引用

  2. 【SSMS增强工具】SQL Sharper 2014介绍

    产品介绍 SQL Sharper是一款SQL Server Management Studio插件,用于数据库对象快速查询.表结构查询.优化查询结果导出.代码生成等方面. 适用人群:T-SQL开发者. ...

  3. iOS支付 IPAPayment demo iTunes Conection里面添加测试帐号,添加商品,实现购买过程

    https://github.com/ccguo/IAPPaymentDemo 发一个demo

  4. C#入门经典(第五版)学习笔记(四)

    ---------------集合.比较和转换--------------- C#中的数组是作为System.Array类的实例实现的,它们是集合类(Collection Classes)中的一种类型 ...

  5. javascript--烟火效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <meta nam ...

  6. PHP 用Class构造JSON数据

    header('Content-type: appliction/json; charset=shift-JIS'); // error //{ // "result": fals ...

  7. ubuntu 升级命令

    apt-get update && apt-get dist-upgrade

  8. Asp.net 实现图片缩放 无水印(方法一)

    /// <summary> /// 图片缩放 无水印 /// </summary> /// <param name="sourceFile">图 ...

  9. MySql数据库4【命令行赋权操作】

    MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant selec ...

  10. Redis 中的事务

    Redis支持简单的事务 Redis与mysql事务的对比 Mysql Redis 开启 start transaction muitl 语句 普通sql 普通命令 失败 rollback 回滚 di ...