Luogu P1631 sequence merger
First look at the subject, the two lengths are required \ (N \) sequence \ (A \) and \ (B \) , in \ (A \) and \ (B \) from each of a number of phases of the added together to obtain \ (N ^ 2 \) months and, this \ (N ^ 2 \) th and the smallest \ (N \) a.
See output from small to big, think of this possibility by the priority queue and enumeration resolved, begin to try
The easiest way is to have each and violence to go into, but will properly properly timed out
The next two optimization methods
The first: selectively go into
It is easy to think of these two series become
\(B_1+A_1,B_1+A_2,B_1+A_3,B_1+A_4 ... B_1+A_{n-1},B_1+A_n\)
\(B_2+A_1,B_2+A_2,B_2+A_3,B_2+A_4 ... B_2+A_{n-1},B_2+A_n\)
......
\(B_n+A_1,B_n+A_2,B_n+A_3,B_n+A_4 ... B_n+A_{n-1},B_n+A_n\)
Such a sequence of n, because the topic that \ (A_i \ A_ Leq. 1} + {I \) \ (B_i \ Leq B_. 1} + {I \) , guarantees the monotonicity sequence
It might be a first sequence number of each stack into a first
Thus as long as the number n of the i-th sequence into the stack after the first output number i + 1
Complexity is \ (O (n \ log \ n) \) level, may be too
\(code:\)
#include<bits/stdc++.h>
using namespace std;
inline int redn() { // 快读
int ret = 0;
char ch = getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') {ret = ret*10+ch-'0';ch=getchar();}
return ret;
}
int n,cnt(0);
int a[(int)1E5+7],b[(int)1E5+7];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q; // 懒人STL
int main() {
n = redn();
for(int i=1;i<=n;++i) a[i] = redn();
for(int i=1;i<=n;++i) b[i] = redn(),q.push(make_pair(a[1]+b[i],1));
while(++cnt<=n) {
printf("%d ",q.top().first);
int cur = q.top().second;
int pls = q.top().first;
q.pop();
if(cur+1<=n) q.push(make_pair(pls-a[cur]+a[cur+1],cur+1));
}
return 0;
}
The second: to reduce the number of enumeration
Very violent
The above sequence has become
\(B_1+A_1,B_1+A_2,B_1+A_3,B_1+A_4 ... B_1+A_{n-1},B_1+A_n\)
\(B_2+A_1,B_2+A_2,B_2+A_3,B_2+A_4 ... B_2+A_{n-1},B_2+A_n\)
......
\(B_n+A_1,B_n+A_2,B_n+A_3,B_n+A_4 ... B_n+A_{n-1},B_n+A_n\)
So we have to think about
For each \ (B_i \) , if the \ (B_ {i-1} \) already before it and \ (K \) a \ (A_i \) and counted, then must satisfy \ (B_i + A_k \ geq B_ {i-1} + a_k \)
Therefore, just before the sequence will begin our {B_i} (n-i + 1 \) \ number into the stack to
The time complexity is probably \ (O (n \ log ^ 2 \ n) \) levels, barely had experienced
\ (code \) will not give up
Function value the smallest Luogu P2085
Description meaning of the questions:
For \ (n-\) functions \ (f_i (x) = a_i \ times x ^ 2 + b_i \ times x + c_i \) each a positive integer (X \) \ function values obtained, the smallest output \ (m \) th
Almost keep up with the title, just the sequence of function value becomes constituted
\ (F_1 (1), F_1 (1), F_1 (1), F_1 (1), ..., F_1 (inf) \)
\ (F_2 (1), F_2 (1), F_2 (1), F_2 (1), ..., F_2 (inf) \)
\ (F_3 (1), F_3 (1), F_3 (1), F_3 (1), ..., F_3 (inf) \)
...
\ (f_n (1), f_n (1), f_n (1), f_n (1), ..., f_n (inf) \)
As the operation can be problems
\(code:\)
#include<bits/stdc++.h>
using namespace std;
inline int redn() { //快读
int ret = 0;
char ch = getchar();
while(ch<'0'||ch>'9') ch = getchar();
while(ch>='0'&&ch<='9') {ret = ret*10+ch-'0';ch = getchar();}
return ret;
}
int Equ[23333][3];
inline int Gtv(int x,int n) { //取值
return Equ[n][0]*x*x+Equ[n][1]*x+Equ[n][2];
}
int n,m;
int t[23333];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
int main() {
n=redn(),m=redn();
for(int i=1;i<=n;++i) {
Equ[i][0]=redn(),Equ[i][1]=redn(),Equ[i][2]=redn();
q.push(make_pair(Gtv(++t[i],i),i));
}
// while(!q.empty()) printf("%d ",q.top()),q.pop();
// return 0;
int s = 0,mi = (int)1e9+7;
while(s<m) {
printf("%d ",q.top().first);
q.push(make_pair(Gtv(++t[q.top().second],q.top().second),q.top().second));
q.pop();
s++;
}
return 0;
}