老年玩家每日水题(完结)

先定一个小目标:

51nod:基础题(5/5)1级题(5/5)2级题(5/5)3级题(5/5)4级题(5/5)

  • 2017年10月23日

51nod1264

#include<iostream>
#include<cmath>
using namespace std;
const double eps = 1e-8;

class Point
{
public:
    double x, y;
    Point(double a=0, double b=0):x(a), y(b){}
};

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        Point p[5];
        for(int i=0; i<4; ++i)
            cin>>p[i].x>>p[i].y;
        double v1 = (p[1].x-p[0].x)*(p[1].y-p[2].y)-(p[1].y-p[0].y)*(p[1].x-p[2].x);//p10 x p12
        double v2 = (p[1].x-p[0].x)*(p[1].y-p[3].y)-(p[1].y-p[0].y)*(p[1].x-p[3].x);//p10 x p13
        double v3 = (p[3].x-p[2].x)*(p[3].y-p[0].y)-(p[3].y-p[2].y)*(p[3].x-p[0].x);//p23 x p20
        double v4 = (p[3].x-p[2].x)*(p[3].y-p[1].y)-(p[3].y-p[2].y)*(p[3].x-p[1].x);//p23 x p21
        if((v1*v2<=0) && (v3*v4<=0))
            cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}
View Code

 51nod1212

#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const LL maxn = 1e3+3, INF = 2e9+7;

int n, m;
double grap[maxn][maxn];

LL mst()
{
    LL res = 0;
    int dis[maxn];
    bool vis[maxn];
    for(int i=0; i<=n; ++i)
    {
        dis[i] = INF;
        vis[i]=false;
    }
    dis[1]=0;
    int u=1;
    for(int i=1; i<=n; ++i)
    {
        LL MinDis = INF;
        int v = u;
        for(int i=1; i<=n; ++i)
        {
            //cout<<dis[i]<<" ";
            if(!vis[i] && MinDis>dis[i])
            {
                MinDis = dis[i];
                v=i;
            }
        }
        vis[v]=true;
        u=v;
        //cout<<endl;
        //cout<<u<<" "<<dis[u]<<endl;
        res+=MinDis;
        for(int i=1; i<=n; ++i)
        {
            if(!vis[i] && grap[u][i]!=INF && dis[i] > grap[u][i])
            {
                dis[i] = grap[u][i];
            }
        }
    }
    return res;
}

int main()
{
    cin>>n>>m;
    for(int i=0; i<=n; ++i)
        for(int j=0; j<=n; ++j)
        {
            if(i==j) grap[i][j] = 0;
            else grap[i][j]=INF;
        }
    int u, v;
    double c;
    for(int i=0; i<m; ++i)
    {
        cin>>u>>v>>c;
        grap[u][v]=min(grap[u][v], c);
        grap[v][u]=min(grap[v][u], c);
    }
    cout<<mst()<<endl;
    return 0;
}
View Code
  •  2017年10月24日

51nod1183

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 1e3+3, INF = 2e9+7;
string a, b;
int dp[maxn][maxn];


int main()
{
    while(cin>>a>>b)
    {
        int la=a.size(), lb=b.size();
        for(int i=0; i<maxn; ++i)
            dp[i][0]=dp[0][i]=i;
        for(int i=0; i<la; ++i)
        {
            for(int j=0; j<lb; ++j)
            {
                int ii=i+1, jj=j+1;
                if(a[i]==b[j])
                {
                    dp[ii][jj]=dp[ii-1][jj-1];
                }
                else
                {
                    dp[ii][jj]=min(dp[ii-1][jj-1], min(dp[ii-1][jj], dp[ii][jj-1]))+1;
                }
            }
        }
        cout<<dp[la][lb]<<endl;
    }
    return 0;
}
View Code

 51nod1181

#include<iostream>
#define LL long long
using namespace std;
const int maxn = 1e7+6;

LL prime[maxn] = {0}, num_prime=0;
int isNotPrime[maxn] = {1, 1};

int main()
{
    LL n;
    cin>>n;
    for(LL i=0; i<maxn; ++i)
    {
        if(!isNotPrime[i])
        {
            prime[num_prime++]=i;
            if(i>=n && !isNotPrime[num_prime])
            {
                cout<<i<<endl;
                break;
            }
        }
        for(LL j=0; j<num_prime && i*prime[j]<maxn; ++j)
        {
            isNotPrime[i*prime[j]]=1;
            if(!(i%prime[j]))
                break;
        }
    }
    return 0;
}
View Code

