codeforces-237【B构造】【C二分】

来源:互联网 发布:指向字符串数组的指针 编辑:程序博客网 时间:2024/05/16 14:48

题目链接:点击打开链接

B. Young Table
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci cells, at that for all i (1 < i ≤ n) holdsci ≤ ci - 1.

Let's denote s as the total number of cells of table a, that is, . We know that each cell of the table contains a single integer from 1 to s, at that all written integers are distinct.

Let's assume that the cells of the i-th row of table a are numbered from 1 to ci, then let's denote the number written in the j-th cell of thei-th row as ai, j. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions:

  1. for all i, j (1 < i ≤ n; 1 ≤ j ≤ ci) holds ai, j > ai - 1, j;
  2. for all i, j (1 ≤ i ≤ n; 1 < j ≤ ci) holds ai, j > ai, j - 1.

In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap.

Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than s. You do not have to minimize the number of operations.

Input

The first line contains a single integer n (1 ≤ n ≤ 50) that shows the number of rows in the table. The second line contains n space-separated integers ci (1 ≤ ci ≤ 50; ci ≤ ci - 1) — the numbers of cells on the corresponding rows.

Next n lines contain table а. The i-th of them contains ci space-separated integers: the j-th integer in this line represents ai, j.

It is guaranteed that all the given numbers ai, j are positive and do not exceed s. It is guaranteed that all ai, j are distinct.

Output

In the first line print a single integer m (0 ≤ m ≤ s), representing the number of performed swaps.

In the next m lines print the description of these swap operations. In the i-th line print four space-separated integers xi, yi, pi, qi(1 ≤ xi, pi ≤ n; 1 ≤ yi ≤ cxi; 1 ≤ qi ≤ cpi). The printed numbers denote swapping the contents of cells axi, yi and api, qi. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed.

Examples
input
33 2 14 3 56 12
output
21 1 2 22 1 3 1
input
144 3 2 1
output
21 1 1 41 2 1 3
大意:给你 n 行,每行有 c[ i ] 个数,每次你可以交换任意两个不同的数。现在要求每行非降序排列、每列非降序排列。让你输出交换方案。

思路:全 sort 下,因为 m 限制为 m <= s,那么我们从左到右、从上到下扫一遍就可以了。

#include<cstdio>#include<cstring>#include<algorithm>#include<map>using namespace std;typedef pair<int,int> p;map<int,p> M;int n,a[55];int table[55][55];int order[5050],ans[5050][4];int main(){while(~scanf("%d",&n)){M.clear();for(int i=1;i<=n;i++)scanf("%d",a+i);int num=0;for(int i=1;i<=n;i++){for(int j=1;j<=a[i];j++){scanf("%d",&table[i][j]);order[num++]=table[i][j];M[table[i][j]]=p(i,j);}}sort(order,order+num);int k=0,cnt=0;for(int i=1;i<=n;i++){for(int j=1;j<=a[i];j++){if(order[k]!=table[i][j]){ans[cnt][0]=i;ans[cnt][1]=j;int nx=M[order[k]].first;int ny=M[order[k]].second;ans[cnt][2]=nx;ans[cnt++][3]=ny;M[table[i][j]]=p(nx,ny);swap(table[i][j],table[nx][ny]);}k++;}}printf("%d\n",cnt);for(int i=0;i<cnt;i++)printf("%d %d %d %d\n",ans[i][0],ans[i][1],ans[i][2],ans[i][3]);}return 0;}


题目链接:点击打开链接

C. Primes on Interval
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors.

Consider positive integers aa + 1...b (a ≤ b). You want to find the minimum integer l (1 ≤ l ≤ b - a + 1) such that for any integer x(a ≤ x ≤ b - l + 1) among l integers xx + 1...x + l - 1 there are at least k prime numbers.

Find and print the required minimum l. If no value l meets the described limitations, print -1.

Input

A single line contains three space-separated integers a, b, k (1 ≤ a, b, k ≤ 106a ≤ b).

Output

In a single line print a single integer — the required minimum l. If there's no solution, print -1.

Examples
input
2 4 2
output
3
input
6 13 1
output
4
input
1 4 3
output
-1

大意:在区间 [ a ,b ] 找出一个最小的区间长度 l ,使得区间 [ a ,b ]长度为 l 的子区间,含有至少 k 个质数

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int MAXN=1e6+10;int a,b,k;bool shu[MAXN]={1,1};int num[MAXN];bool judge(int x){x--; // 题中说的是子区间 x,x+1,...,x+l-1 for(int i=a;i<=b-x;i++){if(num[i+x]-num[i]+(!shu[i])<k)return 0;}return 1;}int solve(int x,int y){int ans=0,mid,l=1,r=y-x+1;while(l<=r){mid=l+r>>1;if(judge(mid)){ans=mid;r=mid-1;}elsel=mid+1;}return ans;}int main(){for(int i=2;i<MAXN;i++){if(shu[i])continue;for(int j=i*2;j<MAXN;j+=i)shu[j]=1;}num[1]=0;for(int i=2;i<MAXN;i++){num[i]=num[i-1]+(!shu[i]);}while(~scanf("%d%d%d",&a,&b,&k)){if(num[b]-num[a]+(!shu[a])<k)puts("-1");elseprintf("%d\n",solve(a,b));}return 0;}

0 0
原创粉丝点击