Problem - A - Codeforces
题目大意:有三种尺寸的披萨,6,8,10,每种披萨分别需要花15, 20, 25块钱,有n个小孩,确保每个小孩至少都能有一块披萨吃,请问至少需要花多少钱.
思路:思维题,分类讨论即可.我们可以发现对于6,8,10所有尺寸的披萨,他们的价格都是5/2每块.当n<=6的时候,都至少需要一大小为6的披萨,当n>6之后,如果n是偶数,那么一定可以由6,8,10组合得到,那么只需要将n*(5/2)就可以,如果是奇数,那么将其+1变成偶数,然后运算即可.
#include<bits/stdc++.h>
using namespace std;
#define rep(x,y,z) for(int x = y; x<=z;++x)
#define dec(x,y,z) for(int x = y; x>=z;--x)
#define INF 0x3f3f3f3f
#define ll long long
#define LOOP ll _; cin>> _; for(ll LP_ = 1; LP_<=_; ++LP_)
#define syncfalse ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
return s*w;
}
ll n;
int main(){
LOOP{
cin>>n;
if (n <= 6){
puts("15");
}else{
ll ans = 0;
if (n %2 == 0){
ans = n/2*5;
}else{
n++;
ans = n/2*5;
}
cout << ans <<endl;
}
}
return 0;
}
Problem - B - Codeforces
题目大意:给定长为W,高为H的平面,现在给定一块木板的左下角的坐标和右上角的坐标,请你将其平移尽可能少的距离,使得该平面下可以放的下第二块木板.
思路:思维题,如果第二块木板放得下,那么必须第一块木板的长加上第二块木板的长小于平面的长,或者是宽满足对应关系也可以.那么我们就可以知道,最小的平移距离就是左右移动和上下移动,然后只需要模拟一下即可.
#include<bits/stdc++.h>
using namespace std;
#define rep(x,y,z) for(int x = y; x<=z;++x)
#define dec(x,y,z) for(int x = y; x>=z;--x)
#define INF 0x3f3f3f3f
#define ll long long
#define LOOP ll _; cin>> _; for(ll LP_ = 1; LP_<=_; ++LP_)
#define syncfalse ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
return s*w;
}
double w, h;
double lx, ly, rx, ry, tw, th;
int main(){
LOOP{
cin>>w>>h;
cin>>lx>>ly>>rx>>ry>>tw>>th;
int high = ry-ly, width = rx-lx;
if (high + th > h && width + tw > w){
puts("-1");
continue;
}else{
double distance = INF;
ll yuw = max(lx, w-rx), yuh = max(ly, h-ry);
if (yuw < tw && width + tw <= w){
distance = min(distance, tw - yuw);
}
if (yuh <th && high + th <= h){
distance = min(distance, th - yuh);
}
if (th <= yuh || tw <= yuw){
distance = 0;
}
printf("%.6lf\n", distance);
}
}
return 0;
}
Problem - C - Codeforces
题目大意:给定一个2*n的地图,Alice和Bob从(1,1)处出发,需要到达(2,n)的位置,总共可以有两种移动方式,向下移动或者向右移动,每个方格上有一定的数值,Alice走过这些方格之后,数值就会被清零.现在Alice先走一次,然后Bob再走一次,请问Alice如何走才能使得Bob的得分尽可能地小.(Bob也会采取最优策略)
思路:思维+暴力,因为每个人都只有一次向下走的机会,那我们枚举Alice向下走的位置i,对于Bob而言,最优的策略只由两种,一种是从一开始就走下面一条路,这样他的的得分就是下面一条路的前i个,或者一开始就走上面,这样的话他的得分就是(i+1,n)这些方格的数值,枚举取最优即可.
#include<bits/stdc++.h>
using namespace std;
#define rep(x,y,z) for(int x = y; x<=z;++x)
#define dec(x,y,z) for(int x = y; x>=z;--x)
#define INF 0x3f3f3f3f
#define ll long long
#define LOOP ll _; cin>> _; for(ll LP_ = 1; LP_<=_; ++LP_)
#define syncfalse ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int MAX = 1e5+5;
int a[MAX][2];
int pre[MAX][2];
int dow[MAX][2];
int m;
int main(){
LOOP{
cin>>m;
rep(i,1,m)cin>>a[i][0];
rep(i,1,m)cin>>a[i][1];
pre[0][0] = pre[0][1] = 0;
dow[m+1][0] = dow[m+1][1] =0;
rep(i,1,m){
pre[i][0] = pre[i-1][0] + a[i][0];
pre[i][1] = pre[i-1][1] + a[i][1];
}
dec(i,m,1){
dow[i][0] = dow[i+1][0] + a[i][0];
dow[i][1] = dow[i+1][1] + a[i][1];
}
int ans = INF;
rep(i,1,m){
ans = min(ans, max(pre[i-1][1], dow[i+1][0]));
}
cout << ans << endl;
}
return 0;
}
Problem - D - Codeforces
题目大意:给定一个仅由abc组成的字符串s,现在对它取任意子串,对于取出的子串,你可以通过将任意个字符转换成别的字符,请求出最小的转换花费.
思路:思维题,其实经过一点初步的思考就会发现,合法的字符串只有6种,那就是abc的全排列,然后不断地填充,对于这道题,因为输入量很大,所以我们做一个前缀和来优化,用pre[6][MAX]来表示对于6种不同的字符串所需要的花费,然后枚举最优即可.
#include<bits/stdc++.h>
using namespace std;
#define rep(x,y,z) for(int x = y; x<=z;++x)
#define dec(x,y,z) for(int x = y; x>=z;--x)
#define INF 0x3f3f3f3f
#define ll long long
#define LOOP ll _; cin>> _; for(ll LP_ = 1; LP_<=_; ++LP_)
#define syncfalse ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
return s*w;
}
int n, m;
int l, r;
const int MAX = 2e5+5;
char s[MAX];
string tar="abc";
int pre[6][MAX];
int main(){
syncfalse
cin>>n>>m;
cin>>s+1;
int k = 0;
do{
int index = 0;
rep(i,1,n){
pre[k][i] = pre[k][i-1];
if (s[i] != tar[index%3]){
pre[k][i]++;
}
index++;
}
k++;
}while(next_permutation(tar.begin(), tar.end()));
rep(i,1,m){
cin>>l>>r;
int ans = INF;
rep(i,0,5){
ans = min(ans, pre[i][r] - pre[i][l-1]);
}
cout << ans << endl;
}
return 0;
}
342




被折叠的 条评论
为什么被折叠?