51nod1079

#include<iostream>
#define int long long
using namespace std;
int Extended_Euclid(int a, int b, int &x, int &y)
{
    int d;
    if(b==0)
    {
        x=1, y=0;
        return a;
    }
    d=Extended_Euclid(b, a%b, y, x);
    y-=a/b*x;
    return d;
}

int Chinese_Remainder(int a[], int w[], int len)
{
    int i, d, x, y, m, n=1, ret=0;
    for(i=0; i<len; ++i)
        n*=w[i];
    for(i=0; i<len; ++i)
    {
        m=n/w[i];
        d=Extended_Euclid(w[i], m, x, y);
        ret=(ret+y*m*a[i])%n;
    }
    return (n+ret%n)%n;
}

main()
{
    int n;
    int w[10], a[10];
    while(cin>>n)
    {
        for(int i=0; i<n; ++i)
            cin>>w[i]>>a[i];
        cout<<Chinese_Remainder(a, w, n)<<endl;
    }
    return 0;
}
View Code
  •  2017年10月25日

51nod1305

#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#define int long long
using namespace std;

vector<int> a;

int fun(vector<int>& a)
{
    sort(a.begin(), a.end());
    int sum=0, tmp=0, la=a.size();
    for(int i=0; i<la; ++i)
    {
        for(int j=i+1; j<la; ++j)
        {
            tmp=(floor((a[i]+a[j])/(a[i]*a[j])));
            sum+=tmp;
            if(tmp<=0) break;
        }
    }
    return sum;
}

main()
{
    int n, t;
    cin>>n;
    for(int i=0; i<n; ++i)
    {
        cin>>t;
        a.push_back(t);
    }
    cout<<fun(a)<<endl;
    return 0;
}
View Code
#include<iostream>
#include<vector>
#define int long long
using namespace std;

main()
{
    int n, t;
    cin>>n;
    int ans=0, two=0;
    for(int i=1; i<=n; ++i)//只有 1, 2 会对结果产生影响
    {
        cin>>t;
        if(t==1)
        {
            ans+=n-1;
        }
        else if(t==2)
        {
            ans+=two;
            two++;
        }
    }
    cout<<ans<<endl;
    return 0;
}
View Code
  •  2017年10月28日

51nod1289

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>

using namespace std;
stack<int> Stack;


int main()
{
    int n;
    cin>>n;
    int cnt=n;
    for(int i=0; i<n; ++i)
    {
        int a, b;
        cin>>a>>b;
        if(b==1) Stack.push(a);
        else
        {
            while(!Stack.empty())
            {
                if(a>Stack.top())
                {
                    cnt--;
                    Stack.pop();
                }
                else
                {
                    cnt--;
                    break;
                }
            }
        }
    }
    cout<<cnt<<endl;

    return 0;
}
View Code

51nod1283

#include<iostream>
#include<cmath>
#define LL long long
using namespace std;

int main()
{
    LL s;
    cin>>s;
    LL a=sqrt(s);
    for(int i=a; i<=s; ++i)
    {
        if(s%i==0)
        {
            cout<<2*(i+s/i)<<endl;
            break;
        }
    }
    return 0;
}
View Code

51nod1182

#include<iostream>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;

int sum[100];

int main()
{ 
    memset(sum, 0, sizeof(sum));
    string str;
    cin>>str;
    for(int i=0; i<str.size(); ++i)
    {
        char t = str[i];
        sum[tolower(t)]++;
    }
    sort(sum+'a', sum+'z'+1);
    int ans=0;
    for(int i='z'; i>='a'; --i)
    {
        ans+=(sum[i]*(i+1-'a'));
    }
    cout<<ans<<endl;
    return 0;
}
View Code
  •  2017年10月29日

51nod1091

#include<iostream>
#include<vector>
#include<algorithm>
#define int long long
using namespace std;

class Line
{
public:
    int x, y;
    Line(int a, int b):x(a), y(b)
    {
    }

    bool operator < (const Line& l)
    {
        if(x==l.x)
            return y<l.y;
        return x<l.x;
    }
};

