codeforces B. Pasha and String(贪心)

题意:给定一个长度为len的字符序列,然后是n个整数,对于每一个整数ai,

将字符序列区间为[ai,len-ai+1]进行反转。求出经过n次反转之后的序列!

/*
     思路1:将区间为偶数次的直接去掉!对剩下的区间进行反转。超时了,智商上的压制...
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#define N 100005
using namespace std;
char mp[2*N];
int num[N];
int cnt[N*2];

void my_swap(int &a, int &b){
    a^=b;
    b^=a;
    a^=b;
}

void my_reverse(int x, int y){
    for(int i=x, j=y; i<j; ++i, --j){
        char tmp = mp[i];
        mp[i] = mp[j];
        mp[j] = tmp;
    }
}

int main() {
     scanf("%s", mp+1);
    int m, mm=0;
    cin>>m;
    for(int i=0; i<m; ++i){
        scanf("%d", &num[i]);
        ++cnt[num[i]];
    }

    int len = strlen(mp+1);
    for(int i=1; i<=len/2; ++i){
        if(cnt[num[i]] + cnt[len-num[i]+1])%2!=0){
            int x = num[i];
            int begin = x, end= len-x+1;
            if(begin > end) my_swap(begin, end);
            my_reverse(begin, end);
        }
    }
    printf("%s\n", mp+1);
}

/*
    思路2:仔细分析,每一个反转的区间左右是对称的,如果[ai, len-ai+1]区间进行反转,
    那么就有str[ai]与str[len-ai+1]交换,str[ai+1]与str[len-ai]交换.....
    也就是ai位置发生交换,那么ai+1,ai+2...len/2也一定发生交换。如果ai位置的交换的次数
    为偶数就不用交换,为奇数就进行交换!
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#define N 100005
using namespace std;
char mp[2*N];
int cnt[N*2];//统计每一个位置交换的次数 

int main() {
    scanf("%s", mp+1);
    int m, mm=0;
    scanf("%d", &m) ;
    int len = strlen(mp+1);
    while(m--){
        int x;
        scanf("%d", &x);
        ++cnt[x], ++cnt[len-x+1];
    }

    for(int i=2; i<=len/2; ++i)
        cnt[i] += cnt[i-1];//第i个位置交换,那么第i+1,i+2..len/2个位置也一定发生交换 

    for(int i=1; i<=len/2; ++i)
        if(cnt[i]%2!=0)//奇数位置交换
            swap(mp[i], mp[len-i+1]);
    printf("%s\n", mp+1);
}
时间: 2024-12-20 18:13:18

codeforces B. Pasha and String(贪心)的相关文章

CodeForces 275C 贪心

A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x<y) from the set, such that y=x·k. You're given a set of n distinct posi

Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)

Valera has array a, consisting of n integers a0,a1,...,an-1, and function f(x), taking an integer from 0 to 2n-1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number xconta

Codeforces Round #311 (Div. 2) B. Pasha and Tea

题目链接:http://codeforces.com/contest/557/problem/B 题意:给你n个boy,n个girl ,然后w表示茶壶的最大容量,然后n个茶杯,每个都有不同的容量,要求boy的茶杯里的茶水是girl的两倍,且boy和boy的容量一样,girl和girl的容量一样,问如何倒茶,求最大的倒茶量 #include <iostream> #include <cstdio> #include <algorithm> using namespace

CodeForces 287B 二分贪心

Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water

Codeforces B. Taxi 算法题解

简单总结题意: 有一个数组,数组中的数值不会大于4,即只能是1,2,3,4,要把这些数值装进一个容量最大为4的容器里面,使得所用到这样的容器的个数最小. 经测试数据很大,会有10万个数据,所以这里我并不用排序数据,而是使用counting sort的思想,根据特定的数据优化,使得题解时间复杂度为O(n). 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 程序如下: #include <iostream>

Codeforces:Tram 列车载人问题 题解

题目: 一列电车,已经知道每站上下车的人数,那么问电车最小需要多大的容量才能让所有乘客都能搭车? Sample test(s) input 4 0 3 2 5 4 2 4 0 output 6 每站都是先下后上的,那么其实就是个很简单的问题了,可以使用贪心法: 1 初始化第一站乘客为零 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 2 每站计算上一站有的乘客加上上站的乘客,减去下站的乘客 3 每站剩下乘

UVa 714:Copying Books,最大值最小化问题(贪心 + 二分)

Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most fa

【OJ】贪心法(最小字典序)poj3617 Best Cow Line// acmclub 12701/12695

     题目链接:      点击打开链接 /* POJ 3617 Best Cow Line 贪心法--最小字典序 */ #include<stdio.h> #include<string.h> char ss[30010]; int main(){ int n,left1;scanf("%d",&n); getchar();//不可少,接收前一个\n for(int j=0;j<n;j++){// // scanf("%c"

Python解决codeforces ---- 7

 第一题 20A A. BerOS file system time limit per test 2 seconds memory limit per test 64 megabytes input standard input output standard output The new operating system BerOS has a nice feature. It is possible to use any number of characters '/' as a deli