fork download
  1. #include <stdio.h>
  2. int main()
  3. {
  4. long long int n, m, p, i, j, total_cost, cost;
  5. scanf( "%lld %lld %lld", &n, &m, &p );
  6. long long int ar[n][m];
  7. for( i = 1; i <= n; ++i ){
  8. for( j = 1; j <= m; ++j ){
  9. ar[i][j] = j;
  10. }
  11. }
  12. while( p-- ){
  13. scanf( "%lld %lld", &i, &j );
  14. ar[i][j] += 1;
  15. }
  16. /*
  17.   printf("\n");
  18.   for( i = 1; i <= n; ++i ){
  19.   for( j = 1; j <= m; ++j ){
  20.   printf("%d ", ar[i][j]);
  21.   }
  22.   printf("\n");
  23.   }
  24.   printf("\n");
  25.   */
  26. if( n == 1 && m == 1 ){
  27. printf( "0\n" );
  28. return 0;
  29. }
  30. if( m == 1 ){
  31. for( i = 1; i <= n; ++i ){
  32. printf( "0\n" );
  33. }
  34. return 0;
  35. }
  36.  
  37. for( i = 1; i <= n; ++i ){
  38. total_cost = 0, cost = 0;
  39. for( j = m; j >= 2; --j ){
  40. cost = ar[i][j] - ar[i][j - 1];
  41. if( cost < 0 ){
  42. printf( "-1\n" );
  43. break;
  44. }
  45. total_cost += cost;
  46. }
  47. if( cost >= 0 ){
  48. printf( "%lld\n", total_cost );
  49. }
  50. }
  51. return 0;
  52. }
Success #stdin #stdout 0s 3300KB
stdin
4 4 6
2 2
3 2 
3 2 
4 3
4 4
4 3
stdout
-1
-1
-1
-1