NOIP模拟题 2016.11.8 [数学归纳法] [贪心] [二分答案] [Bash模拟]

7 篇文章 0 订阅
3 篇文章 0 订阅

T1:
题意:有n个字符串,每个字符串可以通过无限次翻转区间[1,m]中的字符,但是要保证m为偶数。

通过简单的变换可以把整个字符串左移2位,那么由数学归纳法可以得到左移K位都是可以做到的,只要保证K是偶数。
把相邻的两个奇数位和偶数位看成一个单位,再次变换可以发现交换任意两个相邻单位都是合法的,那么可以推出任意两个单位之间的交换都是合法的。
由于一个单位里的两个字符是可以互换位置的,这样每一个字符串都有一个可以确定的最小字典序表示方法,规定每个单位组成的二元组的大小为以第一个字符为第一关键字,第二个字符为第二关键字来比较大小。
这样随便用个什么东西统计一下就好了,这里数据范围给得很宽裕,就map来处理即可。
最后答案是Sigma(cnt[i]%2).

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
template <class T> inline void read(T &x)
{
    x = 0;
    T flag = 1;
    char ch = (char)getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch == '-') flag = -1;
        ch = (char)getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x = (x<<1) + (x<<3) + ch - '0';
        ch = (char)getchar();
    }
    x *= flag;
}
template <class T> T gcd(T a,T b) { return !b?a:gcd(b,a%b); }
const int INF=0x3f3f3f3f;
const int maxn = 55;
string s;
int n;
map <string,int> mp;
inline void init()
{
    mp.clear();
    read(n);
    for(int k=1;k<=n;k++)
    {
        cin>>s;
        int lens = s.size();
        for(int i=0;(i|1)<lens;i+=2) if(s[i]>s[i|1]) swap(s[i],s[i|1]);
        for(int i=0;(i|1)<lens;i+=2)
            for(int j=0;(j|1)<lens-(i+2);j+=2)
                if(s.substr(j,2)>s.substr(j+2,2))
                    swap(s[j],s[j+2]),swap(s[j|1],s[(j|1)+2]);
        if(mp.count(s)) mp[s]++;
        else mp[s]=1;
    }
}
int work()
{
    int ans = 0;
    for(map <string,int> ::iterator it = mp.begin(); it!=mp.end(); it++)
        ans += it->second % 2;
    return ans;
}
int main()
{
    freopen("kahuucino.in","r",stdin);
    freopen("kahuucino.out","w",stdout);
    int T;
    read(T);
    while(T--)
    {
        init();
        int ans = work();
        printf("%d",ans);
        putchar('\n');
    }
    return 0;
}

T2:
题意:每个油库里都有两个值,战斗力和能量值,规定战斗力大的可以吃掉战斗力小的,吃掉之后新的战斗力为 原战斗力+吃掉的能量值-吃掉的战斗力,求最大能得到的 战斗力+能量值。

规定战斗力小于能量值的为good,其他的为bad,那么对于确定的一个油库里,只有good类别的油库里是有用的,bad一定不吃,吃了浪费战斗力。。
显然应当首先吃战斗力小的,因为吃了小的之后战斗力会增加,更有利于吃战斗力大的good。
那么可以枚举油库里,但是这样是O(n^2)的算法,要GG。
由于是按照战斗力从小到大的顺序去吃good,在某一时刻可能不足以吃掉后面的油库里,所以应当维护一个当前需要的最大战斗力值。
sum[i] = Sigma(Y[i] - X[i])
MAX[i] = max(MAX[i-1] , X[i] - sum[i-1])
前i-1个实际上是给吃第i个提供能量的,所以在这里减去sum[i-1],为了保证二分的确定性。
所以枚举每一个bad中的油库里和good中战斗力最高的油库里,二分确定最大的能够吃掉的油库里即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
template <class T> inline void read(T &x)
{
    x = 0;
    T flag = 1;
    char ch = (char)getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch == '-') flag = -1;
        ch = (char)getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x = (x<<1) + (x<<3) + ch - '0';
        ch = (char)getchar();
    }
    x *= flag;
}
template <class T> T gcd(T a,T b) { return !b?a:gcd(b,a%b); }
const LL INF = (1LL<<60);
const int maxn = 100005;
struct Node
{
    LL x,y;
    Node() {}
    Node (const LL &_x,const LL &_y) { x=_x; y=_y; }
    bool operator < (const Node &t) const
    {
        if(x ^ t.x) return x < t.x;
        return y < t.y;
    }
}good[maxn],bad[maxn];
int goodn,badn;
int n;
LL sum[maxn],MAX[maxn]; // formed as prefix
inline void init()
{
    read(n);
    for(int i=1;i<=n;i++)
    {
        LL x,y;
        read(x); read(y);
        if(x < y) good[++goodn] = Node(x,y);
        else bad[++badn] = Node(x,y);
    }
    sort(good+1,good+goodn+1);
    MAX[0]=0; sum[0]=0;
    for(int i=1;i<=goodn;i++)
    {
        sum[i] = sum[i-1] + good[i].y - good[i].x;
        MAX[i] = max(MAX[i-1],good[i].x - sum[i-1]);
    }
}
inline int search(int L,int R,LL x)
{
    while(L < R)
    {
        int mid = (L+R+1)>>1;
        if(MAX[mid] < x) L = mid;
        else R = mid - 1;
    }
    return L;
}
LL work()
{
    LL ans = 0;
    smax(ans,sum[search(0,goodn-1,good[goodn].x)]+good[goodn].x+good[goodn].y);
    for(int i=1;i<=badn;i++)
    {
        int pos = search(0,goodn,bad[i].x);
        smax(ans,sum[pos]+bad[i].x+bad[i].y);
    }
    return ans;
}
int main()
{
    freopen("zyougamaya.in","r",stdin);
    freopen("zyougamaya.out","w",stdout);
    init();
    LL ans = work();
    printf(AUTO,ans);
    return 0;
}

