Gym - 100637B Lunch 规律
题意:n个点,给定起点和终点,可以每次可以走一格或两格,走一格则需要一个代价,每个格子只能走一次,问从起点到终点并经过每一个点的最小代价
思路:这题我没看出什么道理,先打了个暴力,结果发现了个相当坑的规律,,然后就过了。
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 200005
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int n, s, t, o;
while(~scanf("%d%d%d", &n, &s, &t)){
//printf("%d %d %d %d ", n, s, t, o);
int ans;
if(s == t){
ans = ;
}
else{
if(s > t){
swap(s, t);
}
if(t - s == ){
if(s != && t != n){
ans = -;
}
else{
ans = ;
}
}
else{
int tt = n -s + ;
int ss = n - t + ;
if(ss < s){
s = ss;
t = tt;
}
int p;
if(s == ){
p = t - s - ;
ans = + p / + (p % );
}
else{
p = t - s - ;
ans = + p / + (p % );
}
}
}
if(s == && t == n && n % == ){
ans -= ;
}
printf("%d\n", ans);
}
/*while(~scanf("%d%d%d", &n, &s, &t)){
memset(vis, 0, sizeof(vis));
vis[s] = true;
vis[t] = true;
ans = INF;
dfs(s, 0, 0);
if(ans == INF){
printf("%d %d %d -1\n", n, s, t);
continue;
}
printf("%d %d %d %d\n", n, s, t, ans);
}*/
}
下面是暴力的
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 200005
int n, s, t;
int ans;
bool vis[MAXN];
using namespace std;
void dfs(int x, int cnt, int cost){
if(cnt == n - ){
if(abs(x - t) == ){
ans = min(ans, cost + );
}
else if(abs(x - t) == ){
ans = min(ans, cost);
}
else{
return;
}
}
int y;
for(int i = ; i <= ; i++){
int p = i == ? : ;
y = x + i;
if(y >= && y <= n && !vis[y]){
vis[y] = true;
dfs(y, cnt + , cost + p);
vis[y] = false;
}
y = x - i;
if(y >= && y <= n && !vis[y]){
vis[y] = true;
dfs(y, cnt + , cost + p);
vis[y] = false;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE while(~scanf("%d%d%d", &n, &s, &t)){
memset(vis, , sizeof(vis));
vis[s] = true;
vis[t] = true;
ans = INF;
dfs(s, , );
if(ans == INF){
printf("-1\n");
return ;
}
printf("%d\n", ans);
}
}
Gym - 100637B Lunch 规律的更多相关文章
- CF Gym 100637B Lunch(拆分子问题)
题意:在一条狭窄的沼泽上有一列数量为n的连续荷叶,编号为1-n.有一只蛤,在边s号荷叶上,其他荷叶上苍蝇,哈可以跳到相邻的荷叶上,或者越过一片荷叶,跳完以后原来的荷叶会沉,目标是f荷叶,在跳到f荷叶之 ...
- Codeforces Gym 100637B B. Lunch 找规律
B. Lunch Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/B Des ...
- Gym - 101911C Bacteria (规律题)
传送门:点我 Time limit2000 ms Memory limit262144 kB Recently Monocarp has created his own mini-laboratory ...
- Gym 101334J 找规律
题意: 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + - + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, 3)=3 mo ...
- Codeforces Gym 100015A Another Rock-Paper-Scissors Problem 找规律
Another Rock-Paper-Scissors Problem 题目连接: http://codeforces.com/gym/100015/attachments Description S ...
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- Gym 101981G - Pyramid - [打表找规律][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem G]
题目链接:http://codeforces.com/gym/101981/attachments The use of the triangle in the New Age practices s ...
- Codeforces Gym 100015B Ball Painting 找规律
Ball Painting 题目连接: http://codeforces.com/gym/100015/attachments Description There are 2N white ball ...
- codeforces Gym 100418D BOPC 打表找规律,求逆元
BOPCTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
随机推荐
- 【Paper Reading】Improved Textured Networks: Maximizing quality and diversity in Feed-Forward Stylization and Texture Synthesis
Improved Textured Networks: Maximizing quality and diversity in Feed-Forward Stylization and Texture ...
- 单机Mongo复制集安装配置(数据库版本:4.x)
官方文档: https://docs.mongodb.com/manual/tutorial/deploy-replica-set-with-keyfile-access-control/#dep ...
- Linux ping 不通 域名 添加DNS
修改路由配置文件 vi /etc/resolv.conf # Generated by NetworkManager #NDS nameserver 192.168.32.2 redhat7 系统优化 ...
- Vue组件开发 -- Markdown
利用marked 和 highlight.js开发markdown组件 实现效果图如下: markdown组件已这种形式<Markdown v-model="markdown" ...
- 紫书 例题11-9 UVa 1658 (拆点+最小费用流)
这道题要求每个节点只能经过一次,也就是结点容量为1, 要拆点, 拆成两个点, 中间连一条弧容量为1, 费用为0. 因为拆成两个点, 所以要经过原图中的这个节点就要经过拆成的这两个点, 又因为这两个点的 ...
- weak和alias
一.强符号和弱符号 在C语言中,如果多个模块定义同名全局符号时,链接器认为函数和已初始化的全局变量(包括显示初始化为0)是强符号,未初始化的全局变量是弱符号. 根据这个定义,Linux链接器使用下面的 ...
- dashboard安装
1,安装程序包 # yum install -y openstack-dashboard 2,修改配置文件 # vim /etc/openstack-dashboard/local_settings ...
- Json学习总结(2)——Java 下的 JSON库性能比较:JSON.simple vs. GSON vs. Jackson vs. JSONP
JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上它 ...
- POJ 1975 Median Weight Bead
Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Orig ...
- cogs 141. [USACO Jan08] 奶牛的选举
141. [USACO Jan08] 奶牛的选举 ★ 输入文件:elect.in 输出文件:elect.out 简单对比时间限制:1 s 内存限制:16 MB 在推翻了Farmer J ...