试题编号: | 201809-4 |
---|---|
试题名称: | 再卖菜 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
在一条街上有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的正整数。
请注意,以上都是给的第二天菜价的范围,第一天菜价可能会超过此范围。
这里的关键是差分约束系统同时存在上下界的处理
如: a <= x1 - x2 <= b
转化为 x1 - x2 >= a 以及 x2 - x1 >= -b (这里变号成为下界)
差分约束入门:点击这里
#include
#include
#include
#include
using namespace std;
const int maxn = 300 + 5;
typedef pair<int, int> Edge;
vector<Edge> edge[maxn];
int n, dis[maxn], inq[maxn], a[maxn]; // dis[i]存储的是从原点 0 到 i 之间的和
void spfa(int s) {
memset(dis, 0, sizeof(dis));
memset(inq, 0, sizeof(inq));
queue<int> q;
q.push(s); dis[s] = 0; inq[s] = 1;
while (q.size())
{
int u = q.front(); q.pop(); inq[u] = 0;
for (int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i].first, d = edge[u][i].second;
if (dis[v] < dis[u] + d) {
dis[v] = dis[u] + d;
if (!inq[v]) {
inq[v] = 1;
q.push(v);
}
}
}
}
for (int i = 1; i <= n; i++) {
cout << dis[i] - dis[i - 1] << " ";
}
cout << endl;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
// 每条边都要把 上界 转为负数求 下界
// 最左和最右都要特殊处理
edge[0].push_back(Edge(2, a[1] * 2)); // 可以理解为 dis[2] - dis[0] >= a[1] * 2
edge[2].push_back(Edge(0, -(a[1] * 2 + 1))); // 可以理解为 dis[0] - dis[2] >= - (a[1] * 2 + 1)
edge[n - 2].push_back(Edge(n, a[n] * 2));
edge[n].push_back(Edge(n - 2, -(a[n] * 2 + 1)));
for (int i = 2; i < n; i++) {
edge[i - 2].push_back(Edge(i + 1, a[i] * 3));
edge[i + 1].push_back(Edge(i - 2, -(a[i] * 3 + 2)));
}
for (int i = 1; i <= n; i++) edge[i - 1].push_back(Edge(i, 1)); // 每个店铺都至少售出一件
spfa(0);
return 0;
}
#include
#include
using namespace std;
const int maxn = 305;
int n, a[maxn], b[maxn], vis[maxn][maxn][maxn];
void dfs(int index) {
if (vis[index][b[index - 1]][b[index - 2]]) return;
vis[index][b[index - 1]][b[index - 2]] = 1; // 记录是否访问过
int maybe = a[index - 1] * 3 - b[index - 1] - b[index - 2];
if (index == n) {
for (int i = 0; i < 3; i++) {
if (maybe + i > 0) {
b[n] = maybe + i;
if ((b[n] + b[n - 1]) / 2 == a[n]) {
for (int j = 1; j <= n; j++) cout << b[j] << " ";
exit(0);
}
}
}
return;
}
for (int i = 0; i < 3; i++) {
if (maybe + i > 0) {
b[index] = maybe + i;
dfs(index + 1);
}
}
}
int main(){
memset(vis, 0, sizeof(vis));
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= 2 * a[1]; i++) { // 注意这里要有 = , 因为b[2] = 1 时仍成立,只需要在最后加个判断
b[1] = i, b[2] = a[1] * 2 - i;
for (int j = 0; j < 2; j++) {
b[2] += j;
if ((b[1] + b[2]) / 2 == a[1]) dfs(3); // 判断
}
}
return 0;
}