T3:
题意:实现一个Bash模拟器,支持文件和文件夹创建删除操作,并且可以查看当前目录内容。

这个直接搞。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
typedef long long LL;
template <class T> inline void read(T &x)
{
    x = 0;
    T flag = 1;
    char ch = (char)getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch == '-') flag = -1;
        ch = (char)getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x = (x<<1) + (x<<3) + ch - '0';
        ch = (char)getchar();
    }
    x *= flag;
}
template <class T> T gcd(T a,T b) { return !b?a:gcd(b,a%b); }
const int INF=0x3f3f3f3f;
const int maxn = 120;
struct Node
{
    int fa;
    string s;
    int val; // 1 for D , 2 for F
    bool disabled;
}node[maxn];
#define fa(x) node[x].fa
#define s(x) node[x].s
#define val(x) node[x].val
#define disabled(x) node[x].disabled
struct Edge
{
    int to,next;
}edge[maxn];
int head[maxn];
int maxnode,maxedge;
inline void addedge(int u,int v)
{
    edge[++maxedge] = (Edge) { v,head[u] };
    head[u] = maxedge;
}
int root;
void initialize()
{
    root = maxnode = 1;
    memset(head,-1,sizeof(head)); maxedge=-1;
}
void go_back()
{
    if(!fa(root)) puts("No parent directory!");
    else root = fa(root);
}
void go_forward(const string &to)
{
    for(int i=head[root];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(disabled(v) || val(v)!=1) continue;
        if(s(v) == to)
        {
            root = v;
            return;
        }
    }
    puts("No such directory!");
}
void touch(const string &to)
{
    for(int i=head[root];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(disabled(v) || val(v)!=2) continue;
        if(s(v) == to)
        {
            puts("File already exists!");
            return;
        }
    }
    ++maxnode;
    fa(maxnode) = root;
    val(maxnode) = 2; // File
    s(maxnode) = to;
    addedge(root,maxnode);
}
void rm(const string &to)
{
    for(int i=head[root];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(disabled(v) || val(v)!=2) continue;
        if(s(v) == to)
        {
            disabled(v) = true;
            return;
        }
    }
    puts("No such file!");
}
void mkdir(const string &to)
{
    for(int i=head[root];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(disabled(v) || val(v)!=1) continue;
        if(s(v) == to)
        {
            puts("Directory already exists!");
            return;
        }
    }
    ++maxnode;
    fa(maxnode) = root;
    val(maxnode) = 1; // Directory
    s(maxnode) = to;
    addedge(root,maxnode);
}
void rmdir(const string &to)
{
    for(int i=head[root];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(disabled(v) || val(v)!=1) continue;
        if(s(v) == to)
        {
            disabled(v) = true;
            return;
        }
    }
    puts("No such directory!");
}
stack <int> sta;
void ls()
{
    for(int i=head[root];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(disabled(v)) continue;
        sta.push(v);
    }
    while(!sta.empty())
    {
        int u = sta.top(); sta.pop();
        cout<<s(u);
        printf(" <%c>\n",val(u)==1?'D':'F');
    }
}
int main()
{
    freopen("nacumegu.in","r",stdin);
    freopen("nacumegu.out","w",stdout);
    initialize();
    int Q;
    read(Q);
    while(Q--)
    {
        string cmd,s;
        cin>>cmd;
        if(cmd!="ls") cin>>s;
        if(cmd=="cd")
            if(s=="..") go_back();
            else go_forward(s);
        else if(cmd[0]=='t') touch(s);
        else if(cmd[0]=='m') mkdir(s);
        else if(cmd=="ls") ls();
        else if(cmd=="rm") rm(s);
        else rmdir(s);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值