#419(div2) C. Karen and Game

题意:给出一个n*m的矩阵,然后我们可以每一行-1,每一列-1,问是否可以全部变成0

思路:最开始的时候马上就想到了无论怎样,他每一行该减去的时候无论先后都要减去,那么我每一行取一个最小值减去,然后每一列取最小值减去,然后判断是否全部为0,然后学弟给了我一组数据,3 2 2 2 1 1 2 2,题要求最少步骤,那么我们那样就不可以,所以得判断下n和m的大小,n大先做列,那样就减去的数字更多了,m大反之亦然。(手速狗靠这涨了一波大分,o(* ̄▽ ̄*)ブ)

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3
  4 int a[105][105];
  5 int hang[105],lie[105];
  6 int n,m;
  7 struct node{
  8     int x,y;
  9 }e[200002];
 10
 11 int main(){
 12     memset(hang,127,sizeof(hang));
 13     memset(lie,127,sizeof(lie));
 14     scanf("%d%d",&n,&m);
 15     int l=0;
 16     for(int i=1;i<=n;i++)
 17         for(int j=1;j<=m;j++) scanf("%d",&a[i][j]);
 18     if(n<=m){
 19
 20         for(int i=1;i<=n;i++){
 21         for(int j=1;j<=m;j++){
 22         hang[i]=min(hang[i],a[i][j]);
 23         }
 24     }
 25     for(int i=1;i<=n;i++){
 26          if(hang[i]>0){
 27             for(int j=1;j<=m;j++)
 28             {
 29                 a[i][j]-=hang[i];
 30
 31             }
 32             while(hang[i]--){
 33                 e[l].x=1;e[l++].y=i;
 34             }
 35
 36          }
 37     }
 38     for(int i=1;i<=m;i++){
 39         for(int j=1;j<=n;j++){
 40             lie[i]=min(lie[i],a[j][i]);
 41         }
 42     }
 43     for(int i=1;i<=m;i++){
 44         if(lie[i]>0){
 45             for(int j=1;j<=n;j++)
 46             {
 47                 a[j][i]-=lie[i];
 48
 49             }
 50             while(lie[i]--){
 51                 e[l].x=2;e[l++].y=i;
 52             }
 53
 54         }
 55     }
 56     int sum=0;
 57     for(int i=1;i<=n;i++)
 58         for(int j=1;j<=m;j++)
 59         if(a[i][j]==0) sum++;
 60     if(sum!=n*m){
 61         printf("-1\n");
 62     }
 63     else {
 64             printf("%d\n",l);
 65         for(int i=0;i<l;i++){
 66             if(e[i].x==1) printf("row ");
 67             else printf("col ");
 68             printf("%d\n",e[i].y);
 69         }
 70       }
 71     }
 72     else {
 73
 74             for(int i=1;i<=m;i++){
 75         for(int j=1;j<=n;j++){
 76             lie[i]=min(lie[i],a[j][i]);
 77         }
 78     }
 79     for(int i=1;i<=m;i++){
 80         if(lie[i]>0){
 81             for(int j=1;j<=n;j++)
 82             {
 83                 a[j][i]-=lie[i];
 84
 85             }
 86             while(lie[i]--){
 87                 e[l].x=2;e[l++].y=i;
 88             }
 89
 90         }
 91     }
 92          for(int i=1;i<=n;i++){
 93         for(int j=1;j<=m;j++){
 94         hang[i]=min(hang[i],a[i][j]);
 95         }
 96      }
 97     for(int i=1;i<=n;i++){
 98          if(hang[i]>0){
 99             for(int j=1;j<=m;j++)
100             {
101                 a[i][j]-=hang[i];
102
103             }
104             while(hang[i]--){
105                 e[l].x=1;e[l++].y=i;
106             }
107
108          }
109     }
110
111     int sum=0;
112     for(int i=1;i<=n;i++)
113         for(int j=1;j<=m;j++)
114         if(a[i][j]==0) sum++;
115     if(sum!=n*m){
116         printf("-1\n");
117     }
118     else {
119             printf("%d\n",l);
120         for(int i=0;i<l;i++){
121             if(e[i].x==1) printf("row ");
122             else printf("col ");
123             printf("%d\n",e[i].y);
124         }
125       }
126     }
127 }
时间: 2024-08-09 08:15:04

