题目:
Total Submission(s): 20862 Accepted Submission(s): 8629
Billboard
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20862 Accepted Submission(s): 8629
Problem Description
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in the dining room menu, and other important information.
On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).
Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
On September 1, the billboard was empty. One by one, the announcements started being put on the billboard.
Each announcement is a stripe of paper of unit height. More specifically, the i-th announcement is a rectangle of size 1 * wi.
When someone puts a new announcement on the billboard, she would always choose the topmost possible position for the announcement. Among all possible topmost positions she would always choose the leftmost one.
If there is no valid location for a new announcement, it is not put on the billboard (that's why some programming contests have no participants from this university).
Given the sizes of the billboard and the announcements, your task is to find the numbers of rows in which the announcements are placed.
Input
There are multiple cases (no more than 40 cases).
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
The first line of the input file contains three integer numbers, h, w, and n (1 <= h,w <= 10^9; 1 <= n <= 200,000) - the dimensions of the billboard and the number of announcements.
Each of the next n lines contains an integer number wi (1 <= wi <= 10^9) - the width of i-th announcement.
Output
For each announcement (in the order they are given in the input file) output one number - the number of the row in which this announcement is placed. Rows are numbered from 1 to h, starting with the top row. If an announcement can't be put on the billboard, output "-1" for this announcement.
Sample Input
3 5 5 2 4 3 3 3
Sample Output
1 2 1 3 -1
分析:
h行w列的海报墙贴海报 总共n个海报 每个海报1*m,自顶向下,自左向右选择空位贴
输出每个海报贴在第几行 如果没地方贴,输出-1
将每行看做一个节点,用线段树维护区间最大空位数
代码:
没用模板手敲了一发调了好久。。。mdzz
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<ctype.h> //tower()
#include<set>
#include<map>
#include<iomanip>// cout<<setprecision(1)<<fixed<<a;
#include<vector>
#include<time.h>
#include<assert.h> //assert
#include<cmath>
#include<algorithm>
#include<bitset>
#include<limits.h>
#include<stack>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=200090;
int stree[maxn<<2],h,w,n,t;
void built(int rt,int l,int r){
if(l==r){
stree[rt]=w;//一开始每行都有w个空位
return;
}
int mid=(l+r)>>1;
built(rt<<1,l,mid);
built(rt<<1|1,mid+1,r);
stree[rt]=max(stree[rt<<1],stree[rt<<1|1]);
}
int query(int rt,int l,int r,int t){
if(stree[rt]<t) return -1;
if(l==r){
stree[rt]-=t;//rt not l
// stree[rt]=max(stree[rt<<1],stree[rt<<1|1]); //回到上一层pushup一下就可以了
return l;
}
int mid=(l+r)>>1,ans;
if(stree[rt<<1]>=t) ans=query(rt<<1,l,mid,t);
else ans=query(rt<<1|1,mid+1,r,t);
stree[rt]=max(stree[rt<<1],stree[rt<<1|1]);
return ans;//mdzz收到了递归得到的值还要返回给调用方
}
int main(){//3853MS 3628K
while(~scanf("%d%d%d",&h,&w,&n)){
if(h>n) h=n;
built(1,1,h);//h not n
while(n--){
scanf("%d",&t);
printf("%d\n",query(1,1,h,t));
}
}
return 0;
}
使用scanf+printf可以在4S之内跑出来,快的话可以3s之内
若使用 cin+cout+
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
至少需要5.5S.....
可见还是scanf+printf更快一些......