codeforces-237【B构造】【C二分】
来源:互联网 发布:指向字符串数组的指针 编辑:程序博客网 时间:2024/05/16 14:48
题目链接:点击打开链接
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:
- for all i, j (1 < i ≤ n; 1 ≤ j ≤ ci) holds ai, j > ai - 1, j;
- 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.
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.
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.
33 2 14 3 56 12
21 1 2 22 1 3 1
144 3 2 1
21 1 1 41 2 1 3
思路:全 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;}
题目链接:点击打开链接
#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;}
- codeforces-237【B构造】【C二分】
- Codeforces Round #403 B 二分,C , 染色
- Codeforces 360B + Codeforces 689C ( 二分 + DP )
- CodeForces 302B 二分
- CodeForces 600B 二分
- Codeforces 650B 二分
- Codeforces 580 B-----二分
- Codeforces 702B【二分】
- codeforces-760【B二分】
- Codeforces 760B (二分)
- Codeforces 671B 二分
- codeforces-782【B二分】
- Codeforces 237B Young Table 【构造】
- codeforces 225B B. Well-known Numbers(数论+二分+贪心+构造)
- Codeforces 605B 构造
- Codeforces 639B (构造)
- CodeForces 359B 构造
- Codeforces Round #377 (Div. 2)A,B,C,D【二分】
- hdu 2066 一个人的旅行 (多源最短路 Floyd)
- Android—ImageView的使用
- English考研句子第四句
- Android笔记2
- CSS3 3D 转换
- codeforces-237【B构造】【C二分】
- B-1001
- 解决Windows10移动硬盘显示2个
- matlab 打出入仓出仓点
- SecureCRT 连接 linux 服务器
- Android Java与JS的通信
- WebRTC PeerConnection
- 笔试题
- leetcode 2. Add Two Numbers