Noip problem solution Zhenti Collection

table of Contents

Because I'm too lazy Yeah, coupled with the recent daily exam, you write it together. (Long-term update (CSP ago))。

Toy Puzzle

noip2016 improve group day1 t1

topic

Meaning of the questions:

Find toys based on the information.

Difficulty: sign title

Ideas:

Simulation can be.
Code:

#include <iostream>
#include <cstdio>
using namespace std;
//Mystery_Sky
//
#define INF 0x3f3f3f3f
#define ll long long
#define M 200010
inline int read()
{
    int x=0,f=1; char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f; 
} 
struct node{
    string name;
    int turn;
}man[M];
int n, m;

int main() {
//  freopen("miti.in", "r", stdin);
//  freopen("miti.out", "w", stdout);
    n = read(), m = read();
    for(int i = 1; i <= n; i++) {
        man[i].turn = read();
        cin >> man[i].name;
    }
    int id = 1;
    for(int i = 1; i <= m; i++) {
        int t = read(), num = read();
        if(man[id].turn == 0) {
            if(t == 0) {
                id -= num;
                if(id <= 0) id += n;
            }
            else {
                id += num;
                if(id > n) id -= n;
            }
        }
        else {
            if(t == 0) {
                id += num;
                if(id > n) id -= n;
            }
            else {
                id -= num;
                if(id <= 0) id += n;
            }
        }
    }
    cout << man[id].name << endl;
    return 0;
}

earthworm

noip2016 improve group day2 t2

topic

Meaning of the questions:

Article earthworm given n, m next time in seconds per cut the longest ⌊px⌋ earthworms and x-⌊px⌋ (0 <p < 1), the remaining worms grew p, every t seconds the output once cut earthworms original length, and finally outputs the resultant earthworms descending order t, 2t, 3t ...... length earthworms. ps: the length of the cut obtained is also needed to retain earthworms 0

Difficulty: Moderate

Ideas:

[60 minute practice]:

According to the data range, q 12 points to 0, that earthworms will not grow, you can simply use the priority queue all the earthworms into each team's first out, then back in to finish the cut.

