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

Codeforces Round #249 (Div. 2) A B

时间:2014-10-02 16:53:03      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   for   sp   div   c   on   amp   

C好像就是个模拟,D 是个编码复杂度大的,但是好像也就是枚举三角形,我这会儿准备区域赛,尽量找点思维难度大的,所以昨晚A B 还是去做区域赛题吧.....

B 也有点意思 贪心

题意:交换相邻两个位的数,然后最多换k次,求最大数

解法,找<=k范围内的最大数,与之交换,右移一位,继续找,直到k用完

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const double pi = acos(-1.0);
const int INF = 100000000;

int num[65];
ll a;
int k;

int change()
{
    ll t=a;
    int f=0;
    while(t)
    {
        num[f++]=t%10;
        t/=10;
    }
    return f;
}

void print()
{
    int id=64;
    while(!num[id])--id;
    //printf("%d",num[id]);
    for(int i=id;i>=0;i--)
        printf("%d",num[i]);
    putchar('\n');
}

int main()
{

    while(~scanf("%I64d%d",&a,&k))
    {
        CL(num,0);
        int wei=change();
        wei--;
        for(int i=wei;i>=0;i--)
        {
            int maxpos=i,mx=num[i],cnt=k;
            for(int j=i-1;j>=0 && cnt>=1;j--)//
            {
                cnt--;
                if(num[j]  > mx){mx=num[j];maxpos=j;}
            }
            //k-=i-maxpos;
            //cout << "***************"  << endl;
            //cout << "mx=" << mx << "pos=" << maxpos << endl;
            //print();
            ///////
           // printf("i=%d k=%d\n",i,k);
            //for(int j=i-1;j>=maxpos && k>=1;j--)
                //swap(num[j],num[j+1]),k--;
            for(int j=maxpos;j<=i-1&&k>=1;j++)
                swap(num[j],num[j+1]),k--;
            if(!k)break;
        }
        print();
    }
    return 0;
}

A  纯模拟暴力

//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const double pi = acos(-1.0);
const int INF = 100000000;

const int MAXN= 200;
int num[MAXN];

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
            scanf("%d",&num[i]);
        int ans=0,d=0;
        for(int i=0;i<n;i++)
        {
            if(m-d>=num[i])
            {
                d+=num[i];
            }
            else
            {
                ans++;
                d=num[i];
            }
        }
        printf("%d\n",ans+1);
    }
    return 0;
}


Codeforces Round #249 (Div. 2) A B

标签:io   os   ar   for   sp   div   c   on   amp   

原文地址:http://blog.csdn.net/u011026968/article/details/39736345

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