Codeforces Round #419 C
题意:给你一个矩阵,有2种操作,1是将某一行全部减1,2 是将某一列全部减1,问你最少多少次操作可以使得矩阵全部为0,并输出每次的操作,如果不可能,输出-1
思路:判断行和列的大小,如果行数大于列数,则优先进行操作1,否则优先进行操作2,将每次操作存入字符串,最后判断是否全部为0,xjb模拟就是了
AC代码:
#include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "map" #include "algorithm" #include "stdio.h" #include "math.h" #define bug(x) cout<<x<<" "<<"UUUUU"<<endl; #define mem(a) memset(a,0,sizeof(a)) using namespace std; const int N=2e5+100; int n,m,g[105][105],ans,l[105],r[105],ll=1; char anss[350005]; int main(){ cin>>n>>m; memset(l,111,sizeof(l)); memset(r,111,sizeof(r)); for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j){ scanf("%d",&g[i][j]); l[i]=min(l[i],g[i][j]); } } if(m>=n){ for(int i=1; i<=n; ++i){ for(int k=1; k<=l[i]; ++k){ ans++; anss[ll++]='r',anss[ll++]='o',anss[ll++]='w',anss[ll++]=' ',anss[ll++]=i,anss[ll++]='\n'; } for(int j=1; j<=m; ++j){ g[i][j]-=l[i]; } } for(int j=1; j<=m; ++j){ for(int i=1; i<=n; ++i){ r[j]=min(r[j],g[i][j]); } } for(int j=1; j<=m; ++j){ for(int k=1; k<=r[j]; ++k){ ans++; anss[ll++]='c',anss[ll++]='o',anss[ll++]='l',anss[ll++]=' ',anss[ll++]=j,anss[ll++]='\n'; } for(int i=1; i<=n; ++i){ g[i][j]-=r[j]; } } } else{ for(int j=1; j<=m; ++j){ for(int i=1; i<=n; ++i){ r[j]=min(r[j],g[i][j]); } } for(int j=1; j<=m; ++j){ for(int k=1; k<=r[j]; ++k){ ans++; anss[ll++]='c',anss[ll++]='o',anss[ll++]='l',anss[ll++]=' ',anss[ll++]=j,anss[ll++]='\n'; } for(int i=1; i<=n; ++i){ g[i][j]-=r[j]; } } for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j){ l[i]=min(l[i],g[i][j]); } } for(int i=1; i<=n; ++i){ for(int k=1; k<=l[i]; ++k){ ans++; anss[ll++]='r',anss[ll++]='o',anss[ll++]='w',anss[ll++]=' ',anss[ll++]=i,anss[ll++]='\n'; } for(int j=1; j<=m; ++j){ g[i][j]-=l[i]; } } } for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j){ if(g[i][j]>0){ cout<<"-1"; return 0; } } } anss[ll]='\0'; cout<<ans<<endl; for(int i=0; i<ans; ++i){ for(int j=1; j<=4; ++j){ printf("%c",anss[i*6+j]); } int kk=anss[i*6+5]; printf("%d%c",kk,anss[i*6+6]); } return 0; }