题目链接
题意
A cardiogram is a polyline with the following corners:
((0;0), (a_1;a_1), (a_1+a_2;a_1-a_2),...,(sum_{i=1}^n{a_i};sum_{i=1}^n{(-1)^{i+1}a_i}))
That is, a cardiogram is fully defined by a sequence of positive integers (a_1, a_2, ..., a_n).
Your task is to paint a cardiogram by given sequence (a_i).
Input
The first line contains integer n (2 ≤ n ≤ 1000). The next line contains the sequence of integers a 1, a 2, ..., a n (1 ≤ a i ≤ 1000). It is guaranteed that the sum of all a i doesn't exceed 1000.
Output
Print (max |y_i - y_j|) lines (where y k is the y coordinate of the k-th point of the polyline), in each line print characters. Each character must equal either « / » (slash), « » (backslash), « » (space). The printed image must be the image of the given polyline. Please study the test samples for better understanding of how to print a cardiogram.
Note that in this problem the checker checks your answer taking spaces into consideration. Do not print any extra characters. Remember that the wrong answer to the first pretest doesn't give you a penalty.
思路
- 我的思路是从上到下一行行处理,然后一行里面按两点之间分块,一块块处理。
- 如果这一块靠左的点的y坐标大于等于现在这一行,而右边这个点的y坐标小于等于现在这一行,那么这一块中有一个要输出符号'',其他输出空格。同理可以推出其他输出'/'的情况,其他情况都是输出空格。
- 通过给的式子,可以知道,每两个点之间的连线的斜率一定是 1 或者 -1,因此可以推出哪一个地方输出'','/', 其他地方都输出空格。
- 题目有说一共有(max |y_i - y_j|) lines,但是!!!我错在没有考虑到(0;0)!!!!卡了一个钟,我才发现啊啊啊,日常忘记初始化,边界情况,日常不会写博弈和难题。
代码
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1005;
int n;
typedef pair<int,int> pa;
pa a[MAXN];
int main(){
// freopen("out.txt","w",stdout);
scanf("%d",&n);
int sumx=0,sumy=0;
int mxy=0,mny=0;
int t;
a[0]=make_pair(0,0);
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
sumx+=t;
if(i&1)
sumy+=t;
else sumy-=t;
a[i]=make_pair(sumx,sumy);
mxy=mxy<sumy?sumy:mxy;
mny=mny>sumy?sumy:mny;
}
for(int i=mxy;i>=mny;i--){
for(int j=0;j<n;j++){
if(a[j].second<=i&&a[j+1].second>=i){
int difY = a[j+1].second-a[j].second;
int difX = a[j+1].first-a[j].first;
for(int z=1;z<=difX;z++){
if(z==(i-a[j].second))
printf("/");
else printf(" ");
}
}
else if(a[j].second>=i&&a[j+1].second<=i){
int difY = a[j].second-a[j+1].second;
int difX = a[j+1].first-a[j].first;
for(int z=0;z<difX;z++){
if(z==(a[j].second-i))
printf("\");
else printf(" ");
}
}
else{
for(int z=a[j].first;z<a[j+1].first;z++) printf(" ");
}
}
printf("
");
}
return 0;
}