Find Amir CodeForces - 805C (贪心+思维)
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
2
0
10
4
Note
In the first example we can buy a ticket between the schools that costs .
思路:只需要贪心的这样走,就一定可以花费最小,即。

这样我们可以得出公式,ans=(N+1)/2-1
我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#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 gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ int main()
{
ll n;
cin>>n;
ll ans=(n+)/-;
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 - '';
}
}
}
Find Amir CodeForces - 805C (贪心+思维)的更多相关文章
- Find Amir CodeForces 805C
http://codeforces.com/contest/805/problem/C 题意:有n个学校,学校的编号是从1到n,从学校i到学校j的花费是(i+j)%(n+1),让你求遍历完所有学校的最 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- T - Posterized(贪心思维)
Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...
- codeforces 233 C. Cycles(贪心+思维)
题目链接:http://codeforces.com/contest/233/problem/C 题意:在一个无相图中有N个长度为3 的回路,输出符合条件的图.注意此图的节点数不得超过100 题解:贪 ...
- codeforces 798 D. Mike and distribution(贪心+思维)
题目链接:http://codeforces.com/contest/798/problem/D 题意:给出两串长度为n的数组a,b,然后要求长度小于等于n/2+1的p数组是的以p为下表a1-ap的和 ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
随机推荐
- ScheduledThreadPoolExecutor源码解读
1. 背景 在之前的博文--ThreadPoolExecutor源码解读已经对ThreadPoolExecutor的实现原理与源码进行了分析.ScheduledExecutorService也是我们在 ...
- Java面试——微服务
1.什么是微服务? 就目前而言,对于微服务业界并没有一个统一的,标准的定义. 但通常而言,微服务架构是一种架构模式或者说是一种架构风格,它提倡将单一应用程序划分一组小的服务,每个服务运行在其独立 ...
- 方程:方程(equation)是指含有未知数的等式
方程(equation)是指含有未知数的等式.是表示两个数学式(如两个数.函数.量.运算)之间相等关系的一种等式,使等式成立的未知数的值称为“解”或“根”.求方程的解的过程称为“解方程”. 方程中文一 ...
- UVA10129-Play on Words(欧拉路径)
Problem UVA10129-Play on Words Accept: 2534 Submit: 19477 Time Limit: 3000 mSec Problem Description ...
- 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165318
2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 20165318 下载地址 Kali官网,选择Kali Linux 64 bit VMware 安装步骤 以下步 ...
- Java关于ReentrantLock获取锁和释放锁源码跟踪
通过对ReentrantLock获取锁和释放锁源码跟踪主要想进一步深入学习AQS. 备注:AQS中的waitStatus状态码含义:
- 常用类(Date,Calendar,Math,枚举)
1.日期时间类 计算机时间戳是指距离历元(1970-01-01 00:00:00:000)的时间间隔(以毫秒ms为单位). 如:计算机时间2019-04-29 14:14:00是该时间距离历元经过的毫 ...
- activemq5.14+zookeeper3.4.9实现高可用
一.activeMQ主要的几类部署方式比较1.默认的单机部署(kahadb)activeMQ的默认存储的单机方式,以本地kahadb文件的方式存储,所以性能指标完全依赖本地磁盘IO,不能提供高可用. ...
- mysql innodb引擎 一次线上死锁分析排查步骤
我们的线上erp系统一天使用人员反映部分数据死活保存不上而且页面操作很慢.开始以为操作数据量大的原因, 后来查看了我们线上的glowroot系统,发现slowtrace中有超长时间的访问,点开查看详情 ...
- MySQL 基础十 性能优化
1.优化sql 2.建立索引 3.优化表结构,避免多个join查询