vector<Line> Lines;

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    int x, y;
    cin>>n;
    for(int i=0; i<n; ++i)
    {
        cin>>x>>y;
        Lines.push_back(Line(x, y));
    }
    sort(Lines.begin(), Lines.end());
    int len = Lines.size();
    int ans=0, pre=Lines[0].y;
    for(int i=1; i<len; ++i)
    {
        if(Lines[i].y > pre)
        {
            ans = max(ans, pre-Lines[i].x);
            pre=Lines[i].y;
        }
        else
        {
            ans = max(ans, Lines[i].y-Lines[i].x);
        }
    }
    cout<<ans<<endl;
    return 0;
}
View Code

 51nod1873

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
struct BigNum
{
    int len;
    int num[10000];
    int point;
    BigNum()
    {
        len=1;
        point=0;
        memset(num,0,sizeof(num));
    }
};
bool input(BigNum &a)
{
    string s;
    if(cin>>s)
    {
        memset(a.num,0,sizeof(a.num));
        int t=0,i;
        a.len=s.size();
        a.point=0;
        for(i=s.size()-1;i>=0;i--)
        {
            if(s[i]=='.'){a.len--;a.point=t;continue;}
            a.num[t++]=s[i]-'0';
        }
        return 1;
    }
    else return 0;
}
void output(BigNum &a)
{
    int i,j=0,flag;
    for(i=0;i<a.point&&a.num[i]==0;i++);
    flag=i;
    if(a.point==a.len)
    {
        if(flag==a.point){cout<<"0"<<endl;return;}
        else cout<<".";
    }
    for(i=a.len-1;i>=0;i--)
    {
        cout<<a.num[i];
        if(i==flag)break;
        if(i==a.point)cout<<".";
    }
    cout<<endl;
}
BigNum Mul(BigNum &a, BigNum &b)
{
    int i, j, len = 0;
    BigNum c;
    for(i = 0; i < a.len; i++)
        for(j = 0; j < b.len; j++)
        {
            c.num[i+j] += (a.num[i]*b.num[j]);
            if(c.num[i+j] >= 10)
            {
                c.num[i+j+1] += (int)c.num[i+j]/10;
                c.num[i+j] %= 10;
            }
        }
    c.point=a.point+b.point;
    len = a.len+b.len;
    while(c.num[len-1] == 0 && len > 1&&len>c.point) len--;
    if(c.num[len]) len++;
    c.len = len;
    return c;
}
BigNum a;
int b;
int main()
{
    while(input(a)&&~scanf("%d",&b))
    {
        BigNum ans;
        if(b==0){cout<<1<<endl;continue;}
        ans.num[0]=1;
        while(b--)
        {
            ans=Mul(ans,a);
        }
        output(ans);
    }
}
View Code

51nod1413

#include<iostream>
using namespace std;
int main()
{
    string str;
    cin>>str;
    int ans=0;
    for(int i=0; i<str.size(); ++i)
        ans=max(ans, int(str[i]));
    cout<<(ans-'0')<<endl;
    return 0;
}
View Code

 51nod1432

#include<iostream>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
const int maxn = 10005;
int Weight[maxn];

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin>>n>>m;
    for(int i=0; i<n; ++i)
    {
        cin>>Weight[i];
    }
    int cnt=0;
    sort(Weight, Weight+n);
    int pre=0, las=n-1;
    while(pre<=las)
    {
        if(pre==las)
        {
            cnt++;
            break;
        }
        if(Weight[pre] + Weight[las] <= m)
        {
            pre++;
            las--;
            cnt++;
        }
        else
        {
            while(las>pre && (Weight[pre] + Weight[las] > m))
            {
                las--;
                cnt++;
            }
            pre++;
            las--;
            cnt++;
        }
    }
    cout<<cnt<<endl;
    return 0;
}
View Code

51nod1428

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#define int long long
using namespace std;
const int maxn = 10004;

class Line
{
public:
    int s, f;

    Line(int a=0, int b=0):s(a), f(b)
    {
    }

    bool operator < (const Line& l) const
    {
        if(this->f==l.f)
            return this->s>l.s;
        return this->f>l.f;
    }
};

vector<Line> lines;
priority_queue<Line> PQ;
bool vis[maxn];

