250:

题目大意:

在一个N行无限大的网格图里,每经过一个格子都要付出一定的代价。同一行的每个格子代价相同。 给出起点和终点,求从起点到终点的付出的最少代价。

思路:

最优方案肯定是从起点沿竖直方向走到某一行,然后沿水平方向走到终点那一列,然后再沿竖直方向走到终点那一行。

枚举是通过哪一行的格子从起点那列走到终点那列的,求个最小值就好了。

代码:

 // BEGIN CUT HERE  

 // END CUT HERE
#line 5 "LongMansionDiv1.cpp"
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std; typedef long long ll; class LongMansionDiv1
{
public:
ll F(int i, int j, vector <int> &t)
{
ll res = ;
if (i > j) swap(i, j);
for (int k = i; k <= j; ++k)
res += t[k];
return res;
}
long long minimalTime(vector <int> t, int sX, int sY, int eX, int eY)
{
//$CARETPOSITION$
int n = t.size();
if (sY > eY) swap(sX, eX), swap(sY, eY);
ll ans = 1e18;
for (int j = ; j < n; ++j)
{
ans = min(ans, F(sX, j, t) + 1ll * t[j] * (eY - sY + ) + F(j, eX, t) - t[j] - t[j]);
}
return ans;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arr0[] = {, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; int Arg4 = ; long long Arg5 = 29LL; verify_case(, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); }
void test_case_1() { int Arr0[] = {, , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; int Arg4 = ; long long Arg5 = 15LL; verify_case(, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); }
void test_case_2() { int Arr0[] = {, , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; int Arg4 = ; long long Arg5 = 1016LL; verify_case(, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); }
void test_case_3() { int Arr0[] = {, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; int Arg4 = ; long long Arg5 = 293443080673LL; verify_case(, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); }
void test_case_4() { int Arr0[] = {}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; int Arg2 = ; int Arg3 = ; int Arg4 = ; long long Arg5 = 1LL; verify_case(, Arg5, minimalTime(Arg0, Arg1, Arg2, Arg3, Arg4)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
LongMansionDiv1 ___test;
___test.run_test(-);
system("pause");
}
// END CUT HERE

500:

题目大意:

给出一棵以0为根的树,从根出发,走过一些节点。 每个节点有一个得分,可能正可能负,可以重复经过节点,但是只有第一次经过会改变当前得分。 如果当前得分为负,会马上变成0.

求最大得分。

比赛的时候没有想出来,好菜菜QAQ。

思路:

这道题主要是 “如果当前得分为负,会马上变成0” 这个地方不好处理。

考虑最后一次得分由负变成0的时刻。这个时候访问过的节点也组成以0为根的树,可以认为这棵树上的节点的得分都是0.

因此可以把问题转化一下:

可以把包含节点0的一个联通块内的节点得分都变成0, 然后再求一个包含节点0的联通块得分和最大。

用A,B两个数组来做DP。

A[x]表示考虑以x为根的子树,且x必须取的最大值。

B[x]表示考虑以x为根的子树,且允许把x的权值变成0(相当于允许不取x)的最大值。

A[x] =  max(val[x] + sum(A[sons of x]), 0)

B[x] =  max(sum(B[sons of x], A[x]))

代码:

 // BEGIN CUT HERE  

 // END CUT HERE
#line 5 "OwaskiAndTree.cpp"
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
class OwaskiAndTree
{
public:
int maximalScore(vector <int> parent, vector <int> pleasure)
{
//$CARETPOSITION$
int n = pleasure.size();
long long a[] = {}, b[] = {};
for (int i = n - ; i >= ; --i)
{
a[i] += pleasure[i];
a[i] = max(a[i], 0ll);
b[i] = max(b[i], a[i]); if (i == ) continue;
int u = parent[i - ];
a[u] += max(a[i], 0ll);
b[u] += b[i];
}
return b[];
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arr0[] = {, , , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {, , -, -, -, -, , , , }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maximalScore(Arg0, Arg1)); }
void test_case_1() { int Arr0[] = {, , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {, , , -, -}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maximalScore(Arg0, Arg1)); }
void test_case_2() { int Arr0[] = {, , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {, , -, -, , , , -, }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maximalScore(Arg0, Arg1)); }
void test_case_3() { int Arr0[] = {, , , , , , , , , , , , , , , , , , }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {-, , , , -, , -, -, , -, , -, , , -, , , -, , -}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maximalScore(Arg0, Arg1)); }
void test_case_4() { int Arr0[] = {}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arr1[] = {-}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); int Arg2 = ; verify_case(, Arg2, maximalScore(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
OwaskiAndTree ___test;
___test.run_test(-);
system("pause");
}
// END CUT HERE

SRM 719 Div 1 250 500的更多相关文章

  1. 竞赛图的得分序列 (SRM 717 div 1 250)

    SRM 717 DIV 1 中 出了这样一道题: 竞赛图就是把一个无向完全图的边定向后得到的有向图,得分序列就是每个点的出度构成的序列. 给出一个合法的竞赛图出度序列, 要求构造出原图(原题是求(u, ...

  2. Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索

    最近连续三次TC爆零了,,,我的心好痛. 不知怎么想的,这题把题意理解成,第一次选择j,第二次选择i后,只能从1~i-1.i+1~j找,其实还可以从j+1~n中找,只要没有被选中过就行... [题意] ...

  3. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  4. Topcoder SRM 661 (Div.1) 250 MissingLCM - 数论

    [题意] 给你一个数N(1<=N<=10^6),要求最小的M(M>N),使得lcm(n+1,n+2,...m)=lcm(1,2,3,...,m) [思路] 手速太慢啦,等敲完代码的时 ...

  5. 【topcoder SRM 702 DIV 2 250】TestTaking

    Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...

  6. Topcoder口胡记 SRM 562 Div 1 ~ SRM 599 Div 1

    据说做TC题有助于提高知识水平? :) 传送门:https://284914869.github.io/AEoj/index.html 转载请注明链接:http://www.cnblogs.com/B ...

  7. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  8. TopCoder SRM 667 Div.2题解

    概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...

  9. 刷题记录:Codeforces Round #719 (Div. 3)

    Codeforces Round #719 (Div. 3) 20210703.网址:https://codeforces.com/contest/1520. 没错,我是个做div3的蒟蒻-- A 大 ...

随机推荐

  1. c语言指针详解 经典

    指针是C语言中广泛使用的一种数据类型. 运用指针编程是C语言最主要的风格之一.利用指针变量可以表示各种数据结构: 能很方便地使用数组和字符串: 并能象汇编语言一样处理内存地址,从而编出精练而高效的程序 ...

  2. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-有时候项目会无法编译,重新生成就自动卡死或者自动退出怎么办

    删除所有中文注释,有中文注释则不一定能编译成功.     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123   我的在线论坛: ...

  3. ffplay的快捷键以及选项 FFmpeg 基本用法 FFmpeg常用基本命令 ffmpeg常用转换命令,支持WAV转AMR

    ffmpeg -i 32_mkv_h264_718x480_ac3.mkv  -codec copy -bsf:v h264_mp4toannexb  -f mpegts xx.ts ./ffmpeg ...

  4. RxJava2.0教程

    尝试在新的项目中,引用一些流行的优秀的开源框架,在简书上偶然发现一篇很棒的写RxJava 2.0的帖子,个人认为非常适合Android开发者,你可以先知道怎么使用,然后再弄清楚里面做了哪些事情,例如可 ...

  5. Codeforces 385 C Bear and Prime Numbers

    题目链接~~> 做题感悟:这题属于想法题,比赛时直接做的 D 题.可是处理坐标处理的头晕眼花的结果到最后也没AC. 解题思路: 由于查询的时候仅仅考虑素数,so~我们仅仅考虑素数就能够,这就须要 ...

  6. PyPy与VirtualEnv的安装问题

    PyPy与VirtualEnv的安装问题 说明:本博客由bitpeach原创撰写,请勿商用.转载免费,请注明出处,谢谢. (零)背景 VirtualEnv工具的详细内容是什么,请自行百度.这里大概简介 ...

  7. for in 与 Object.keys 与 hasOwnProperty区别

    1.结论 for in遍历对象所有可枚举属性 包括原型链上的属性 Object.keys遍历对象所有可枚举属性 不包括原型链上的属性 hasOwnProperty 检查对象是否包含属性名,无法检查原型 ...

  8. C/C++ 编程计算2的100万次方(m的n次方),超长结果输出文件

    #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string> ...

  9. Sphinx-PHP使用Sphinx搜索技术

    Sphinx继承到PHP程序中, 有两种方式: Sphinx PHP模块: 编译生成PHP扩展模块 Sphinx API类: 直接使用Sphinx提供的类即可 首先我们应该使用Sphinx做以下几件事 ...

  10. jquery mobile 的loading提示“正在加载...”在不同版本中的不同实现方式

    在jquery mobile开发中,在页面的切换.或者ajax获取数据时由于网速慢等其他原因,会有一个加载的时间,如果能在这段时间给一个“正在加载...”的提示,用户体验会更好.下面来简单的介绍一下在 ...