Codeforces_845

A.排序,比较中间两个大小。

#include<bits/stdc++.h>
using namespace std;

int n,a[205];

int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1;i <= 2*n;i++) cin >> a[i];
    sort(a+1,a+1+n*2);
    reverse(a+1,a+1+n*2);
    if(a[n] > a[n+1])   cout << "YES" << endl;
    else    cout << "NO" << endl;
    return 0;
}
View Code

B.优先改变价值大的位置。

#include<bits/stdc++.h>
using namespace std;

int n,sub[6],add[6];
string s;

int main()
{
    ios::sync_with_stdio(0);
    cin >> s;
    int sum1 = 0,sum2 = 0;
    for(int i = 0;i < 3;i++)
    {
        sum1 += s[i]-'0';
        sub[i] = s[i]-'0';
        add[i] = '9'-s[i];
    }
    for(int i = 3;i < 6;i++)
    {
        sum2 += s[i]-'0';
        sub[i] = s[i]-'0';
        add[i] = '9'-s[i];
    }
    if(sum1 > sum2)
    {
        int t = sum1-sum2;
        int a[6];
        for(int i = 0;i < 3;i++)    a[i] = sub[i];
        for(int i = 3;i < 6;i++)    a[i] = add[i];
        sort(a,a+6);
        reverse(a,a+6);
        for(int i = 0;i < 6;i++)
        {
            t -= a[i];
            if(t <= 0)
            {
                cout << i+1 << endl;
                return 0;
            }
        }
    }
    else if(sum2 > sum1)
    {
        int t = sum2-sum1;
        int a[6];
        for(int i = 0;i < 3;i++)    a[i] = add[i];
        for(int i = 3;i < 6;i++)    a[i] = sub[i];
        sort(a,a+6);
        reverse(a,a+6);
        for(int i = 0;i < 6;i++)
        {
            t -= a[i];
            if(t <= 0)
            {
                cout << i+1 << endl;
                return 0;
            }
        }
    }
    else    cout << 0 << endl;
    return 0;
}
View Code

C.按l排序,优先队列模拟,处理出最小需要的TV数量。

#include<bits/stdc++.h>
using namespace std;

int n;
struct xx
{
    int l,r;
    friend bool operator<(xx a,xx b)
    {
        return a.l < b.l;
    }
}a[200005];

int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1;i <= n;i++)   cin >> a[i].l >> a[i].r;
    sort(a+1,a+1+n);
    int ans = 0;
    priority_queue< int,vector<int>,greater<int> > q;
    for(int i = 1;i <= n;i++)
    {
        if(q.empty() || a[i].l <= q.top())
        {
            ans++;
            q.push(a[i].r);
        }
        else
        {
            q.pop();
            q.push(a[i].r);
        }
    }
    if(ans > 2) cout << "NO" << endl;
    else    cout << "YES" << endl;
    return 0;
}
View Code

D.栈模拟。

#include<bits/stdc++.h>
using namespace std;

int n;

int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    stack<int> s;
    int ans = 0,now,cntover = 0;
    while(n--)
    {
        int x,y;
        cin >> x;
        if(x == 1)  cin >> now;
        else if(x == 2)
        {
            ans += cntover;
            cntover = 0;
        }
        else if(x == 3)
        {
            cin >> y;
            s.push(y);
        }
        else if(x == 4) cntover = 0;
        else if(x == 5)
        {
            while(!s.empty())   s.pop();
        }
        else    cntover++;
        while(!s.empty() && now > s.top())
        {
            ans++;
            s.pop();
        }
    }
    cout << ans << endl;
    return 0;
}
View Code

G.dfs,每遇到环可以存起来,最后更新答案。

#include<bits/stdc++.h>
using namespace std;

int n,m,a[100005],vis[100005] = {0};
struct xx
{
    int to,w;
    xx(int a,int b):to(a),w(b){};
};
vector<xx> v[100005];
vector<int> vv;

void add(int x)
{
    for(int i = 0;i < vv.size();i++)    x = min(x,x^vv[i]);
    if(x)   vv.push_back(x);
}

void dfs(int now,int val)
{
    vis[now] = 1;
    a[now] = val;
    for(int i = 0;i < v[now].size();i++)
    {
        int t = v[now][i].to,w = v[now][i].w;
        if(vis[t])  add(val^w^a[t]);
        else    dfs(t,w^val);
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin >> n >> m;
    while(m--)
    {
        int x,y,z;
        cin >> x >> y >> z;
        v[x].push_back(xx(y,z));
        v[y].push_back(xx(x,z));
    }
    dfs(1,0);
    for(int i = 0;i < vv.size();i++)    a[n] = min(a[n],a[n]^vv[i]);
    cout << a[n] << endl;
    return 0;
}
View Code

 

posted @ 2017-08-26 19:34  zzzzzzzzhu  阅读(187)  评论(0编辑  收藏  举报