Code:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
//Mystery_Sky
//预计->60
#define M 200010
#define INF 0x3f3f3f3f
#define ll long long
inline int read()
{
    int x=0,f=1; char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
int n, m, q, u, v, t;
double p;
int a[M];
struct node{
    int l;
    inline bool operator <(const node& x)const{
        return x.l > l;
    }
};
priority_queue <node> Q1;
priority_queue <node> Q2;
vector <int> ans_1;
vector <int> change;

int main() {
    n = read(), m = read(), q = read(), u = read(), v = read(), t = read();
    p = u * 1.0 / v;
    for(int i = 1; i <= n; i++) a[i] = read(), Q1.push((node){a[i]});
    for(int i = 1; i <= m; i++) {
        node top = Q1.top();Q1.pop();
        int x1 = (int)(top.l * p);
        int x2 = top.l - x1;
        Q1.push((node){x1}); Q1.push((node){x2});  
        if(i % t == 0) ans_1.push_back(top.l); 
    }
    int len = ans_1.size();
    for(int i = 0; i < len; i++) printf("%d ", ans_1[i]);
    putchar(10);
    int tot = 0;
    while(!Q1.empty()) {
        tot++;
        node top = Q1.top(); Q1.pop();
        if(tot % t == 0) {
            printf("%d ", top.l);
        }
    }
    return 0;
}
[70 minute practice]:

When I wanted to test out a practice. Establishing two priority queues, and the structural body of c b, c represents an article by earthworms was last cut in the first seconds c, b = l - c, b and a size of all the keywords are arranged earthworms. First of all earthworms into Q1, after each cut, the length of a stored Q1, Q2 into a short, then takes out a larger value Q1 and Q2 of the first team in a to b cut. For the i earthworm second cut, which, compared with the current length l + (i-1) * q - c * q, and after cutting, the current length of each worm was l + (i - c) * q.

Code:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
//Mystery_Sky
//->70
#define M 200010
#define INF 0x3f3f3f3f
#define ll long long
inline int read()
{
    int x=0,f=1; char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
int n, m, q, u, v, t;
double p;
int a[M];
struct node{
    int l;
    int c;
    int b;
    inline bool operator <(const node& x)const{
        return x.b > b;
    }
};
priority_queue <node> Q1;
priority_queue <node> Q2;
priority_queue <int> Q;
vector <int> ans_1;

int main() {
    n = read(), m = read(), q = read(), u = read(), v = read(), t = read();
    p = u * 1.0 / v;
    for(int i = 1; i <= n; i++) a[i] = read(), Q1.push((node){a[i], 0, a[i]});
    for(int i = 1; i <= m; i++) {
        node top_1;
        if(!Q1.empty()) top_1 = Q1.top(), Q1.pop();
        node top_2;
        top_2.l = -1;
        if(!Q2.empty()) top_2 = Q2.top(), Q2.pop();
        node top;
        if(top_2.l != -1 && top_1.b >= top_2.b) Q2.push(top_2), top = top_1;
        else if(top_2.l == -1) top = top_1;
        else Q1.push(top_1), top = top_2;
        int len = top.l - top.c + (i - 1) * q;
        int x1 = (int) (len * p);
        int x2 = len - x1;
        if(x1 < x2) swap(x1, x2);
        Q1.push((node){x1, i * q, x1-i*q});
        Q2.push((node){x2, i * q, x2-i*q});  
        if(i % t == 0) ans_1.push_back(len); 
    }
    int len = ans_1.size();
    for(int i = 0; i < len; i++) printf("%d ", ans_1[i]);
    putchar(10);
    while(!Q2.empty()) {
        node top = Q2.top(); Q2.pop();
        Q1.push(top); 
    }
    int tot = 0;
    while(!Q1.empty()) {
        tot++;
        node top = Q1.top(); Q1.pop();
        if(tot % t == 0) {
            printf("%d ", top.l - top.c + m * q);
        }
    }
    return 0;
}

(Saying this does not seem to build two priority queues make sense)

[Correct answer]:

We have established three queues , ah, is the normal queue. First of all the earthworms after descending order into Q [0], and wherein after each cut into a Q [1], put an additional Q [2], since the cutting at the same time has led to a first-team queue will always be greater than the length of the earthworm into the team, so these three queue itself is monotonous, cut each time to take the first three teams in the largest cohort of a cut can be. For the growth of the length of q, calculated violence will obviously t, we can add variables add, it indicates growth in length, each time the first cut earthworms length plus add, after the cut, the length of two minus (add + q), re-entry team, add to q plus.

Code:

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define M 200010
#define INF 0x3f3f3f3f
inline int read()
{
    int x=0, f=1; char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
queue <int> Q[3];
vector <int> ans_1;
int n, m, q, u, v, t, add;
double p;
int a[M];
inline bool cmp(int a, int b){return a > b;}
inline int big()
{
    int res = -1;
    for(int i = 0; i <= 2; i++) if(!Q[i].empty() && ((res == -1) || (Q[i].front() > Q[res].front()))) res = i;
    return res;
}

int main() {
    n = read(), m = read(), q = read(), u = read(), v = read(), t = read();
    p = u * 1.0 / v;
    for(int i = 1; i <= n; i++) a[i] = read();
    sort(a+1, a+n+1, cmp);
    for(int i = 1; i <= n; i++) Q[0].push(a[i]);
    for(int i = 1; i <= m; i++) {
        int top = big();
        int x = Q[top].front() + add; Q[top].pop();
        int x1 = (int)(x * p);
        int x2 = x - x1;
        x1 -= (add + q), x2 -= (add + q);
        Q[1].push(x1), Q[2].push(x2);
        if(i % t == 0) ans_1.push_back(x);
        add += q;   
    } 
    for(int i = 0, len = ans_1.size(); i < len; i++) printf("%d ", ans_1[i]);
    putchar(10);
    for(int i = 1; i <= n+m; i++) {
        int top = big();
        int x = Q[top].front(); Q[top].pop();
        if(i % t == 0) printf("%d ", x + add);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Benjamin-cpp/p/11838792.html