跳过正文
  1. Posts/

uva 120 Stacks of Flapjacks

·2 分钟

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid;=8&page;=show_problem&problem;=56

题意:给出一个长度为n的序列(无重复元素),询问经过多少次flip(i)操作,使得序列升序排列。定义flip(i)为将1到n-i+1的元素反转… 思路:先离散化,然后注意读入….

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年01月25日 星期一 10时42分46秒
  4File Name :code/uva/120.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=105;
 34int n;
 35int a[N];
 36string in;
 37
 38struct node
 39{
 40    int value;
 41    int id;
 42}q[N];
 43int pos[N];
 44bool cmp1(node a,node b)
 45{
 46    return a.value<b.value;
 47}
 48bool cmp2(node a,node b)
 49{
 50    return a.id<b.id;
 51}
 52
 53
 54void flip2( int x)
 55{
 56    for ( int i = 1 ; i <= x/2 ; i++)
 57    {
 58	swap(q[i].value,q[x+1-i].value);
 59    }
 60}
 61
 62void print( int n)
 63{
 64    for ( int i = 1 ; i <= n ; i ++) printf(" %d ",q[i].value);
 65    printf("\n");
 66}
 67
 68void getpos( int n)
 69{
 70    for ( int i = 1 ; i <=  n ; i++)
 71    {
 72	pos[q[i].value] = i;
 73    }
 74}
 75int main()
 76{
 77	#ifndef  ONLINE_JUDGE
 78	freopen("code/in.txt","r",stdin);
 79  #endif
 80
 81	while (getline(cin,in))
 82	{
 83	    ms(pos,-1); //pos[i]表示数字i的位置
 84	   // cout<<"oriin:"<<in<<endl;
 85	    int p = in.find(' ');
 86	    int val;
 87	    int cnt = 0 ;
 88	    while (p!=-1)
 89	    {
 90
 91		string tmp = in.substr(0,p);
 92		sscanf(tmp.c_str(),"%d",&val);
 93		in.erase(0,p+1);
 94		p = in.find(' ');
 95	//	cout<<"in:"<<in<<endl;
 96		cnt++;
 97		a[cnt] = val;
 98	    }
 99	    sscanf(in.c_str(),"%d",&val);
100	    cnt++;
101	    a[cnt]=val;
102
103	   for ( int i = 1 ; i <= cnt ; i++) cout<<a[i]<<" ";
104	   cout<<endl;
105	  //hash
106	    for ( int i = 1 ; i <= cnt ; i++)
107	    {
108		q[i].value = a[i];
109		q[i].id = i ;
110	    }
111
112	    sort(q+1,q+cnt+1,cmp1);
113	    int hashnum = 0;
114	    for ( int i = 1 ; i <=cnt ; i++)
115	    {
116		hashnum++;
117		q[i].value = hashnum;
118	    }
119	    sort(q+1,q+cnt+1,cmp2);
120
121	    //check hash
122//		for ( int i =1 ; i <= cnt ; i++) cout<<"q[i].val:"<<q[i].value<<" ";
123
124
125	    for ( int i = 1 ; i <= cnt ; i++)
126	    {
127		pos[q[i].value] = i ;
128	    }
129
130	   // print(cnt);
131	    for ( int i = cnt ; i >=1 ; i--)
132	    {
133		if (pos[i]==i) continue;
134		if (pos[i]==1)
135		{
136		    printf("%d ",cnt+1-i);
137		    flip2(i);
138		 //   print(cnt);
139		    getpos(cnt);
140
141		}
142		else
143		{
144		    printf("%d ",cnt+1-pos[i]);
145		    flip2(pos[i]);
146		  //  print(cnt);
147		    getpos(cnt);
148		    printf("%d ",cnt+1-i);
149		    flip2(i);
150		  //  print(cnt);
151		    getpos(cnt);
152		}
153	    }
154	    puts("0");
155
156
157
158	}
159
160
161  #ifndef ONLINE_JUDGE
162  fclose(stdin);
163  #endif
164    return 0;
165}

相关文章

codeforces 29 C. Mail Stamps

http://codeforces.com/contest/29/problem/C 题意:给出n个边的关系,保证可以构成一条链。正向或者反向输出这个链。 思路:由于下标很大(1E9),而关系个数只有1E5..需要离散化。。而且离散化的同时不能丢失边的关系。。。实际上。。直接用vector+map就好了。。。 map >e;即可。然后找到一个度为1的点。。做个dfs…