bool cmp(Line& a, Line& b)
{
    if(a.s == b.s)
        return a.f<b.f;
    return a.s<b.s;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    memset(vis, false, sizeof(vis));

    int n, s, f;
    cin>>n;
    for(int i=0; i<n; ++i)
    {
        cin>>s>>f;
        if(s>f) swap(s, f);
        lines.push_back(Line(s, f));
    }
    sort(lines.begin(), lines.end(), cmp);
    int len = lines.size();
    PQ.push(lines[0]);
    //cout<<"push: "<<PQ.top().s<<", "<<PQ.top().f<<endl;
    for(int i=1; i<len; ++i)
    {
        //cout<<"top: "<<PQ.top().f<<endl;
        if(lines[i].s < PQ.top().f)
        {
            PQ.push(lines[i]);
            //cout<<"push: "<<lines[i].s<<", "<<lines[i].f<<endl;
        }
        else
        {
            //cout<<"pop: "<<PQ.top().s<<", "<<PQ.top().f<<endl;
            PQ.pop();
            PQ.push(lines[i]);
            //cout<<"push: "<<lines[i].s<<", "<<lines[i].f<<endl;
        }
    }
    cout<<PQ.size()<<endl;
    return 0;
}
View Code
  •  2017年10月30日

51nod1315

#include<iostream>
#include<vector>
#include<cstring>
#include<bitset>
#define int long long
using namespace std;
const int maxn = 2e9+9;

int binary[50];
bool vis[50];
int nums[50];

int GetBinary()
{
    memset(binary, 0, sizeof(binary));
    int val=1, i=0;
    while(val<=maxn)
    {
        binary[i]=val;
        i++;
        val<<=1;
    }
    return i;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int sum=GetBinary();
    memset(vis, false, sizeof(vis));
    memset(nums, 0, sizeof(nums));

    int n, x, t, maxs=0;
    cin>>n>>x;
    for(int i=0; i<sum; ++i)
    {
        if(x&binary[i])
        {
            maxs=max(maxs, i+1);
            vis[i]=true;
        }
        else vis[i]=false;
    }
    for(int i=0; i<n; ++i)
    {
        cin>>t;
        if((t|x)<=x)
            for(int i=0; i<sum; ++i)
                if(t&binary[i])
                    nums[i]++;
    }
    int ans=n;
    for(int i=0; i<maxs; ++i)
        if(vis[i])
            ans=min(ans, nums[i]);
    cout<<ans<<endl;
    return 0;
}
View Code

 51nod1636

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#define int long long
using namespace std;
const int maxn = 105, maxm = 105, maxk = 200;

class Lesson
{
public:
    int a, b, c;

    Lesson(int x=0, int y=0, int z=0):a(x), b(y), c(z)
    {
    }

    bool operator < (const Lesson& l) const
    {
        return this->c<l.c;
    }
};

vector<Lesson> lessons;
int dp[maxn][maxm][maxk];

main()
{
    int n, m, k;
    cin>>n>>m>>k;
    int a, b, c;
    for(int i=1; i<=m; ++i)
    {
        cin>>a>>b>>c;
        lessons.push_back(Lesson(a, b, c));
    }
    sort(lessons.begin(), lessons.end());
    memset(dp, -1, sizeof(dp));

    for(int i=0; i<m; ++i)//dp[i][j][k] : i天、以课程j结尾、当天的作业量为j+a[j].c的总作业量。
    {
        for(int j=lessons[i].a; j<=lessons[i].b; ++j)
        {
            dp[1][i][j-lessons[i].a] = j;
        }
    }//预处理完一天的

    for(int i=2; i<=n; ++i)//i天
    {
        for(int j=0; j<m; ++j)//以课程j结尾
        {
            for(int p=lessons[j].a; p<=lessons[j].b; ++p)//最后一天课程量为p
            {
                for(int q=0; lessons[q].c<lessons[j].c; ++q)//前一个状态
                {
                    if(p-k >= lessons[q].a && p-k <= lessons[q].b && dp[i-1][q][p-k-lessons[q].a]!=-1)
                    {
                        dp[i][j][p-lessons[j].a]=max(dp[i][j][p-lessons[j].a], dp[i-1][q][p-k-lessons[q].a] + p);
                    }
                    if(p%k==0 && p/k >= lessons[q].a && p/k<=lessons[q].b && dp[i-1][q][p/k-lessons[q].a]!=-1)
                    {
                        dp[i][j][p-lessons[j].a]=max(dp[i][j][p-lessons[j].a], dp[i-1][q][p/k-lessons[q].a] + p);
                    }
                }
            }
        }
    }
    int ans = 0;
    for(int i=0; i<m; ++i)
    {
        for(int j=lessons[i].a; j<=lessons[i].b; ++j)
        {
            ans = max(ans, dp[n][i][j-lessons[i].a]);
        }
    }
    if(ans == 0)
        cout<<"NO"<<endl;
    else
    {
        cout<<"YES"<<endl;
        cout<<ans<<endl;
    }
    return 0;
}
View Code
  •  2017年10月31日

