- 卖菜
问题描述
在一条街上有n个卖菜的商店,按1至n的顺序排成一排,这些商店都卖一种蔬菜。
第一天,每个商店都自己定了一个价格。店主们希望自己的菜价和其他商店的一致,第二天,每一家商店都会根据他自己和相邻商店的价格调整自己的价格。具体的,每家商店都会将第二天的菜价设置为自己和相邻商店第一天菜价的平均值(用去尾法取整)。
注意,编号为1的商店只有一个相邻的商店2,编号为n的商店只有一个相邻的商店n-1,其他编号为i的商店有两个相邻的商店i-1和i+1。
给定第一天各个商店的菜价,请计算第二天每个商店的菜价。
输入格式
输入的第一行包含一个整数n,表示商店的数量。
第二行包含n个整数,依次表示每个商店第一天的菜价。
输出格式
输出一行,包含n个正整数,依次表示每个商店第二天的菜价。
样例输入
8
4 1 3 1 6 5 17 9
样例输出
2 2 1 3 4 9 10 13
数据规模和约定
对于所有评测用例,2 ≤ n ≤ 1000,第一天每个商店的菜价为不超过10000的正整数。
代码:
#include <iostream>
using namespace std;
const int MAXN = 1010;
int arr[MAXN];
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i ++)
cin >> arr[i];
for(int i = 0; i < n; i ++){
//头尾特殊处理
if(!i){
cout << (arr[0] + arr[1]) / 2 << " ";
}else if(i == n - 1){
cout << (arr[n - 2] + arr[n - 1]) / 2 << " ";
}else{
cout << (arr[i - 1] + arr[i] + arr[i + 1]) / 3 << " ";
}
}
return 0;
}
- 买菜
问题描述
小H和小W来到了一条街上,两人分开买菜,他们买菜的过程可以描述为,去店里买一些菜然后去旁边的一个广场把菜装上车,两人都要买n种菜,所以也都要装n次车。具体的,对于小H来说有n个不相交的时间段[a1,b1],[a2,b2]…[an,bn]在装车,对于小W来说有n个不相交的时间段[c1,d1],[c2,d2]…[cn,dn]在装车。其中,一个时间段[s, t]表示的是从时刻s到时刻t这段时间,时长为t-s。
由于他们是好朋友,他们都在广场上装车的时候会聊天,他们想知道他们可以聊多长时间。
输入格式
输入的第一行包含一个正整数n,表示时间段的数量。
接下来n行每行两个数ai,bi,描述小H的各个装车的时间段。
接下来n行每行两个数ci,di,描述小W的各个装车的时间段。
输出格式
输出一行,一个正整数,表示两人可以聊多长时间。
样例输入
4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14
样例输出
3
数据规模和约定
对于所有的评测用例,1 ≤ n ≤ 2000, ai < bi < ai+1,ci < di < ci+1,对于所有的i(1 ≤ i ≤ n)有,1 ≤ ai, bi, ci, di ≤ 1000000。
代码:
#include <iostream>
using namespace std;
const int MAXN = 2010;
typedef pair<int, int> PII;
PII a[MAXN], b[MAXN];
int fun(int i, int j){
if(a[i].second <= b[j].first || b[j].second <= a[i].first)
return 0;
return min(a[i].second, b[j].second) - max(a[i].first, b[j].first);
}
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i ++)
cin >> a[i].first >> a[i].second;
for(int i = 0; i < n; i ++)
cin >> b[i].first >> b[i].second;
int ans = 0;
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
ans += fun(i, j);
cout << ans;
return 0;
}
- 元素选择器
借鉴一下别人的代码:AcWing 3264. 元素选择器
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <vector>
#include <stack>
using namespace std;
int n, m;
vector<string> strs;
struct Node
{
int tab;
string tag, id;
int k; // 当前节点最多可以匹配到第几个位置
Node(string str)
{
int i = 0;
while (str[i] == '.') i ++ ;
tab = i;
while (i < str.size() && str[i] != ' ')
tag += tolower(str[i ++ ]);
i ++ ; // 过滤掉空格
while (i < str.size()) id += str[i ++ ];
k = 0;
}
bool check(string& word)
{
if (word[0] == '#') return word == id;
return word == tag;
}
};
void work(vector<string>& ws)
{
vector<int> res;
stack<Node> stk;
for (int i = 0; i < strs.size(); i ++ )
{
string str = strs[i];
Node t(str);
while (stk.size() && stk.top().tab >= t.tab) stk.pop();
if (stk.size()) t.k = stk.top().k;
if (t.k == ws.size()) t.k -- ;
if (t.check(ws[t.k]))
{
t.k ++ ;
if (t.k == ws.size()) res.push_back(i + 1);
}
stk.push(t);
}
cout << res.size();
for (auto x: res) cout << ' ' << x;
cout << endl;
}
int main()
{
cin >> n >> m;
getchar();
string str;
while (n -- )
{
getline(cin, str);
strs.push_back(str);
}
while (m -- )
{
getline(cin, str);
stringstream ssin(str);
vector<string> ws;
while (ssin >> str)
{
if (str[0] != '#')
for (auto& c: str)
c = tolower(c);
ws.push_back(str);
}
work(ws);
}
return 0;
}
- 再卖菜
问题描述
在一条街上有n个卖菜的商店,按1至n的顺序排成一排,这些商店都卖一种蔬菜。
第一天,每个商店都自己定了一个正整数的价格。店主们希望自己的菜价和其他商店的一致,第二天,每一家商店都会根据他自己和相邻商店的价格调整自己的价格。具体的,每家商店都会将第二天的菜价设置为自己和相邻商店第一天菜价的平均值(用去尾法取整)。
注意,编号为1的商店只有一个相邻的商店2,编号为n的商店只有一个相邻的商店n-1,其他编号为i的商店有两个相邻的商店i-1和i+1。
给定第二天各个商店的菜价,可能存在不同的符合要求的第一天的菜价,请找到符合要求的第一天菜价中字典序最小的一种。
字典序大小的定义:对于两个不同的价格序列(a1, a2, …, an)和(b1, b2, b3, …, bn),若存在i (i>=1), 使得ai<bi,且对于所有j<i,aj=bj,则认为第一个序列的字典序小于第二个序列。
输入格式
输入的第一行包含一个整数n,表示商店的数量。
第二行包含n个正整数,依次表示每个商店第二天的菜价。
输出格式
输出一行,包含n个正整数,依次表示每个商店第一天的菜价。
样例输入
8
2 2 1 3 4 9 10 13
样例输出
2 2 2 1 6 5 16 10
数据规模和约定
对于30%的评测用例,2<=n<=5,第二天每个商店的菜价为不超过10的正整数;
对于60%的评测用例,2<=n<=20,第二天每个商店的菜价为不超过100的正整数;
对于所有评测用例,2<=n<=300,第二天每个商店的菜价为不超过100的正整数。
请注意,以上都是给的第二天菜价的范围,第一天菜价可能会超过此范围。
代码:
// 80分暴力解法
#include <iostream>
using namespace std;
const int MAXN = 310;
int a[MAXN], b[MAXN], n;
//深搜
bool dfs(int k){
//判断最后一个数字对不对
if(k == n + 1){
if((a[n] + a[n - 1]) / 2 == b[n]){
for(int i = 1; i <= n; i ++)
cout << a[i] << " ";
return true;
}
return false;
}
int j = 3 * b[k - 1] - a[k - 1] - a[k - 2] + 2;
int i = max(1, 3 * b[k - 1] - a[k - 1] - a[k - 2]);
for(; i <= j; i ++){
a[k] = i;
if(dfs(k + 1))
return true;
}
return false;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> b[i];
if(n == 2){
cout << 1 << " " << 2 * b[1] - 1 << endl;
return 0;
}
//先确定好前两个数字
for(int i = 1; i <= 101; i ++){
a[1] = i;
for(int j = 1; j <= 101; j ++){
a[2] = j;
if((i + j) / 2 > b[1])
break;
if((i + j) / 2 == b[1])
if(dfs(3))
return 0;
}
}
return 0;
}
更多历年题解戳这里:ccf-csp 历年真题题解