本文整理汇总了C++中priority_queue::size方法的典型用法代码示例。如果您正苦于以下问题:C++ priority_queue::size方法的具体用法?C++ priority_queue::size怎么用?C++ priority_queue::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类priority_queue
的用法示例。
在下文中一共展示了priority_queue::size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%lld",&w[i]);
while((n-1)%(k-1)!=0)n++;
for(int i=1;i<=n;i++)q.push(make_pair(w[i],0));
LL ans1=0,ans2=0;
while(q.size()>1){
LL res=0,dep=0;
for(int i=0;i<k;i++){
res+=q.top().first;
dep=max(q.top().second,dep);
q.pop();
}
ans1+=res;
ans2=max(ans2,dep+1);
q.push(make_pair(res,dep+1));
}
cout<<ans1<<endl;
cout<<ans2<<endl;
return 0;
}
示例2: main
int main()
{
//freopen ("1msched.txt","r",stdin);
freopen ("msched.in","r",stdin);
freopen ("msched.out","w",stdout);
int n, i, j, k;
PII p;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> j >> k;
v.pb (make_pair(k,-j));
}
sort(v.begin(),v.end());
for (i = 0; i < n; i++)
{
p = v[i];
k = p.x;
q.push(p.y);
while(!q.empty()&&q.size() > k)
q.pop();
}
int ans = 0;
while(!q.empty()) {
ans -= q.top();
q.pop();
}
cout << ans << endl;
return 0;
}
示例3: main
int main(){
int n ;
cin >> n;
int A[n],B[n];
for(int i = 0 ; i< n;i++)
cin >> A[i];
for(int i = 0 ; i< n;i++)
cin >> B[i];
sort(A,A+n);
sort(B,B+n);
for(int i = 0 ; i < n;i++)
heap.push(A[i]+B[0]);
for(int i = 1;i<n;i++){
for(int j = 0 ; j< i;j++)
{
int sum = B[i]+A[j];
if(heap.size()<n)heap.push(sum);
else{
if(sum< heap.top()){
heap.pop();
heap.push(sum);
}
}
}
}
deque<int> c;
while(!heap.empty()){
c.push_front(heap.top());
heap.pop();
}
for(int i = 0 ; i < c.size();i++)
cout<<c[i]<<" ";
cout<<endl;
}
示例4: build
void build(char* Table) {
for (int i = 0; i < 26; ++ i) {
if (Table[i] > 0) {
Node* node = new Node();
node->freq = Table[i];
node->val =(char) (i + 'A');
myQueue.push(node);
}
}
while (myQueue.size() > 1) {
Node* first = myQueue.top();
myQueue.pop();
Node* second = myQueue.top();
myQueue.pop();
Node* tmp = new Node();
tmp->val = '*';
tmp->freq = first->freq + second->freq;
tmp->left = first;
tmp->right = second;
myQueue.push(tmp);
}
root=myQueue.top();
}
示例5: main
int main(){
scanf("%d",&N);
for(int i=0; i<N; i++){
scanf("%d",&a);
pq.push( -a );
}
int cnt = 0;
while( !pq.empty() ){
if( pq.size() == 1 ){
pq.pop(); cnt ++;
break;
}
int a = pq.top(); pq.pop();
if( pq.top() == a ){
pq.pop();
pq.push( a-1 );
}else{
cnt ++;
}
}
printf("%d",cnt);
return 0;
}
示例6: main
int main(){
while(scanf("%d",&n)&&n){
ans=0;
while(!Q.empty())
Q.pop();
int tmp;
for(int i=0;i<n;i++){
scanf("%d",&tmp);
Q.push(tmp);
}
while(Q.size()!=1){
int a=Q.top();
Q.pop();
int b=Q.top();
Q.pop();
a+=b;
ans+=a;
Q.push(a);
}
printf("%d\n",ans);
}
return 0;
}
示例7: process
void process()
{
PostedEvent *event;
EventData *data;
unsigned short processed = 0;
while (posted_events.size() > 0 && processed < process_max)
{
event = posted_events.top();
data = &signals[event->name];
data->signal(event->args);
if (data->deallocator)
{
data->deallocator(event->args);
}
delete event;
posted_events.pop();
processed++;
}
}
示例8: while
//Function builds huffman tree and returns the root node
shared_ptr<HuffmanNode> HuffmanTree::buildTree(priority_queue<shared_ptr<HuffmanNode>,vector<shared_ptr<HuffmanNode>>,HuffmanComparator> &priorityQueue)
{
shared_ptr<HuffmanNode> newParentNode = nullptr;
//Loops until all nodes linked together and all thats left is the root node
while (priorityQueue.size() > 1){
//Get the smallest frequency letter as a Huffman Node - this will be the left node of the new parent
shared_ptr<HuffmanNode> leftOfNewParent = priorityQueue.top();
priorityQueue.pop();
//Get the second smallest frequency letter - right side of parent
shared_ptr<HuffmanNode> rightOfNewParent = priorityQueue.top();
priorityQueue.pop();
//Frequency of parent is the summation of the frequency of its children
int parentFrequency = (*leftOfNewParent).getFrequency() + (*rightOfNewParent).getFrequency();
newParentNode.reset(new HuffmanNode('\0',parentFrequency)); //Empty char and summed frequency
newParentNode->left = leftOfNewParent; //Set pointers from parent to the children nodes so they don't get lost(out of scope)
newParentNode->right = rightOfNewParent;
priorityQueue.push(newParentNode); //Push new parent node onto priority queue to be possibly merged again
}
return newParentNode; //return the last edited node - root node
}
示例9: BuildTree
void BuildTree() //Building Huffman Tree
{
for (int i = 0; i < m; ++ i)
{
Node * node = new Node(); //creating a new node of rxn propensity value
node->prop = Table[i];
node->left=nullptr;
node->right=nullptr;
node->rxno=i+1;
myQueue.push(node); //pushing the contents into priority queue which is min -heap
}
cout << "qUEQUE Top "<<myQueue.top()->prop<<endl;
while (myQueue.size() > 1) //Building Huffman Tree
{
Node * f = myQueue.top();
myQueue.pop();
Node * s = myQueue.top();
myQueue.pop();
Node * tmp = new Node();
tmp->prop = f->prop+ s->prop;
cout<<tmp->prop<<endl;
tmp->left = f;
tmp->right = s;
cout<<"Hello 2"<<endl;
myQueue.push(tmp);
}
cout << "qUEQUE Top"<<myQueue.top()->prop<<endl;
}
示例10: main
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
long long a,b;
while(~scanf("%d",&N))
{
while(!que.empty())
que.pop();
ans=0;
while(N--)
{
scanf("%lld",&a);
que.push(a);
}
while(que.size()>1)
{
a=que.top();
que.pop();
b=que.top();
que.pop();
ans+=a+b;
que.push(a+b);
}
printf("%lld\n",ans);
}
return 0;
}
示例11: main
int main()
{
int a,n,s,t1,t2;
while(cin>>n)
{
if(n==0)break;
for(int i = 0; i < n; i++)
{
cin>>a;
q.push(a);
}
s = 0;
while(q.size()>1)
{
t1 = q.top(); q.pop();
t2 = q.top(); q.pop();
s+=t1+t2;
q.push(t1+t2);
}
cout<<s<<endl;
q.pop();
}
return 0;
}
示例12: main
int main()
{
freopen("huffman.in", "r", stdin);
freopen("huffman.out", "w", stdout);
ios::sync_with_stdio(false);
int n; cin >> n;
for(int i = 0; i < n; i++)
{
int x; cin >> x;
pq.push(-x);
}
lli ans = 0;
while(pq.size() != 1)
{
lli t0 = pq.top();
pq.pop();
lli t1 = pq.top();
pq.pop();
ans -= t0+t1;
pq.push(t0+t1);
}
cout << ans << '\n';
return 0;
}
示例13: main
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
scanf("%d",&n);
int cnt = 0;
while(n -- ){
double ans = 0;
int lval, rval;
int inpt;
scanf("%d",&inpt);
++ cnt;
if(cnt == 1){
ans = inpt;
lg_q.push(inpt);
}else{
if(inpt <= lg_q.top()){
lg_q.push(inpt);
if(lg_q.size() > sm_q.size() + 1){
inpt = lg_q.top();
lg_q.pop();
sm_q.push(inpt);
}
}else{
sm_q.push(inpt);
if(lg_q.size() < sm_q.size()){
inpt = sm_q.top();
sm_q.pop();
lg_q.push(inpt);
}
}
if(lg_q.size() == sm_q.size()){
lval = lg_q.top();
rval = sm_q.top();
ans = ((double)lval + (double)rval)/2.0;
}else{
ans = lg_q.top();
}
}
printf("%.1lf\n",ans);
}
return 0;
}
示例14: main
int main () {
while(scanf("%d", &N) != EOF){
if(MinQ.size() == 0){
MinQ.push(N);
printf("%d\n",N);
}
else if(MaxQ.size() == 0){
MaxQ.push(N);
printf("%d\n",(MaxQ.top() + MinQ.top()) / 2);
if(MaxQ.top() < MinQ.top()) Swap();
}
else if(MinQ.size() == MaxQ.size()){
if(N <= MinQ.top()){
MinQ.push(N);
printf("%d\n",MinQ.top());
}
else{
MaxQ.push(N);
int a = MaxQ.top();
MaxQ.pop();
MinQ.push(a);
printf("%d\n",MinQ.top());
}
}
else if(MinQ.size() - MaxQ.size() == 1){
if(N <= MinQ.top()){
MinQ.push(N);
int a = MinQ.top();
MinQ.pop();
MaxQ.push(a);
printf("%d\n",(MaxQ.top() + MinQ.top()) / 2);
}
else{
MaxQ.push(N);
printf("%d\n",(MaxQ.top() + MinQ.top()) / 2);
}
}
}
return 0;
}
示例15: improve_solution_1
bool improve_solution_1(
graph<pair<int, int>>* solution,
graph<int>& g1,
graph<int>& g2,
unordered_map<int, int>& g2_to_g1_mapping,
unordered_set<int>& mapped_nodes,
unordered_set<int>& unmapped_nodes,
vector<int>& unmapped_nodes_vector,
float neighbourhood_proportion,
bool strict_comparisons,
unsigned int tabu_list_size,
priority_queue<movement, vector<movement>,
movement_compare>& tabu_queue,
movement_compare& mc,
vector<movement>& tabu_list,
bool allow_worse,
const float time_delta,
int& max_edge_diff,
const float aspiration_threshold
) {
vector<pair<int, int>> solution_vertices =
solution->get_vertices();
bool use_tabu = tabu_list_size > 0;
bool is_tabu_mapping = false;
vector<pair<int, int>> tabu_free_mappings;
bool any_improvement = false;
pair<int, int> best_to_remove;
pair<int, int> best_to_add;
int best_edge_diff = 0;
vector<int> best_new_edges;
// This iterates over every pair present in the solution, each of
// which corresponds to a vertex in g1
for (unsigned int i = 0; i < solution->n(); i++) {
// This iterates over the possible nodes in g2 to which the
// current g1 vertex could be associated
unsigned int neighbourhood_size = unmapped_nodes_vector.size();
for (unsigned int j = 0; j < neighbourhood_size; j++) {
if (rand() < neighbourhood_proportion * RAND_MAX) {
// Possible new pair. Let's evaluate its potential
int node_mapped = solution_vertices[i].first; // part of g1
int node_to_remove = solution_vertices[i].second; // part of g2
int node_to_add = unmapped_nodes_vector[j]; // part of g2
int lost_edges = solution->degree({node_mapped, node_to_remove});
vector<int> new_edges;
vector<int> node_to_add_neigh = g2.neighbours(node_to_add);
for (unsigned int k = 0; k < node_to_add_neigh.size(); k++) {
if (node_to_add_neigh[k] != node_to_remove &&
unmapped_nodes.find(node_to_add_neigh[k]) ==
unmapped_nodes.end() &&
g1.adjacent(node_mapped,
g2_to_g1_mapping[node_to_add_neigh[k]])
)
{
new_edges.push_back(node_to_add_neigh[k]);
}
}
int edge_diff = new_edges.size() - lost_edges;
// Check if this movement is tabu
if (use_tabu && allow_worse) {
for (unsigned int k = 0; k < tabu_list.size(); k++) {
// If this movement is in the tabu list
if (tabu_list[k].mapping.first == node_mapped &&
tabu_list[k].mapping.second == node_to_add
) {
// If it doesn't gain much edges, we discard it
if (edge_diff < max_edge_diff*aspiration_threshold) {
is_tabu_mapping = true;
break;
}
}
}
// Try with next potential matching
if (is_tabu_mapping) {
is_tabu_mapping = false;
continue;
}
else {
tabu_free_mappings.push_back({i, j});
}
}
bool is_improvement = strict_comparisons
? edge_diff > best_edge_diff
: edge_diff >= best_edge_diff;
if (is_improvement) {
any_improvement = true;
best_to_add = {node_mapped, node_to_add};
best_to_remove = {node_mapped, node_to_remove};
best_edge_diff = edge_diff;
//.........这里部分代码省略.........