#419(div2) C. Karen and Game的相关文章

#419(div2) B. Karen and Coffee

题意:给出n个温度区间,k,Q个询问,每个询问给出一个温度区间x--y.问这之间有多少个温度在给出K的温度区间内. 思路:前缀和小技巧 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 3e5+10; 4 5 int n,k,q,x,y,a[N],b[N]; 6 7 int main() { 8 scanf("%d%d%d",&n,&k,&q); 9 for(int i

#419(div2) A. Karen and Morning

题意:给出某个时刻,问多少秒后时刻变成回文的了 思路:A题就直接暴力咯,一秒一秒加,判断,注意:24点为0点 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main(){ 5 string s1; 6 cin>>s1; 7 int x1=s1[0]-'0'; 8 int x2=s1[1]-'0'; 9 int y1=s1[3]-'0'; 10 int y2=s1[4]-'0'; 11 for(int i=0;;i+

code force #419(div2)C. Karen and Game

On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or

cf386(div2)大一狗ACM之路

#cf386(div2)总结#前两题很顺利的做了出来, c题扔了, D题wrong了5发才A掉.A题签到题, 但是想多了, 代码写的有点长了. 找被整除最小值*7.B题 读题读了一会, 读完了就有思路了, 1A. 字符串问题, 从后往前两个两个的放到新的字符串里, 一个从最左, 一个从最右, 模拟指针扫着放, 最后特判会不会扫到一起.C题跳了没看, 最后做完了D题回来看了一眼没什么思路 日后再说.D题, 恩.. 两个多小时都用在这题上面了, 20分钟的时候做完了B之后就一直再啃D题, 暴力判断啊

CF #262 (DIV2) C . Present (二分答案)

output standard output Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on

Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形DP)

题目链接:Codeforces Round #419 (Div. 2) E. Karen and Supermarket 题意: 有n件物品,每个物品有一个价格,和一个使用优惠券的价格,不过这个优惠券有一个限制,必须要在第x个使用后才可以使用.现在有m的钱,问最多能买多少个物品. 题解: 每个优惠券都只与一个券有关,所以根据这个关系就可以构成一棵树. 考虑树形dp,dp[i][j][k(0|1)]表示第i个节点所构成的子树中买了j个物品,使用优惠券和不使用优惠券的最少钱. 转移方程看代码详细解释

#282(div2) C. Treasure

题意:#可以变换成>=1个')',问每个#可以变换成多少个')'.使得整个字符串正常,否则输出-1. 思路:我们可以先把可以消掉的()消掉,再判断下比如#在新的字符串最前面或者(在最后面是不行的,然后我们让前面的#都变成一个),那么最后一个可以变成剩下的. 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int b[100005]; 5 int main(){ 6 string s; 7 cin>>s; 8 int l

Codeforces Round #326(Div2)

CodeForces 588A 题意:Duff喜欢吃肉,想在接下来的n天,每天都有Ai斤肉吃,但每一天肉的单价Pi不定,肉 可以保存不过期,现已知n天每天肉的斤数Ai,以及单价Pi,为了使每天都             有想要的Ai斤肉吃,求最小花费.  思路:cost=Ai*min(pi)  1<=i<=n; 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using

Codeforces Round #419 (Div. 2)C. Karen and Game

C. Karen and Game 给定n行m列数字,每次可以让一行或一列都减一,求出让全部数字全为0的最小的次数,没有则输出-1: 比赛时没有考虑,n和m的大小问题,被hack了.5555555555555555555555555555 好的方法没有想到,只有一个死方法了. 1 #include <iostream> 2 #include <stdio.h> 3 #define long long ll 4 using namespace std; 5 int a[110][11