Codeforces Round #469 (Div. 2)

C. Zebras
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.


Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days.


Input
In the only line of input data there is a non-empty string s consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as |s|) does not exceed 200 000 characters.


Output
If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer k (1 ≤ k ≤ |s|), the resulting number of subsequences. In the i-th of following k lines first print the integer li (1 ≤ li ≤ |s|), which is the length of the i-th subsequence, and then li indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to n must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.


Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of k.


Examples
inputCopy
0010100
output
3
3 1 3 4
3 2 5 6
1 7
inputCopy
111
output

-1

题意:把一串字符分成0,010,01010,0101010........

#include<map>
#include<set>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f
#define ll long long
#define maxn 200005
using namespace std;
string s;
int ans;
vector<int>v[maxn];
int main(){
    cin>>s;ans=0;int flag=0;
    for(int i=0;i<s.size();i++){
        if(s[i]=='1'){
            if(ans-1<0){cout<<-1<<endl;return 0;}
            else v[--ans].push_back(i+1);
        }
        else if(s[i]=='0'){
        v[ans++].push_back(i+1);
        }
        flag=max(flag,ans);
    }
    if(flag!=ans){cout<<-1<<endl;return 0;}
    printf("%d
",ans);
    for(int i=0;i<ans;i++){printf("%d",v[i].size());
        for(int j=0;j<v[i].size();j++)
         printf(" %d",v[i][j]);
         printf("
");
    }
}

D. A Leapfrog in the Array
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.


Let's consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:


http://codeforces.com/predownloaded/1e/83/1e838f4fb99d933b7259fbfe5b8722990c08d718.png



You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima's algorithm finishes.


Input
The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.


Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes.


Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.


Examples
inputCopy
4 3
2
3
4
output
3
2
4
inputCopy
13 4
10
5
4
8
output
13
3
8
9
Note
The first example is shown in the picture.


In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].

规律题。。

#include<map>
#include<set>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f
#define ll long long
#define maxn 200005
using namespace std;
int main(){
    ll n,m;
    cin>>n>>m;
    ll x;
    for(int i=0;i<m;i++){
        cin>>x;
        if(x>n) cout<<0<<endl;
        else{
            if(x%2==1) cout<<x/2+1<<endl;
            else{
                ll a=x,b,c=n;
                while(1){
                    if(a%2==1)break;
                    b=a+(c-a/2);
                    a=b;
                }cout<<a/2+1<<endl;
            }
        }
    }

}





原文地址:https://www.cnblogs.com/da-mei/p/9053261.html