每天一道算法题

考研狗的日常休闲活动。

2019/2/17 

CF 1113A Sasha and His Trip

第一站先把油桶加满,后面每到一站判断油箱内的油能否支持开到最后,不能的话加一升油(花费$i$元)。思路就是尽量在前面站加油。

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <deque>
 5 #include <stack>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <string>
10 #include <cstring>
11 #include <fstream>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 #define eps 1e-8
17 #define pb push_back
18 #define PI acos(-1.0)
19 #define INF 0x3f3f3f3f
20 #define clr(a,b) memset(a,b,sizeof(a)
21 #define bugc(_) cerr << (#_) << " = " << (_) << endl
22 #define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
23 
24 typedef long long ll;
25 typedef unsigned long long ull;
26 
27 int main(){
28     FAST_IO;
29     int n,v,cost=0;
30     cin>>n>>v;
31     if(v>=n-1){
32         cout<<n-1<<endl;
33         return 0;
34     }
35     cout<<v+(2+n-v)*(n-v-1)/2<<endl;
36     return 0;
37 }
View Code

CF 1113B Sasha and Magnetic Machines

两个数中乘上$x$的一定是所有数中最小的,再枚举剩下的数($ai<=100$),每个数再枚举其因子,过程中更新答案。

 1 #include <set>
 2 #include <map>
 3 #include <queue>
 4 #include <deque>
 5 #include <stack>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <string>
10 #include <cstring>
11 #include <fstream>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 #define eps 1e-8
17 #define pb push_back
18 #define PI acos(-1.0)
19 #define INF 0x3f3f3f3f
20 #define clr(a,b) memset(a,b,sizeof(a)
21 #define bugc(_) cerr << (#_) << " = " << (_) << endl
22 #define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
23 
24 typedef long long ll;
25 typedef unsigned long long ull;
26 const int N=5e4+10;
27 int a[N];
28 
29 int main(){
30     FAST_IO;
31     int n,sum=0,ans=INF;
32     cin>>n;
33     for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i];
34     ans=sum;
35     sort(a+1,a+1+n);
36     for(int i=2;i<=n;i++){
37         for(int j=1;j*j<=a[i];j++){
38             if(a[i]%j==0){
39                 ans=min(ans,sum-a[1]-a[i]+a[1]*j+a[i]/j);
40             }
41         }
42     }
43     cout<<ans<<endl;
44     return 0;
45 }
View Code

猜你喜欢

转载自www.cnblogs.com/ehanla/p/10390118.html
今日推荐