NC17436. plan
描述
There are n students going to travel. And hotel has two types room:double room and triple room. The price of a double room is p2 and the price of a triple room is p3
Now you need to calulate the minimum total cost of these students.
输入描述
The first line has three integers n, p2, p3
输出描述
Output the minimum total cost.
示例1
输入:
4 2 3
输出:
4
示例2
输入:
5 1 3
输出:
3
C 解法, 执行用时: 3ms, 内存消耗: 404K, 提交时间: 2022-05-15 12:14:55
#include<stdio.h> int main() { long n,p1,p2,s=0,m; scanf("%ld %ld %ld",&n,&p1,&p2); if(p1*3<p2*2) { m=n/2; if(n%2==0) s=p1*m; else s=(m-1)*p1+p2; } else { m=n/3; if(n%3==0) s=p2*m; else if(n%3==1) s=(m-1)*p2+2*p1; else s=p2*m+p1; } printf("%ld",s); }
C++14(g++5.4) 解法, 执行用时: 849ms, 内存消耗: 600K, 提交时间: 2018-08-02 12:04:21
#include<bits/stdc++.h> using namespace std; long long n,p2,p3; int main() { cin>>n>>p2>>p3; long long ans=(1LL<<62); for(int i=0;i*2<=(n+1);++i) { long long thr=(n-2*i+2)/3; ans=min(ans,(long long)i*p2+thr*p3); } cout<<ans<<"\n"; return 0; }
Python3 解法, 执行用时: 61ms, 内存消耗: 7020K, 提交时间: 2021-10-05 12:15:40
n, p1, p2 = map(int, input().split()) a = n // 2 * p1 if n % 2 : a -= p1 a += p2 b = n // 3 * p2 if n % 3 == 2 : b += p1 if n % 3 == 1 : b = min(b - p2 + 2 * p1, b + p1) print(min(a, b, (n + 1) // 2 * p1, (n + 2) // 3 * p2))
C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 484K, 提交时间: 2018-11-22 15:18:35
#include<bits/stdc++.h> using namespace std; int main() { long long n,a,b; cin>>n>>a>>b; cout<<min(min((n+1)/2*a,(n-2)/2*a+b),min(min((n+2)/3*b,n/3*b+a),(n-2)/3*b+a+a)); }
pypy3 解法, 执行用时: 120ms, 内存消耗: 49500K, 提交时间: 2021-10-05 12:21:23
n,p2,p3=map(int,input().split()) def f(n): return 0 if n == 0 else min((n+1)//2*p2,(n+2)//3*p3) print(min(f(n),f(n-3)+p3,f(n-2)+p2,f(n-4)+2*p2))