codeforces 27 C. Unordered Subsequence

·2 分钟
http://codeforces.com/contest/27/problem/C 题意:给出一个序列,问是否存在一个disordered的子序列。。输出长度并输出组成子序列的下表(1..n)。如果有多组,输出任意一组。 disordered的意思是。。升序或者降序(不严格也可以)之外的情况。 思路: 首先我们可以知道,我们要找的子序列至少需要三个点。因为两个点怎么看都是有序的。而如果有k个点(k>3)组成的子序列存在。。那么机智得去掉其中一些点,可以只剩三个 ,同样满足题意。所以我们只需要找到三个点即可。如果把点以下标为横坐标,值为纵坐标花在坐标系上,就是找一个v型或者倒v型的三个点。

codeforces 612 A. The Text Splitting

http://codeforces.com/contest/612/problem/A 水题…直接枚举就好。 1/* *********************************************** 2Author :111qqz 3Created Time :2015年12月25日 星期五 22时58分26秒 4File Name :code/cf/edu4/A.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=105; 34int n,p,q; 35char st[N]; 36bool v[10005]; 37int main() 38{ 39 #ifndef ONLINE_JUDGE 40 freopen("code/in.txt","r",stdin); 41 #endif 42 cin>>n>>p>>q; 43 cin>>st; 44 ms(v,false); 45 int b,c; 46 for ( int i = 0 ; i*p<=105 ; i++ ) 47 { 48 for ( int j = 0 ; j*q <= 105 ; j++) 49 { 50 v[i*p+j*q] = true; 51 if (i*p+j*q==n) 52 { 53 b = i; 54 c = j; 55 } 56 } 57 } 58// cout<<"b:"<<b<<endl; 59// cout<<"c:"<<c<<endl; 60 if (!v[n]) 61 { 62 puts("-1"); 63 } 64 else 65 { 66 int cnt = 0 ; 67 printf("%d\n",b+c); 68 for ( int i = 1 ; i <= b ; i++) 69 { 70 for ( int i = 0 ; i < p ; i++) 71 printf("%c",st[cnt]),cnt++; 72 printf("\n"); 73 } 74 for ( int i =1 ; i <= c ; i++) 75 { 76 for ( int i = 0 ; i < q ; i++) 77 printf("%c",st[cnt]),cnt++; 78 printf("\n"); 79 } 80 } 81 82 83 84 #ifndef ONLINE_JUDGE 85 fclose(stdin); 86 #endif 87 return 0; 88}

codeforces #336 div 2 A. Saitama Destroys Hotel

·1 分钟
http://codeforces.com/contest/608/problem/A 题意:一个电梯,从s到0层,单项,给出n个人,每个人在某时间出现在某层,问要花多长时间把所有人运到0。初始电梯在s,每下一层话费时间1,上来人不花时间。 思路:由于电梯只能是单项,那么到达某一层的时候,一定要等到把这层中最晚来的那个接走。所以排序的时候按照楼层高到低为第一关键字,等待时间长到短为第二关键字。对于同一楼层出现的,只考虑第一个即可,也就是最后出现的。需要注意的是最后接完最后一个人以后把电梯运行道0层。

codeforces 466 C. Number of Ways

·2 分钟
http://codeforces.com/problemset/problem/466/C 题意:给定一个序列。要将序列分成三个非零的连续部分,使得三部分的和相等。问有多少中分法。 思路:首先可以知道,如果是序列的和不为3的倍数,那么一定无解,输出0.设序列的和为sum,那么每一部分的和就应该为sum/3。我们可以预处理出从1开始的和为sum/3的点(我开了数组表示前缀和。。想了下其实不用。。我只需要点的信息。。所以用一个变量表示即可),将点的下标存在p[i]里。对于每一个p[i],我想要知道比p[i]大且补与p[i]相邻的点中,有多少个j,使得从j到n的和为sum/3。因为如果有两部分的和都为sum/3,那么剩下的那部分也一定为sum/3.然后要知道有多少个满足题意的j,我们可以从后往前扫一遍,标记从n开始往前扫,和为sum/3的点,可以用一个0,1数组表示。如果和为sum/3,那么标记为1,否则为0.然后再用一个类似前缀和的思路。再开一个数组c记录从j到n有的和为多少,也就是从j到n有多少个点满足该点到n的和为sum/3.