51nod1875

#include<iostream>
#define int long long
using namespace std;

int ans[20]={0};

int deal(int x)
{
    for(int i=1; i<=1e7; ++i)
    {
        int all = 2*x;
        int s = 0;
        while(all>x)
        {
            s += (s==0?0:-1);
            s = (s+i)%all;
            if(s<=x && s!=0)
                break;
            all--;
            if(all == x)
                return i;
        }
    }
}

void init()
{
    ans[1]=1;
    for(int i=1; i<15; ++i)
        ans[i] = deal(i);
}

main()
{
    init();
    int G;
    while(cin>>G && G!=0)
    {
        cout<<ans[G]<<endl;
    }
    return 0;
}
View Code

51nod1572

#include<iostream>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
const int maxn = 1e3+5;

class Op
{
public:
    int dir, len;

    Op(int a=0, int b=0):dir(a), len(b)
    {
    }
};

char grap[maxn][maxn];
int limit[maxn][maxn][5];//每个点四个方向的最大值
int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1} };//上下右左
int word[30][3];
vector<Op> ops;

bool check(int w, int n, int m)
{
    int x=word[w][1], y=word[w][2];
    for(int i=0; i<ops.size(); ++i)
    {
        Op item = ops[i];
        if(item.len >= limit[x][y][item.dir])
            return false;
        x+=item.len * dir[item.dir][0], y+=item.len * dir[item.dir][1];
        if(x<0 || y<0 || x>n || y>m || grap[x][y]=='#')
            return false;
    }
    return true;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(word, -1, sizeof(word));

    int n, m, k, pre=0;
    cin>>n>>m;
    for(int i=0; i<n; ++i)
    {
        cin>>grap[i];
        pre=0;
        for(int j=0; j<m; ++j)
        {
            if(grap[i][j]>='A' && grap[i][j]<='Z')
            {
                word[int(grap[i][j]-'A')][0] = 1;
                word[int(grap[i][j]-'A')][1] = i;
                word[int(grap[i][j]-'A')][2] = j;
            }
            if(grap[i][j] == '#')
            {
                pre=j;
            }
            else
            {
                limit[i][j][3]=abs(j-pre);
            }
        }
        pre=0;
        for(int j=m-1; j>=0; --j)
        {
            if(grap[i][j] == '#')
            {
                pre=j;
            }
            else
            {
                limit[i][j][2]=abs(j-pre);
            }
        }
    }
    for(int j=0; j<m; ++j)
    {
        pre=0;
        for(int i=0; i<n; ++i)
        {
            if(grap[i][j] == '#')
            {
                pre=i;
            }
            else
            {
                limit[i][j][0]=abs(i-pre);
            }
        }
        pre=0;
        for(int i=n-1; i>=0; --i)
        {
            if(grap[i][j] == '#')
            {
                pre=i;
            }
            else
            {
                limit[i][j][1]=abs(i-pre);
            }
        }
    }
    cin>>k;
    char d;
    int l;
    for(int i=0; i<k; ++i)
    {
        cin>>d>>l;
        switch(d)
        {
            case 'N':ops.push_back(Op(0, l));break;
            case 'S':ops.push_back(Op(1, l));break;
            case 'E':ops.push_back(Op(2, l));break;
            case 'W':ops.push_back(Op(3, l));break;
        }
    }
    bool can = false;
    for(int i=0; i<26; ++i)
    {
        if(word[i][0]!=-1)
        {
            if(check(i, n, m))
            {
                cout<<char(i+'A');
                can = true;
            }
        }
    }
    if(!can)cout<<"no solution"<<endl;

    return 0;
}
View Code

51nod1562

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<stack>
#define int long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1;
using namespace std;
const int maxn = 200005;

class Point
{
public:
    int LeftPos, RightPos, LeftLen;

    Point(int lp=0, int rp=0, int ll=0):LeftPos(lp),RightPos(rp),LeftLen(ll)
    {
    }
};

