Static Sushi AtCoder - 4118 (技巧枚举)
Problem Statement
"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.
Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are Npieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is xi meters. Also, the i-th sushi has a nutritive value of vikilocalories.
Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1kilocalories per meter.
Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.
Constraints
- 1≤N≤105
- 2≤C≤1014
- 1≤x1<x2<…<xN<C
- 1≤vi≤109
- All values in input are integers.
Subscores
- 300 points will be awarded for passing the test set satisfying N≤100.
Input
Input is given from Standard Input in the following format:
N C
x1 v1
x2 v2
:
xN vN
Output
If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.
Sample Input 1
3 20
2 80
9 120
16 1
Sample Output 1
191
There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.
Sample Input 2
3 20
2 80
9 1
16 120
Sample Output 2
192
The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192kilocalories on balance, which is the largest possible value.
Sample Input 3
1 100000000000000
50000000000000 1
Sample Output 3
0
Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.
Sample Input 4
15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000
Sample Output 4
6500000000
All these sample inputs above are included in the test set for the partial score.
思路:
所有的路线等效后只有4种类型。
1,只顺时针走。
2,只逆时针走。
3,先顺时针,再逆时针。
4,先逆时针,在瞬时针。
那么我们不妨处理对于每一个寿司,顺时针和逆时针到这个寿司最大的收益(可能没有走到这个节点,在前面节点的时候就走掉了。)
然后我们枚举对于每一个寿司 i ,
顺时针走到这个寿司的最大收获
逆时针走到这个寿司的最大收获
顺时针走到这个寿司,在逆时针走到 i -1 寿司的最大收获
逆时针走到这个寿司,在顺时针走到 i + 1 寿司的最大收获
然后取这n个中的最大值,即是答案。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n;
ll c;
struct node
{
ll v;
ll x;
};
node a[maxn];
ll shun[maxn];
ll ni[maxn];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n>>c;
repd(i,,n)
{
cin>>a[i].x>>a[i].v;
}
ll ans=0ll;
repd(i,,n)
{
shun[i]=shun[i-]+a[i].v-(a[i].x-a[i-].x);
}
repd(i,,n)
{
shun[i]=max(shun[i-],shun[i]);
}
a[n+].x=c;
for(int i=n;i>=;i--)
{
ni[i]=ni[i+]+a[i].v-(a[i+].x-a[i].x);
}
for(int i=n;i>=;i--)
{
ni[i]=max(ni[i],ni[i+]);
}
for(int i=;i<=n;i++)
{
ans=max(ans,shun[i]);
ans=max(ans,ni[i]);
ans=max(ans,shun[i]-a[i].x+ni[i+]);
ans=max(ans,ni[i]-(c-a[i].x)+shun[i-]);
}
cout<<ans<<endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Static Sushi AtCoder - 4118 (技巧枚举)的更多相关文章
- AtCoder Regular Contest 096 D - Static Sushi(线性dp)
Problem Statement "Teishi-zushi", a Japanese restaurant, is a plain restaurant with only o ...
- Java单例模式的各种实现(饿汉、懒汉、静态内部类、static代码块、enum枚举类型)
饿汉模式 饿汉模式就是立即加载,在方法调用前,实例就已经被创建了,所以是线程安全的. public class MyObject1 { private static MyObject1 myObjec ...
- たくさんの数式 / Many Formulas AtCoder - 2067 (枚举二进制)
Problem Statement You are given a string S consisting of digits between 1 and 9, inclusive. You can ...
- UVa140 Bandwidth 小剪枝+双射小技巧+枚举全排列+字符串的小处理
给出一个图,找出其中的最小带宽的排列.具体要求见传送门:UVa140 这题有些小技巧可以简化代码的编写. 本题的实现参考了刘汝佳老师的源码,的确给了我许多启发,感谢刘老师. 思路: 建立双射关系:从字 ...
- UVA-1312 Cricket Field (技巧枚举)
题目大意:在一个w*h的网格中,有n个点,找出一个最大的正方形,使得正方形内部没有点. 题目分析:寻找正方形实质上等同于寻找矩形(只需令长宽同取较短的边长).那么枚举出所有可能的长宽组合取最优答案即可 ...
- ZROI17普及23-A.如烟题解--技巧枚举
题目链接 因版权原因不予提供 分析 别看这是普及模拟赛,其实基本上是提高难度...像这题做NOIpT1的话也说的过去 有个很显然的暴力思路就是枚举c,a,b,时间复杂度\(O(N^3)\), 然后正解 ...
- 【uva 1312】Cricket Field(算法效率--技巧枚举)
题意:一个 L*R 的网格里有 N 棵树,要求找一个最大空正方形并输出其左下角坐标和长.(1≤L,R≤10000, 0≤N≤100) 解法:枚举空正方形也就是枚举空矩阵,先要固定一个边,才好继续操作. ...
- 【AtCoder】ARC096(C - F)
听说日本题思维都很棒,去涨涨智商qwq C - Half and Half 题解 枚举买多少个AB披萨也行 但是关于买x个AB披萨最后的总花费是个单峰函数,可以三分 这题有点像六省联考2017D1T1 ...
- AtCoder Regular Contest 096
AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...
随机推荐
- 【Zabbix】Zabbix Server自动发现
Zabbix自动发现 由于有上百台的虚拟机需要监控,如果一个个去添加配置,费时费力.Zabbix的自动发现,可以自动发现需要监控的机器,监控相应指标. 前置条件 安装部署好Zabbix Server. ...
- 第一次AOP,附上使用DEMO,可用在简单生产环境了
demo代码如下 public class ConsoleTimeAttribute : ApectBaseAttribute { public override void Before(ApectC ...
- Git在商业项目中的使用流程
一 引言 这一篇文章还是记录我在杭州工作的总结. 我刚来公司的时候,对Git的使用很头痛,因为在学校里面很少用这个东西,即使用,一般也只有一个分支,不会出现代码冲突和代码合并的情况.但是公司里面一个项 ...
- SQLServer之创建表值函数
表值函数创建注意事项 用户定义表值函数返回 table 数据类型. 对于内联表值函数,没有函数主体,表是单个 SELECT 语句的结果集. 表值函数主要用于数据计算出来返回结果集. 使用SSMS数据库 ...
- SQLServer之存储过程简介
存储过程定义 存储的过程 (存储过程(数据库引擎)) 是存储在数据库中的可执行对象. 存储过程分类 系统存储过程 系统存储过程是 SQL Server系统自身提供的存储过程,可以作为命令执行各种操 ...
- C#如何生成JSON字符串提交给接口(服务器)
C#如何生成JSON字符串提交给接口(服务器) 第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化 ...
- c/c++ 多线程 等待一次性事件 异常处理
多线程 等待一次性事件 异常处理 背景:假设某个future在等待另一个线程结束,但是在被future等待的线程里发生了异常(throw一个异常A),这时怎么处理. 结果:假设发生了上面的场景,则在调 ...
- c/c++ linux epoll系列3 利用epoll_wait设置timeout时间长度
linux epoll系列3 利用epoll_wait设置timeout时间长度 epoll_wait函数的第四个参数可以设置,epoll_wait函数的等待时间(timeout时间长度). 例子1, ...
- ATL右键文件菜单
自己写的小程序中用到的,网上资料相对还是毕竟全的,这里再整理下.毕竟我也不是很了解ATL,里面估计还是有不少问题的,就当作参考吧. 1.创建ATL工程,这个没什么好讲的. 我对COM组件没什么研究,这 ...
- CVE-2018-8120 分析
目录 CVE-2018-8120 分析 1.实验环境 1.1.操作系统 1.2.用到的分析工具 2.假如 2.1.我想提权 2.2. 有一个处于内核空间,极少被调用的函数 2.3.R3任意修改R0地址 ...