码迷,mamicode.com
首页 > 其他好文 > 详细

B3300 [USACO2011 Feb]Best Parenthesis 模拟

时间:2018-08-19 18:01:41      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:namespace   efi   write   include   ||   set   stdout   double   end   

这是我今天遇到最奇怪的问题,希望有人帮我解释一下。。。

一开始我能得90分:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
typedef long long ll;
const int INF = 1 << 30;
const ll mod = 12345678910;

typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < 0 || c > 9)
        if(c == -) op = 1;
    x = c - 0;
    while(c = getchar(), c >= 0 && c <= 9)
        x = x * 10 + c - 0;
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar(-), x = -x;
    if(x >= 10) write(x / 10);
    putchar(0 + x % 10);
}
ll n,s = 0;
ll p[50000],x,ans = 0;
ll num = 0;
int main()
{
    read(n);
    duke(i,1,n)
    {
        read(x);
        if(x == 0)
        {
            p[++num] = x;
        }
        else
        {
            s = 0;
            while (p[num] != 0)
            {
                s += p[num];
                s = s % mod;
                num--;
            }
            if(s == 0)
            {
                p[num] = 1;
            }
            else
            {
                p[num] = s * 2 % mod;
            }
        }
    }
    ll tot = 0;
    duke(i,1,num)
    {
//        cout<<p[i]<<endl;
        tot += p[i];
        tot %= mod;
    }
    write(tot);
    return 0;
}

然而并不知道为什么。。。找了个题解,发现没啥区别,然后就改了一下num的顺序,结果变成80,最神奇的是之前错的那个点对了,然而另两个点错了。。。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
typedef long long ll;
const int INF = 1 << 30;
const ll mod = 12345678910;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < 0 || c > 9)
        if(c == -) op = 1;
    x = c - 0;
    while(c = getchar(), c >= 0 && c <= 9)
        x = x * 10 + c - 0;
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar(-), x = -x;
    if(x >= 10) write(x / 10);
    putchar(0 + x % 10);
}
ll n,s = 0;
ll p[50000],x,ans = 0;
ll num = 1;
int main()
{
//    freopen("1.in","r",stdin);
//    freopen("1.out","w",stdout);
    read(n);
    p[0] = 0;
    p[1] = 0;
    clean(p);
    duke(i,1,n)
    {
        read(x);
        if(x == 0)
        {
            p[num++] = x;
        }
        if(x == 1)
        {
            s = 0;
            while (p[num - 1] != 0)
            {
                s += p[num - 1];
                s = s % mod;
                num--;
            }
            /*if(i == n)
            {
                printf("%lld\n",s);
                cout<<num - 1<<endl;
            }*/
            if(s == 0)
            {
                p[num - 1] = 1;
            }
            else
            {
                p[num - 1] = (s * 2) % mod;
            }
            /*if(i == n)
            {
                printf("%lld\n",p[num - 1]);
                cout<<num - 1<<endl;
            }*/
        }
    }
    ll tot = 0;
    duke(i,1,num - 1)
    {
//        cout<<p[i]<<endl;
        tot += p[i];
        tot %= mod;
    }
    write(tot);
    return 0;
}

蒙了,然后一点点de,到最后也没看出来。通过一些手段发现了是算和的时候出问题了,然而并不知道为什么。。。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
typedef long long ll;
const int INF = 1 << 30;
const ll mod = 12345678910;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < 0 || c > 9)
        if(c == -) op = 1;
    x = c - 0;
    while(c = getchar(), c >= 0 && c <= 9)
        x = x * 10 + c - 0;
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar(-), x = -x;
    if(x >= 10) write(x / 10);
    putchar(0 + x % 10);
}
ll n,s = 0;
ll p[50000],x,ans = 0;
ll num = 1;
int main()
{
//    freopen("1.in","r",stdin);
//    freopen("1.out","w",stdout);
    read(n);
    p[0] = 0;
    p[1] = 0;
    clean(p);
    duke(i,1,n)
    {
        read(x);
        if(x == 0)
        {
            p[num] = x;
            num++;
        }
        if(x == 1)
        {
            s = 0;
            while (p[num - 1] != 0)
            {
                s += p[num - 1];
                s = s % mod;
                num--;
            }
            if(s == 0)
            {
                p[num - 1] = 1;
            }
            else
            {
                p[num - 1] = (s * 2) % mod;
            }
        }
    }
    for(int i=2;i<num;i++)
    {
//        cout<<p[i]<<endl;
        p[1] += p[i];
        p[1] = p[1] % 12345678910;
    }
    cout<<p[1];
    return 0;
}
/*
6
0
0
1
1
0
1
( ( ) ) ( )
0 0 1 2 0 1 
*/

就是把

ll tot = 0;
    duke(i,1,num - 1)
    {
//        cout<<p[i]<<endl;
        tot += p[i];
        tot %= mod;
    }
    write(tot);

换成

for(int i=2;i<num;i++)
    {
//        cout<<p[i]<<endl;
        p[1] += p[i];
        p[1] = p[1] % 12345678910;
    }
    cout<<p[1];

就对了!!!震惊!!!为什么?求大佬解释。。。

B3300 [USACO2011 Feb]Best Parenthesis 模拟

标签:namespace   efi   write   include   ||   set   stdout   double   end   

原文地址:https://www.cnblogs.com/DukeLv/p/9501902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!