vector<int> is;
vector<char> cs;
int wline[maxn], hline[maxn];
Point wpoint[maxn], hpoint[maxn];

main()
{
    int w, h, n, x;
    char op;
    memset(wline, 0, sizeof(wline));
    memset(hline, 0, sizeof(hline));

    cin>>w>>h>>n;
    for(int i=0; i<n; ++i)
    {
        cin>>op>>x;
        cs.push_back(op);
        is.push_back(x);
        if(op=='H')
        {
            hline[x]=1;
        }
        else
        {
            wline[x]=1;
        }
    }
    hline[0]=wline[0]=hline[h]=wline[w]=1;
    int maxh=0, maxw=0;
    for(int i=0, pre=0; i<=w; ++i)
    {
        if(wline[i])
        {
            maxw=max(maxw, i-pre);
            wpoint[pre].RightPos=i;
            wpoint[i]=Point(pre, i, i-pre);
            pre=i;
        }
    }
    for(int i=0, pre=0; i<=h; ++i)
    {
        if(hline[i])
        {
            maxh=max(maxh, i-pre);
            hpoint[pre].RightPos=i;
            hpoint[i]=Point(pre, i, i-pre);
            pre=i;
        }
    }
    //cout<<"maxw: "<<maxw<<"maxh: "<<maxh<<endl;
    Point tmp;
    stack<int> ans;
    for(int i=n-1; i>=0; --i)
    {

        ans.push(maxw*maxh);
        if(cs[i]=='H')
        {
            tmp = hpoint[is[i]];
            hpoint[tmp.LeftPos].RightPos = tmp.RightPos;
            hpoint[tmp.RightPos].LeftPos = tmp.LeftPos;
            hpoint[tmp.RightPos].LeftLen += tmp.LeftLen;
            maxh = max(maxh, hpoint[tmp.RightPos].LeftLen);
        }
        else
        {
            tmp = wpoint[is[i]];
            wpoint[tmp.LeftPos].RightPos = tmp.RightPos;
            wpoint[tmp.RightPos].LeftPos = tmp.LeftPos;
            wpoint[tmp.RightPos].LeftLen += tmp.LeftLen;
            maxw = max(maxw, wpoint[tmp.RightPos].LeftLen);
        }
    }
    //cout<<endl<<" ans "<<endl;

    while(!ans.empty())
    {
        cout<<ans.top()<<endl;
        ans.pop();
    }
    return 0;
}
View Code
  •  2017年11月1日

51nod1785

#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int n,sum,p,num,k,now,cnt,s[105],a[1000050];
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 main()
{
    n=read();k=read();
    while(n--)
    {
        p=read();
        if(p==1)
        {
            num=read();
            a[++cnt]=num;
            if(now==k)sum=sum-a[cnt-k]+num,s[a[cnt-k]]--;
            else now++,sum+=num;
            s[num]++;
        }
        else if(p==2)
        {
            printf("%.2lf\n",floor((double)sum/now));
            //printf("debug: %d, %d\n", sum, now);
        }
        else if(p==3)
        {
            double w=(double)sum/now,ans=0;
            for(int i=cnt-now+1;i<=cnt;i++)ans+=((double)a[i]-w)*((double)a[i]-w);
            printf("%.2lf\n",ans/now);
        }
        else
        {
            if(now%2)
            {
                int pos=now/2+1,ans=0;
                for(int i=0;i<=100;i++)
                {
                    ans+=s[i];
                    if(ans>=pos)
                    {
                        printf("%.2lf\n",(double)i);
                        break;
                    }
                }
            }
            else
            {
                int pos1=now/2,pos2=now/2+1,st=-1,ed=-1,ans=0;
                for(int i=0;i<=100;i++)
                {
                    ans+=s[i];
                    if(ans>=pos1&&st==-1)st=i;
                    if(ans>=pos2&&ed==-1)ed=i;
                    if(st!=-1&&ed!=-1)
                    {
                        printf("%.2lf\n",(double)(st+ed)/2);
                        break;
                    }
                }
            }
        }
    }
    return 0;
}
View Code

 51nod1637

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 1e5+5;
char str[maxn];
int main()
{
    int n, k;
    scanf("%d%d",&n,&k);
    getchar();
    gets(str+1);
    bool can=false;
    while(k)
    {
        for(int i=1; i<n; ++i)
        {
            if(str[i]=='4' && str[i+1] == '7')
            {
                can=true;
                if(i&1)
                {
                    str[i+1]='4';
                    if(str[i+2]=='7')
                    {
                        if(!(k&1)) str[i+1]='7';
                        can=false;
                        break;
                    }
                }
                else
                {
                    str[i]='7';
                    if(str[i-1]=='4')
                    {
                        if(!(k&1))
                        {
                            str[i]='4';
                        }
                        can=false;
                        break;
                    }
                }
                break;
            }
        }
        if(!can) break;
        --k;
    }
    puts(str+1);
    return 0;
}
View Code
  •  2017年11月2日

51nod1829

#include<iostream>
#define int long long
using namespace std;
const int mod = 1e9+7, maxn = 1e6+6;

int c[maxn];

int Pow(int x, int n)
{
    int res=1;
    x=x%mod;
    n=n%(mod-1);
    while(n)
    {
        if(n&1)
            res=res*x%mod;
        n>>=1;
        x=x*x%mod;
    }
    return res;
}

int com(int n, int m)
{
    if(n>m) return n;
    return c[m]*Pow(c[n]*c[m-n]%mod, mod-2)%mod;
}

void init()
{
    c[0]=1;
    for(int i=1; i<maxn; ++i)
        c[i]=(c[i-1]*i)%mod;
}

main()
{
    init();
    int n, m;
    cin>>n>>m;
    int ans=0, k=1;
    for(int i=m; i>=1; --i)
    {
        ans=(ans+(com(m-i, m)%mod + mod)%mod*(Pow(i, n)%mod*k + mod)%mod)%mod;
        k=0-k;
    }
    cout<<ans<<endl;
    return 0;
}
View Code

 51nod1832

#include<iostream>
#include<cstring>
#include<stack>
#define int long long
#define L 1
#define R 2
using namespace std;
const int maxn = 1e4+5;

int r[maxn], a[maxn], b[maxn], cnt=0;
int ans[maxn];

void dfs(int al, int ar, int bl, int br)
{
    if(al==ar) return;
    al++, br--;
    int p = r[a[al]];
    if(p==br)
    {
        cnt++;
        dfs(al, ar, bl, br);
        return;
    }
    int len = p-bl+1;
    dfs(al, al+len-1, bl, bl+len-1);
    dfs(al+len, ar, bl+len, br);
}

main()
{
    memset(r, 0, sizeof(r));
    memset(ans, 0, sizeof(ans));
    int n;
    cin>>n;
    for(int i=1; i<=n; ++i)
    {
        cin>>a[i];
    }
    for(int i=1; i<=n; ++i)
    {
        cin>>b[i];
        r[b[i]]=i;
    }
    dfs(1, n, 1, n);
    ans[0]=1;
    int len=1;
    while(cnt)
    {
        for(int i=0; i<len; ++i)
            ans[i]*=2;
        int t=0;
        for(int i=0; i<len; ++i)
        {
            ans[i]+=t;
            t=ans[i]/10;
            ans[i]%=10;
        }
        while(t)
        {
            ans[len]+=t;
            t=ans[len]/10;
            ans[len]%=10;
            len++;
        }
        cnt--;
    }
    for(int i=len-1; i>=0; --i)
        cout<<ans[i];
    return 0;
}
/*
5
1 2 3 4 5
5 4 3 2 1
*/
View Code

 51nod1791

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
#define int long long
string str;
stack<int> s;
int T;
int ans,num[1000505];
main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>T;
    while(T--)
    {
        //memset(num, 0, sizeof(num));
        cin>>str;
        ans=0;
        while(!s.empty()) s.pop();
        int len=str.size();
        for(int i=0; i<len; i++)
        {
            if(str[i]=='(')
            {
                num[i]=0;
                s.push(i);
            }
            else
            {
                if(s.empty())
                {
                    num[i]=0;
                    continue;
                }
                ans+=num[s.top()-1]+1;
                num[i]=num[s.top()-1]+1;
                s.pop();
            }
        }
        cout<<ans<<endl;
    }

}
View Code

 51nod1717

#include<iostream>
#define int long long
using namespace std;

main()
{
    int n;
    while(cin>>n)
    {
        int cnt=n;
        for(int i=1; i*i<=n; ++i)
        {
            cnt--;
        }
        cout<<cnt<<endl;
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/xiepingfu/p/7717212.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值