Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
struct v
{
int i,j;
};
v a[5000];
int n;
long long dp[5000][5002];
bool comp(v x, v y)
{
return((x.i+x.j)<(y.i+y.j));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int i,j,k,r=1;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i].i>>a[i].j;
sort(a,a+n,comp);
dp[0][1]=a[0].j;
dp[0][2]=1e13;
for(i=1;i<n;i++)
{
k=i+2;
for(j=1;j<k;j++)
{
dp[i][j]=1e13;
if(dp[i-1][j-1]<=a[i].i)
{
dp[i][j]=dp[i-1][j-1]+a[i].j;
r=max(r,j);
}
dp[i][j]=min(dp[i][j],dp[i-1][j]);
}
dp[i][j]=1e13;
}
cout<<r;
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
static const int64_t INF=100000000000000;
int main(){
int N;cin>>N;
vector<tuple<int64_t,int64_t,int64_t>>A(N);
for(int i=0;i<N;i++){
int64_t h,p;cin>>h>>p;
tuple<int64_t,int64_t,int64_t>q(h+p,h,p);
A.at(i)=q;
}sort(A.begin(),A.end());
vector<pair<int64_t,int64_t>>B(N);
for(int i=0;i<N;i++){
int64_t h=get<1>(A.at(i));
int64_t p=get<2>(A.at(i));
pair<int64_t,int64_t>q(h,p);
B.at(i)=q;
}vector<vector<int64_t>>dp(1+N,vector<int64_t>(1+N,INF));
for(int i=0;i<=N;i++)dp[i][0]=0;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++){
int64_t h=B[i-1].first;
int64_t p=B[i-1].second;
if(dp[i][j-1]!=INF){
if(dp[i-1][j-1]<=h)
dp[i][j]=min(dp[i-1][j-1]+p,dp[i-1][j]);
else
dp[i][j]=dp[i-1][j];
}
}int64_t ans=0;
for(int j=N;0<=j;j--)
if(dp[N][j]!=INF){ans=j;break;}
cout<<ans<<endl;
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct person
{
int h, p;
bool operator<(const person a) const
{
return h + p < a.h + a.p;
}
} a[5001];
int n;
long long dp[5005];
void checkmin(long long &x, long long y)
{
if (x > y)
{
x = y;
}
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &a[i].h, &a[i].p);
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++)
{
dp[i] = 1e18;
}
for (int i = 1; i <= n; i++)
{
for (int j = i; j >= 1; j--)
{
if (dp[j - 1] <= a[i].h)
{
checkmin(dp[j], dp[j - 1] + a[i].p);
}
}
}
for (int i = n; i >= 1; i--)
{
if (dp[i] < 1e18)
{
printf("%d", i);
return 0;
}
}
return 0;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | // https://cf17-final.contest.atcoder.jp/tasks/cf17_final_d
#include<iostream>
#include<vector>
#include<algorithm>
int main(){
std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
int nPlayer;std::cin>>nPlayer;
struct player{int height,power;};
std::vector<player> players(nPlayer);
for(player& p:players)std::cin>>p.height>>p.power;
std::sort(players.begin(),players.end(),[](player a,player b){
return a.height+a.power<b.height+b.power;
});
std::vector<long long> minHeight(nPlayer+1,1e18);
minHeight[0]=0;
for(player p:players){
auto it=std::upper_bound(minHeight.begin(),minHeight.end(),p.height);
while(it!=minHeight.begin()){
*it=std::min(*it,p.power+*std::prev(it));
--it;
}
}
std::cout<<std::lower_bound(minHeight.begin(),minHeight.end(),1e18)-minHeight.begin()-1<<'\n';
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
const int N=5005;
long long dp[N][N];
struct P{
long long h;
long long p;
}a[N];
bool cmp(P a,P b){
return a.h+a.p<b.h+b.p;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].h>>a[i].p;
}
sort(a+1,a+n+1,cmp);
memset(dp,0x3f,sizeof(dp));
dp[0][0]=0;
for(int i=1;i<=n;i++){
dp[i][0]=0;
for(int j=1;j<=i;j++){
dp[i][j]=dp[i-1][j];
if(dp[i-1][j-1]<=a[i].h)
{
dp[i][j]=min(dp[i-1][j-1]+a[i].p,dp[i][j]);
}
}
}
int ans=0;
long long inf=1e12;
for(int i=5000;i>=0;i--){
if(dp[n][i]<=inf){
ans=i;
break;
}
}
cout<<ans;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;
typedef pair<LL,LL>P;
LL H[5001];
LL PP[5001];
int main(){
int N;
cin>>N;
vector<LL> dp(N+1);
REP(i,N+1){
dp[i]=1e15;
}
vector<P>v;
REP(i,N){
cin>>H[i]>>PP[i];
v.push_back(P(H[i]+PP[i],i));
}
sort(v.begin(),v.end());
dp[0]=0;
REP(i,N){
vector<LL>nxt(N+1);
REP(j,N+1)nxt[j]=dp[j];
REP(j,N){
int now=v[i].second;
if(dp[j]<=H[now]){
nxt[j+1]=min(nxt[j+1],dp[j]+PP[now]);
}
}
swap(dp,nxt);
}
int ans=0;
REP(i,N+1){
if(dp[i]<1e15)ans=i;
}
cout<<ans<<endl;
return(0);
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int n;
struct node
{
int h,p,val;
bool operator <(const node &a) const
{
return val < a.val;
}
}a[5005];
long long dp[5005];
int main()
{
scanf("%d",&n);
for (int i = 1; i <= n; i++)
{
scanf("%d%d",&a[i].h,&a[i].p);
a[i].val = a[i].h + a[i].p;
}
sort(a + 1,a + n + 1);
dp[0] = 0;
for (int i = 1; i <= n; i++)
{
dp[i] = 0x7fffffffffffffff;
}
for (int i = 1; i <= n; i++)
{
for (int j = i; j >= 1; j--)
{
if (dp[j - 1] <= a[i].h) dp[j] = min(dp[j],dp[j - 1] + a[i].p);
}
}
int ans = -1;
for (int i = 0; i <= n; i++)
{
if (dp[i] < 0x7fffffffffffffff) ans = i;
}
printf("%d",ans);
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define INF 1000000000000000LL
#define N 5010
int n;
PII a[N];
ll f[N], g[N]; // min height
bool cmp(PII x, PII y) {
return x.fi + x.se < y.fi + y.se || x.fi + x.se == y.fi + y.se && x.se < y.se;
}
int main() {
cin >> n;
for (int i = 0; i < n; i ++)
cin >> a[i].fi >> a[i].se;
sort(a, a+n, cmp);
for (int i = 0; i <= n; i ++) f[i] = INF;
f[0] = 0;
for (int i = 0; i < n; i ++) {
memcpy(g, f, sizeof g);
for (int j = 0; j <= n; j ++) f[j] = INF;
for (int j = 0; j <= n; j ++) if (g[j] < INF) {
if (g[j] <= a[i].fi) f[j+1] = min(f[j+1], g[j] + a[i].se);
f[j] = min(f[j], g[j]);
}
}
int S = 0;
for (int i = 0; i <= n; i ++)
if (f[i] < INF) S = max(S, i);
cout << S << endl;
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | /*input
4 3
1 2 6
3 4 5
2 3 7
*/
import java.util.Scanner;
import java.lang.*;
import java.util.Arrays;
import java.util.function.BinaryOperator;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
class value {
long h, p;
value(long h, long p) {
this.h = h;
this.p = p;
}
}
class cmp implements Comparator<value> {
public int compare(value a, value b) {
return (int)(a.h + a.p) - (int)(b.h + b.p);
}
}
public class Main {
private static final int N = (int)5e3 + 100;
private static final long oo = (long) 1e18;
static int n;
static value[] a = new value[N];
static Comparator<Long> CmpLong = new Comparator<Long>() {
public int compare(Long x, Long y) {
return (int)(y - x);
}
};
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
n = inp.nextInt();
for(int i = 1; i <= n; i += 1) {
long h = inp.nextLong();
long p = inp.nextLong();
a[i] = new value(h, p);
}
ArrayList<value> tmp = new ArrayList<value>();
for(int i = 1; i <= n ; ++i) tmp.add(a[i]);
Collections.sort(tmp, new cmp());
PriorityQueue<Long> pq = new PriorityQueue<Long>(CmpLong);
long sum = 0L, ans = 0L;
for(value v: tmp) {
sum += v.p;
pq.add(v.p);
while(sum > v.h + v.p) {
sum -= pq.remove();
}
ans = Math.max(ans, pq.size());
}
System.out.print(ans);
}
} | JAVA |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
const int INF=2e9+10;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N; cin >> N;
vector<pair<int,int>> Z;
for (int i=0;i<N;++i){
int H,P; cin >> H >> P;
Z.emplace_back(H+P,P);
}
sort(Z.begin(),Z.end());
vector<vector<int>> dp(N+1,vector<int>(N+1,INF));
dp[0][0]=0;
for (int i=0;i<N;++i){
int H=Z[i].first-Z[i].second,P=Z[i].second;
for (int j=0;j<=N;++j){
dp[i+1][j]=min(dp[i+1][j],dp[i][j]);
if (j+1<=N&&dp[i][j]<=H) dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+P);
}
}
int ans=0;
for (int j=0;j<=N;++j) if (dp[N][j]!=INF) ans=j;
cout << ans << '\n';
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
struct v
{
int i,j;
};
v a[5000];
int n;
long long dp[5000][5002];
bool comp(v x, v y)
{
if((x.i+x.j)==(y.i+y.j))
return(x.j<y.j);
return((x.i+x.j)<(y.i+y.j));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int i,j,k,r=1;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i].i>>a[i].j;
sort(a,a+n,comp);
dp[0][1]=a[0].j;
dp[0][2]=1e13;
for(i=1;i<n;i++)
{
k=i+2;
for(j=1;j<k;j++)
{
dp[i][j]=1e13;
if(dp[i-1][j-1]<=a[i].i)
{
dp[i][j]=dp[i-1][j-1]+a[i].j;
r=max(r,j);
}
dp[i][j]=min(dp[i][j],dp[i-1][j]);
}
dp[i][j]=1e13;
}
cout<<r;
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
#define LL long long
#define pii pair<int,int>
#define pll pair<LL,LL>
using namespace std;
int main(){
int N;cin>>N;
pll p[N];
REP(i,N){
LL H,P;cin>>H>>P;
p[i].first=P+H,p[i].second=H;
}
sort(p,p+N);
LL dp[N+1][N+1];
fill(dp[0],dp[N+1],INT_MAX);
dp[0][0]=0;
REP(i,N){
REP(j,i+1){
dp[i+1][j+1]=dp[i][j+1];
if(p[i].second >=dp[i][j]){
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j] + (p[i].first-p[i].second));
}
}
}
int ans =0;
REP(i,N+1)if(dp[N][i]<INT_MAX)ans=i;
cout<<ans<<endl;
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | //love yjl forever
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const ll INF=4611686018427387904,N=5000+5;
int n;
struct node{
int x,y;
}a[N];
bool cmp(node x,node y)
{
return x.x+x.y<y.x+y.y;
}
ll dp[N][N];
int main()
{
/* freopen("","r",stdin);
freopen("","w",stdout);*/
int i,j;
cin>>n;
for(i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+1,a+n+1,cmp);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
dp[i][j]=INF;
dp[0][0]=0;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
{
dp[i][j]=dp[i-1][j];
if(a[i].x>=dp[i-1][j-1])
dp[i][j]=min(dp[i][j],dp[i-1][j-1]+a[i].y);
}
for(i=n;i>=0;i--)
if(dp[n][i]<INF)
{
cout<<i;
return 0;
}
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
int n;
pii p[5555];
bool cmp(pii p1,pii p2){
return p1.fi+p1.se < p2.fi+p2.se;
}
ll f[5050];
int main(){
cin>>n;
for (int i=1;i<=n;++i) cin>>p[i].fi>>p[i].se;
sort(p+1,p+n+1,cmp);
memset(f,33,sizeof f); f[0]=0;
for (int i=1;i<=n;++i){
for (int j=n;j;--j) if (f[j-1]<=p[i].fi)
f[j]=min(f[j],f[j-1]+p[i].se);
}
for (int i=n;;--i) if (f[i]<(1e15)) return !(cout<<i);
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
#define INF 200100100100100100
#define MOD 1000000007
int N;
int H[5001],P[5001];
vector<pair<int,int>>v;
ll dp[5001][5001]; //processed, used
int main()
{
//ios_base::sync_with_stdio(0);cin.tie(0);
//freopen (".in","r",stdin);
//freopen (".out","w",stdout);
cin>>N;
for (int i=0;i<N;i++){
cin>>H[i]>>P[i];
v.push_back({H[i],P[i]});
}
sort(v.begin(),v.end(),[](pair<int,int>p1, pair<int,int>p2){return p1.first+p1.second<p2.first+p2.second;});
for (int i=0;i<=N;i++)
for (int j=0;j<=N;j++)
dp[i][j]=INF;
dp[0][0]=0;
int ans=0;
for (int i=0;i<N;i++){
for (int j=0;j<=i;j++){
if (dp[i][j]<=v[i].first)
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+v[i].second);
dp[i+1][j]=min(dp[i+1][j],dp[i][j]);
}
}
for (int i=0;i<=N;i++)
for (int j=0;j<=N;j++)
if (dp[i][j]!=INF) ans=max(ans,j);
cout<<ans<<endl;
return 0;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
#define MOD 1000000007
#define INF 4557430888798830399
#define int long long
#define pb push_back
#define in(s) freopen(s,"r",stdin);
#define out(s) freopen(s,"w",stdout);
#define fi first
#define se second
#define bw(i,r,l) for (int i=r-1;i>=l;i--)
#define fw(i,l,r) for (int i=l;i<r;i++)
#define fa(i,x) for (auto i:x)
using namespace std;
const int N = 5005;
struct cont {
int h, p;
bool operator<(const cont &rhs) const {
return h + p < rhs.h + rhs.p;
}
} c[N];
int n;
priority_queue<int> pq;
signed main() {
#ifdef aome
in("aome.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n;
fw (i, 0, n) cin >> c[i].h >> c[i].p;
sort(c, c + n);
int cur = 0;
fw (i, 0, n) {
cur += c[i].p;
pq.push(c[i].p);
if (cur > c[i].p + c[i].h) {
cur -= pq.top();
pq.pop();
}
}
cout << pq.size();
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll inf=(ll)1e16+7;
int n;
ll f[5002];
struct po{
int h,p;
bool operator <(const po &rsh)const
{
return h+p<rsh.h+rsh.p;
}
}a[5001];
int main()
{
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].h>>a[i].p;
f[i]=inf;
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
for(int j=n;j>=1;j--)
if(f[j-1]<=a[i].h) f[j]=min(f[j-1]+a[i].p,f[j]);
for(;n;n--) if(f[n]!=inf) break;
cout<<n<<endl;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
using Int = long long;
int main() {
int N; cin >> N;
vector<tuple<int, int, int>> A(N);
for (int i = 0; i < N; i++) {
int h, p; cin >> h >> p;
A[i] = make_tuple(h + p, h, p);
}
sort(begin(A), end(A));
const Int INF = 1e18;
vector<Int> dp(N+1, INF), dp2;
dp[0] = 0;
for (int i = 0; i < N; i++) {
dp2 = dp;
int h = get<1>(A[i]), p = get<2>(A[i]);
for (int j = 0; j < N; j++) {
if (dp2[j] == INF || dp2[j] > h) {
break;
}
dp[j+1] = min(dp[j+1], dp2[j] + p);
}
}
int ans = 0;
for (int i = 0; i <= N; i++) {
if (dp[i] != INF) {
ans = i;
}
}
cout << ans << '\n';
return 0;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<bits/stdc++.h>
#define int long long
using namespace std;
struct Node{
int x, y;
bool operator < (const Node &b) const {
return x + y < b.x + b.y;
}
}a[10010];
int n, f[5010][5010];
signed main(void) {
scanf("%lld", &n);
for(int i = 1; i <= n; i++)
scanf("%lld%lld", &a[i].x, &a[i].y);
sort(a + 1, a + n + 1);
memset(f, 0x3f3f, sizeof(f));
f[0][0] = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j <= i; j++) {
f[i + 1][j] = min(f[i][j], f[i + 1][j]);
if(f[i][j] <= a[i + 1].x)
f[i + 1][j + 1] = min(f[i + 1][j + 1], f[i][j] + a[i + 1].y);
}
}
// printf("%d\n", f[2][2]);
for(int j = n; j >= 0; j--)
if(f[n][j] != 0x3f3f3f3f3f3f3f3f) {
printf("%lld\n", j);
return 0;
}
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;
class Main {
public static void main(String[] args) {
// long time = System.currentTimeMillis();
new Main().run();
// System.err.println(System.currentTimeMillis() - time);
}
void run() {
Scanner sc = new Scanner();
int n = sc.nextInt();
long[][] a = new long[n][2];
for (int i = 0; i < n; ++i) {
a[i][0] = sc.nextLong();
a[i][1] = sc.nextLong();
}
Arrays.sort(a, new Comparator<long[]>() {
@Override
public int compare(long[] o1, long[] o2) {
return Long.compare(o1[0] + o1[1], o2[0] + o2[1]);
}
});
long INF = Long.MAX_VALUE / 3;
long[] f = new long[n + 1];
Arrays.fill(f, INF);
f[0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = n - 1; j >= 0; --j) {
if (f[j] == INF || f[j] > a[i][0])
continue;
f[j + 1] = Math.min(f[j + 1], f[j] + a[i][1]);
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (f[i] == INF)
break;
ans = i;
}
System.out.println(ans);
}
class Scanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
void tr(Object... objects) {
System.out.println(Arrays.deepToString(objects));
}
} | JAVA |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<cstdio>
#include<algorithm>
using namespace std;
struct stu
{
int h,p;
}a[5005];
int n,i,j,dp[5005];
bool cmp(stu a,stu b){return a.h+a.p<b.h+b.p;}
int main()
{
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%d%d",&a[i].h,&a[i].p);
sort(a+1,a+n+1,cmp);
dp[0]=0;
for(i=1;i<=n;i++)dp[i]=2000000007;
for(i=1;i<=n;i++)for(j=n-1;j>=0;j--)if(dp[j]<=a[i].h)dp[j+1]=min(dp[j+1],dp[j]+a[i].p);
for(i=0;i<=n;i++)if(dp[i]==2000000007)break;
printf("%d\n",i-1);
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define int ll
#define fi first
#define se second
#define pii pair<int, int>
template<class T> void smin(T& a, T val) {if (a > val) a = val;}
using namespace std;
const int N = 5*1e3 + 10;
const int inf = 1e18;
int dp[N], n;
pii v[N];
int32_t main(){_
cin>>n;
for (int i = 1; i <= n; ++i)
{
cin>>v[i].fi>>v[i].se;
v[i].fi += v[i].se;
}
sort(v+1, v+n+1);
for (int j = 0; j <= n; ++j)
{
dp[j] = inf;
}
dp[0] = 0;
for (int i = 1; i <= n; ++i)
{
v[i].fi -= v[i].se;
int H = v[i].fi;
int P = v[i].se;
for (int j = i; j >= 1; --j)
{
if(H >= dp[j-1]){
smin(dp[j], dp[j-1] + P);
}
}
}
int best = 0;
for (int j = 1; j <= n; ++j)
{
if(dp[j] != inf){
best = j;
}
}
cout<<best;
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5010;
struct data {
int h, p;
bool operator < (const data &other) const {
return h + p < other.h + other.p;
}
} a[maxn];
ll dp[maxn];
int n, res;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d %d", &a[i].h, &a[i].p);
dp[i] = 1e18;
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
for (int j = n; j >= 1; j--) {
if (dp[j - 1] <= a[i].h) {
dp[j] = min(dp[j], dp[j - 1] + a[i].p);
}
}
}
for (int i = 1; i <= n; i++) {
if (dp[i] != 1e18) {
res = i;
}
}
printf("%d\n", res);
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long int ll;
ll dp[5050][5050];
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<ll> h(n),p(n);
vector<pair<ll,int>> v;
for(int i=0;i<n;i++){
cin >> h[i] >> p[i];
v.push_back({h[i]+p[i],h[i]});
}
sort(v.begin(),v.end());
for(int i=0;i<5050;i++){
for(int j=1;j<5050;j++){
dp[i][j]=1e12;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(dp[i][j]<=v[i].second){
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+v[i].first-v[i].second);
}
dp[i+1][j]=min(dp[i][j],dp[i+1][j]);
}
}
int ma=0;
for(int i=0;i<=n;i++){
if(dp[n][i]<1e12){
ma=i;
}
}
cout << ma << endl;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 5000 + 10;
const long long INF = (long long)(1e15);
struct Box {
int w, s;
};
int n;
Box a[MAXN];
long long f[MAXN][MAXN];
bool cmp(Box &a, Box &b) {
return a.w + a.s < b.w + b.s;
}
void minmize(long long &a, long long b) {
a = min(a, b);
}
int main() {
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i].s >> a[i].w;
sort(a + 1, a + n + 1, cmp);
for(int i = 0; i <= n; ++i)
for(int j = 0; j <= n; ++j)
f[i][j] = INF;
f[0][0] = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j <= i; j++)
if (f[i][j] < INF) {
// not put i+1
minmize(f[i + 1][j], f[i][j]);
// put i+1
if (a[i + 1].s >= f[i][j])
minmize(f[i + 1][j + 1], f[i][j] + a[i + 1].w);
}
int res = 0;
for(int i = 0; i <= n; ++i)
if (f[n][i] < INF)
res = i;
cout << res << "\n";
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | N = int(input())
A = []
for i in range(N):
h,p = list(map(int, input().split()))
A.append([h+p,h,p])
A.sort()
# N人積んだ時の最小の高さH
dp = [-1 for i in range(N+1)]
dp[0] = 0
for i in range(N):
_,h,p = A[i]
for j in range(N, 0, -1):
## まだj-1人積んだことがない
if dp[j-1] < 0:
continue
## 高すぎてつめない
if dp[j-1] > h:
continue
if dp[j] < 0:
dp[j] = dp[j-1] + p
else:
dp[j] = min(dp[j], dp[j-1] + p)
for i in range(N+1):
if dp[i] < 0:
print(i-1)
exit(0)
print(N)
| PYTHON3 |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | //重要:「積まなくてもよい」と言い換えても答えは変わらない。積まないを選んだ人が後ろにいけばいいだけなので
//「積む」順番はh + pが小さい順が良い。だからこの順番で並べれば、前の人から「積む」「積まない」を試す感じになる。
#include <iostream>
#include <algorithm>
#include <tuple>
#define int long long
using namespace std;
typedef tuple<int, int, int> T;
int n;
int h[5000], p[5000];
T t[5000];
int dp[5001][5001];
signed main() {
int i, j;
cin >> n;
for (i = 0; i < n; i++) cin >> h[i] >> p[i];
for (i = 0; i < n; i++) t[i] = T(h[i] + p[i], h[i], p[i]);
sort(t, t + n);
for (i = 0; i < n; i++) { h[i] = get<1>(t[i]); p[i] = get<2>(t[i]); }
for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) dp[i][j] = 1145141919810;
dp[0][0] = 0;
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
if (h[i] >= dp[i][j]) {
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + p[i]);
}
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
}
}
for (j = n; j > 0; j--) { if (dp[n][j] <= 11451419190) break; }
cout << j << endl;
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
DZabuton solver = new DZabuton();
solver.solve(1, in, out);
out.close();
}
static class DZabuton {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
IntIntPair[] a = in.readIntPairArray(n);
Arrays.sort(a, Comparator.comparing(ii -> ii.first + ii.second));
long[] dp = new long[n + 1];
Arrays.fill(dp, Constants.LINF);
dp[0] = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = n - 1; j >= 0; j--) {
if (dp[j] <= a[i].first) {
dp[j + 1] = Math.min(dp[j + 1], dp[j] + a[i].second);
ans = Math.max(ans, j + 1);
}
}
}
out.printLine(ans);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public IntIntPair[] readIntPairArray(int size) {
IntIntPair[] result = new IntIntPair[size];
for (int i = 0; i < size; i++) {
result[i] = readIntPair();
}
return result;
}
public IntIntPair readIntPair() {
int first = readInt();
int second = readInt();
return new IntIntPair(first, second);
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class IntIntPair implements Comparable<IntIntPair> {
public final int first;
public final int second;
public IntIntPair(int first, int second) {
this.first = first;
this.second = second;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IntIntPair pair = (IntIntPair) o;
return first == pair.first && second == pair.second;
}
public int hashCode() {
int result = first;
result = 31 * result + second;
return result;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public int compareTo(IntIntPair o) {
int value = Integer.compare(first, o.first);
if (value != 0) {
return value;
}
return Integer.compare(second, o.second);
}
}
static class Constants {
public static final long LINF = (long) 1e18 + 1;
}
}
| JAVA |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) FOR(i,0,n)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define UM unordered_map
#define US unordered_set
#define ALL(a) (a).begin(),(a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll,ll> pii;
typedef vector<pii> vpii;
int main(){
ll N; cin>>N;
vpii A(N); REP(i,N) cin>>A[i].first>>A[i].second;
{
auto comp=[](pii a,pii b){
// if(a.first==b.first) return a.second<b.second;
// return a.first>b.first;
if(a.first+a.second==b.first+b.second) return a.first>b.first;
return a.first+a.second>b.first+b.second;
};
sort(ALL(A),comp);
}
vvi dp(N,vi(N+1,-INF));
REP(i,N) dp[i][0]=INF;
dp[0][1]=A[0].first;
FOR(i,1,N) for(ll j=i+1;j>0;j--) {
dp[i][j]=max(dp[i-1][j],min(dp[i-1][j-1]-A[i].second,A[i].first));
}
ll ans=0;
REP(i,N+1) {
if(dp[N-1][i]>=0) ans=i;
}
cout<<ans<<endl;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
struct Participant{
int h, p;
bool operator<(const Participant& obj)const{
if(p != obj.p)return h+p < obj.h+obj.p;
else return p < obj.p;
}
};
long long int dp[5003][5003] = {0};
int main(){
int n;
cin >> n;
vector<Participant> v;
v.push_back((Participant){0, 0});
for(int i=0;i<n;++i){
int h, p;
cin >> h >> p;
v.push_back((Participant){h, p});
}
sort(v.begin(), v.end());
for(int i=0;i<=n;++i){
dp[i][0] = 0;
for(int j=1;j<=n;++j){
dp[i][j] = LLONG_MAX;
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=i;++j){
dp[i][j-1] = min(dp[i][j-1], dp[i-1][j-1]);
if(dp[i-1][j-1] <= v[i].h){
dp[i][j] = min(dp[i][j], dp[i-1][j-1] + v[i].p);
}
}
}
int ans = 0;
for(int i=1;i<=n;++i){
if(dp[n][i] != LLONG_MAX)ans = i;
}
cout << ans << endl;
return 0;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=5003;
const ll inf=0x3f3f3f3f3f3f3f3f;
struct data{
int h,p;
}a[maxn];
bool cmp(data x,data y){
return x.h+x.p<y.h+y.p;
}
ll dp[maxn][maxn];
int n;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d%d",&a[i].h,&a[i].p);
sort(a+1,a+n+1,cmp);
memset(dp,0x3f,sizeof(dp));
dp[0][0]=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=i;++j){
if(dp[i-1][j-1]>a[i].h)
dp[i][j]=dp[i-1][j];
else dp[i][j]=min(dp[i-1][j],dp[i-1][j-1]+a[i].p);
// printf("dp[%d][%d]=%d\n",i,j,dp[i][j]);
}
}
for(int i=n;i>=1;--i)
if(dp[n][i]<inf){
printf("%d\n",i);
return 0;
}
return 0;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | import java.util.*;
import java.io.*;
import static java.lang.System.in;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] hp = new int[n][];
for(int i=0;i<n;i++) hp[i]=new int[]{sc.nextInt(),sc.nextInt()};
Arrays.sort(hp,new myC());
int inf = Integer.MAX_VALUE;
int[][] dp = new int[n+1][n+1];
// dp[i][j] the minimum height when choosing j people from the first i people (1-base);
for(int i=0;i<n+1;i++) Arrays.fill(dp[i],inf);
for(int i=0;i<n+1;i++) dp[i][0]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
dp[i][j] = Math.min(dp[i][j],dp[i-1][j]);
if(dp[i-1][j-1]<=hp[i-1][0]) dp[i][j] = Math.min(dp[i][j],dp[i-1][j-1]+hp[i-1][1]);
}
}
int ans = 0;
for(int i=1;i<=n;i++){
if(dp[n][i]<inf) ans = Math.max(ans,i);
}
System.out.println(ans);
}
static class myC implements Comparator<int[]>{
public int compare(int[] a, int[] b){
if(a[0]+a[1]-b[0]-b[1]!=0) return a[0]+a[1]-b[0]-b[1];
else return a[0]-b[0];
}
}
}
| JAVA |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include"bits/stdc++.h"
using namespace std;
const int N=5e3+100;
typedef long long LL;
struct node{
LL h,p;
}a[N];
LL dp[N];
bool cmp(node a,node b){
if(a.h+a.p==b.h+b.p)return a.h<b.h;
return a.h+a.p<b.h+b.p;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<=5050;i++)dp[i]=0x3f3f3f3f3f3f3f3f;
for(int i=1;i<=n;i++)
scanf("%d %d",&a[i].h,&a[i].p);
sort(a+1,a+n+1,cmp);
dp[0]=0;
for(int i=1;i<=n;i++){
for(int j=i-1;j>=0;j--){
if(a[i].h>=dp[j]){
dp[j+1]=min(dp[j+1],dp[j]+a[i].p);
}
}
}
int ans=1;
for(int i=2;i<=n;i++)
if(dp[i]!=0x3f3f3f3f3f3f3f3f)ans=i;
printf("%d\n",ans);
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | N = int(input())
HP = [tuple(map(int,input().split())) for _ in range(N)]
HP.sort(key=lambda x: sum(x))
INF = float('inf')
dp = [0]
maxj = 0
for i,(h,p) in enumerate(HP):
dp2 = [INF] * (maxj + 2)
for j in range(maxj,-1,-1):
dp2[j] = min(dp2[j], dp[j])
if dp[j] <= h:
dp2[j+1] = min(dp2[j+1], dp[j] + p)
if j == maxj:
maxj += 1
dp = dp2
print(maxj) | PYTHON3 |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <chrono>
using namespace std;
#define INF 1e18
long long DP[5001][5001];
struct Constraint {
long long H, P;
Constraint(long long H = 0, long long P = 0):H(H), P(P){}
bool operator<(const Constraint& r)const {
return P+H < r.P+r.H;
}
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N; cin >> N;
vector<Constraint>C(N);
for (int i = 0; i < N; i++) {
int P, H; cin >> H >> P;
C[i] = Constraint(H, P);
}
sort(C.begin(), C.end());
for (int i = 0; i <= N; i++)for (int j = 0; j <= N; j++)DP[i][j] = INF;
DP[0][0] = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < i + 1; j++) {
if (DP[i][j] == INF)continue;
if (DP[i][j] <= C[i].H) {
DP[i + 1][j + 1] = min(DP[i + 1][j + 1], DP[i][j] + C[i].P);
}
DP[i + 1][j] = min(DP[i + 1][j], DP[i][j]);
}
}
int ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (DP[i][j]==0 || DP[i][j] == INF)continue;
ans = max(ans, j);
}
}
cout << ans << endl;
return 0;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int N;
vector<pair<int,int> >A;
long dp[5050];
int main()
{
cin>>N;
for(int i=0;i<N;i++)
{
int h,p;cin>>h>>p;
A.push_back(make_pair(h,p));
dp[i+1]=9e18;
}
sort(A.begin(),A.end(),[](pair<int,int>x,pair<int,int>y){return x.first+x.second<y.first+y.second;});
for(int i=0;i<N;i++)
{
for(int j=N;j>=0;j--)
{
if(dp[j]<=A[i].first)
{
dp[j+1]=min(dp[j+1],dp[j]+A[i].second);
}
}
}
int ans=0;
while(ans+1<=N&&dp[ans+1]<9e18)ans++;
cout<<ans<<endl;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 5010;
struct Node
{
int h, p;
}a[maxN];
bool cmp(Node x, Node y)
{
return x.p - y.h < y.p - x.h;
}
int n;
ll dp[maxN];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d %d", &a[i].h, &a[i].p);
sort(a + 1, a + n + 1, cmp);
for (int i = 0; i <= 5000; i++) dp[i] = 1e16;
dp[0] = 0;
for (int i = 1; i <= n; i++)
for (int j = i - 1; j >= 0; j--)
if (a[i].h >= dp[j])
dp[j + 1] = min(dp[j + 1], dp[j] + a[i].p);
int ans = 1;
for (int i = 2; i <= n; i++)
if (dp[i] != 1e16)
ans = i;
printf("%d\n", ans);
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
DZabuton solver = new DZabuton();
solver.solve(1, in, out);
out.close();
}
static class DZabuton {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
IntIntPair[] a = in.readIntPairArray(n);
Arrays.sort(a, Comparator.comparing(ii -> ii.first + ii.second));
long[] dp = new long[n + 1];
Arrays.fill(dp, Constants.LINF);
dp[0] = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = ans; j >= 0; j--) {
if (dp[j] <= a[i].first) {
dp[j + 1] = Math.min(dp[j + 1], dp[j] + a[i].second);
ans = Math.max(ans, j + 1);
}
}
}
out.printLine(ans);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public IntIntPair[] readIntPairArray(int size) {
IntIntPair[] result = new IntIntPair[size];
for (int i = 0; i < size; i++) {
result[i] = readIntPair();
}
return result;
}
public IntIntPair readIntPair() {
int first = readInt();
int second = readInt();
return new IntIntPair(first, second);
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class IntIntPair implements Comparable<IntIntPair> {
public final int first;
public final int second;
public IntIntPair(int first, int second) {
this.first = first;
this.second = second;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IntIntPair pair = (IntIntPair) o;
return first == pair.first && second == pair.second;
}
public int hashCode() {
int result = first;
result = 31 * result + second;
return result;
}
public String toString() {
return "(" + first + "," + second + ")";
}
public int compareTo(IntIntPair o) {
int value = Integer.compare(first, o.first);
if (value != 0) {
return value;
}
return Integer.compare(second, o.second);
}
}
static class Constants {
public static final long LINF = (long) 1e18 + 1;
}
}
| JAVA |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define INF 1000000000000000000
typedef long long LL;
struct person{
LL h;
LL p;
bool operator<(const struct person & right) const{
return (h+p<right.h+right.p);
}
};
int main(){
int n;
cin >> n;
vector<struct person> san(n);
vector<LL> dp(n+1,INF);
dp[0]=0;
for(int i=0;i<n;i++){
cin >> san[i].h >> san[i].p;
}
sort(san.begin(),san.end());
for(int i=0;i<n;i++){
for(int j=n;j>0;j--){
if(dp[j-1]<=san[i].h) dp[j]=min(dp[j],dp[j-1]+san[i].p);
}
// for(int j=0;j<=n;j++){
// cout << dp[j] << " ";
// }
// cout << endl;
}
for(int i=n;i>=0;i--){
if(dp[i]!=INF){
cout << i << endl;
return 0;
}
}
return 0;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
struct person{
long long h;
long long p;
};
int main(){
int n;
int ans=0;
std::vector<person> list;
std::vector<long long> dpList;
std::cin>>n;
list.resize(n);
dpList.resize(n+1, -1);
dpList[0]= 0;
for(int i=0; i<n; i++)
std::cin>>list[i].h>>list[i].p;
std::stable_sort(list.begin(), list.end(), [](const person& x, const person& y){return x.p+ x.h< y.p + y.h;});
for(int i=0; i<n; i++){
for(int j=i; j>=0; j--){
if(dpList[j]==-1)
continue;
if(dpList[j]>list[i].h)
continue;
if(dpList[j+1]==-1)
dpList[j+1]=dpList[j]+list[i].p;
else
dpList[j+1]=std::min(dpList[j]+list[i].p, dpList[j+1]);
}
}
for(int i=n; i>=0; i--){
if(dpList[i]!=-1){
ans= i;
break;
}
}
std::cout<<ans<<std::endl;
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
const int N=5010;
const ll INF=1e18;
#define N 5005
int n;
struct node{
ll h,p;
}a[N];
ll dp[N];
int ans;
bool cmp(node x,node y){
return x.h+x.p<y.h+y.p;
}
int main(){
scanf("%d",&n);
for (int i=1; i<=n; i++)
scanf("%d%d",&a[i].h,&a[i].p);
sort(a+1,a+n+1,cmp);
for (int i=1; i<=n; i++)
dp[i]=INF;
dp[0]=0;
for (int i=1; i<=n; i++)
for (int j=i; j>=1; j--)
if (dp[j-1]<=a[i].h)
dp[j]=min(dp[j],dp[j-1]+a[i].p);
for (int i=0; i<=n; i++)
if (dp[i]<INF) ans=i;
printf("%d\n",ans);
return 0;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <iostream>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <set>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
#define f first
#define s second
typedef long long LLI;
int n;
pair<int, pair<int, int> > p[5001];
long long dp[5001][5001];
const long long inf = 1e15;
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d %d", &p[i].s.f, &p[i].s.s);
p[i].f = p[i].s.f + p[i].s.s;
}
sort(p, p + n);
for(int i = 0; i < 5001; ++i) {
dp[i][0] = 0;
for(int j = 1; j < 5001; ++j) dp[i][j] = inf;
}
int ans = 1;
dp[0][1] = p[0].s.s;
for(int i = 1; i < n; ++i) {
for(int j = 1; j <= n; ++j) {
dp[i][j] = dp[i-1][j];
if(p[i].s.s + dp[i-1][j-1] < dp[i][j] && p[i].s.f >= dp[i-1][j-1]) dp[i][j] = p[i].s.s + dp[i-1][j-1];
if(dp[i][j] < inf) ans = max(ans, j);
}
}
printf("%d\n", ans);
return 0;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define A first
#define B second
ll n;
pii s[5005];
ll dp[5005][5005];
ll inf = (1LL << 60);
// in the first m participants, if k people go, min value of h
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s[i].A >> s[i].B;
s[i].A += s[i].B;
}
for (int i = 0; i < n; ++i)
for (int j = 0; j <= 5000; ++j)
dp[i][j] = inf;
sort(s, s+n);
for (int i = 0; i < n; ++i)
s[i].A -= s[i].B;
for (int i = 0; i < n; ++i)
for (int k = 0; k <= i+1; ++k) {
if (i == 0) {
dp[0][0] = 0;
dp[0][1] = s[0].B;
continue;
}
dp[i][k] = dp[i-1][k];
if (dp[i-1][k-1] <= s[i].A)
dp[i][k] = min(dp[i][k], dp[i-1][k-1] + s[i].B);
}
for (int i = 5000; i >= 0; --i) {
if (dp[n-1][i] != inf) {
cout << i << endl;
return 0;
}
}
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <bits/stdc++.h>
#include <random>
using namespace std;
#define rep(i, N) for (int i = 0; i < N; i++)
#define pb push_back
typedef long long ll;
typedef pair<int, int> i_i;
const ll INF = LLONG_MAX / 2;
const int MOD = 1e9 + 7;
int main() {
int N; cin >> N;
vector<int> a(N), b(N);
rep(i, N) cin >> a[i] >> b[i];
vector<i_i> ci(N);
rep(i, N) ci[i] = i_i(a[i] + b[i], i);
sort(ci.begin(), ci.end());
vector<int> _a(N), _b(N);
rep(i, N) {
int _i = ci[i].second;
_a[i] = a[_i], _b[i] = b[_i];
}
a = _a, b = _b;
vector<ll> dp(N + 1, INF);
dp[0] = 0;
rep(i, N) {
int x = a[i], y = b[i];
vector<ll> _dp = dp;
rep(z, N + 1) if (dp[z] <= x) _dp[z + 1] = min(_dp[z + 1], dp[z] + y);
dp = _dp;
}
int ans = 0;
rep(z, N + 1) if (dp[z] < INF) ans = z;
cout << ans << endl;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <functional>
#include <numeric>
#include <set>
// #include <bits/stdc++.h>
using namespace std;
typedef long long li;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
li n;
cin >> n;
vector<li> hs(n), ps(n);
priority_queue<pair<li, li>> q;
for (int i = 0; i < n; ++i) {
cin >> hs[i] >> ps[i];
q.emplace(-hs[i] - ps[i], i);
}
const li inf = 1LL << 60;
vector<li> min_tall(n + 1, inf);
min_tall[0] = 0;
li ans = 0;
while (not q.empty()) {
li i = q.top().second; q.pop();
for (int j = n - 1; j >= 0; --j) {
if (min_tall[j] <= hs[i]) {
li next_tall = min_tall[j] + ps[i];
if (next_tall < min_tall[j + 1]) {
min_tall[j + 1] = next_tall;
ans = max(ans, j + 1LL);
}
}
}
}
cout << ans << endl;
return 0;
}
| CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
int N;
ll h[5010],p[5010],dp[5010][5010] = {},inf = 1e18;
vector<P> v;
int main(){
cin >> N;
for(int i=1;i<=N;i++){
cin >> h[i] >> p[i];
v.push_back(P(h[i]+p[i],i));
}
v.push_back(P(0,0));
sort(v.begin(),v.end());
for(int i=0;i<=N;i++){
for(int j=0;j<=N;j++){
dp[i][j] = inf;
}
}
dp[0][0] = 0;
for(int i=1;i<=N;i++){
int id = v[i].second;
for(int j=0;j<=i;j++){
if(j<i && dp[i-1][j]<=h[id]){
dp[i][j+1] = min(dp[i][j+1],dp[i-1][j]+p[id]);
}
dp[i][j] = min(dp[i][j],dp[i-1][j]);
}
}
int ans = 0;
for(int i=0;i<=N;i++) if(dp[N][i]!=inf) ans = i;
cout << ans << endl;
} | CPP |
p03526 CODE FESTIVAL 2017 Final - Zabuton | In the final of CODE FESTIVAL in some year, there are N participants. The height and power of Participant i is H_i and P_i, respectively.
Ringo is hosting a game of stacking zabuton (cushions).
The participants will line up in a row in some order, and they will in turn try to add zabuton to the stack of zabuton. Initially, the stack is empty. When it is Participant i's turn, if there are H_i or less zabuton already stacked, he/she will add exactly P_i zabuton to the stack. Otherwise, he/she will give up and do nothing.
Ringo wants to maximize the number of participants who can add zabuton to the stack. How many participants can add zabuton to the stack in the optimal order of participants?
Constraints
* 1 \leq N \leq 5000
* 0 \leq H_i \leq 10^9
* 1 \leq P_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
H_1 P_1
H_2 P_2
:
H_N P_N
Output
Print the maximum number of participants who can add zabuton to the stack.
Examples
Input
3
0 2
1 3
3 4
Output
2
Input
3
2 4
3 1
4 1
Output
3
Input
10
1 3
8 4
8 3
9 1
6 4
2 3
4 2
9 2
8 3
0 1
Output
5 | 6 | 0 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
Pair[] pairs = new Pair[n];
for (int i = 0; i < n; i++) {
long h = in.nextLong();
long p = in.nextLong();
pairs[i] = new Pair(h, p);
}
Arrays.sort(pairs);
long[] dp = new long[n + 1];
Arrays.fill(dp, Long.MAX_VALUE);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j >= 0; j--) {
if (dp[j] <= pairs[i].x) {
dp[j + 1] = Math.min(dp[j + 1], dp[j] + pairs[i].y);
}
}
}
for (int i = n; i >= 0; i--) {
if (dp[i] != Long.MAX_VALUE) {
out.println(i);
return;
}
}
}
public class Pair implements Comparable<Pair> {
long x;
long y;
Pair(long x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair p) {
return Long.compare(x + y, p.x + p.y);
}
}
}
static class InputReader {
BufferedReader in;
StringTokenizer tok;
public String nextString() {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine(), " ");
} catch (IOException e) {
throw new InputMismatchException();
}
}
return tok.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextString());
}
public long nextLong() {
return Long.parseLong(nextString());
}
public InputReader(InputStream inputStream) {
in = new BufferedReader(new InputStreamReader(inputStream));
tok = new StringTokenizer("");
}
}
}
| JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int H,W,h,w;
int main()
{
cin>>H>>W>>h>>w;
if (H%h==0&&W%w==0)
{
cout<<"No";
return 0;
}
else
{
cout<<"Yes"<<endl;
int t=(H/h)*(W/w);
int x=t+1,y=(t+1)*(h*w-1)+1;
for (int i=1;i<=H;i++)
for (int j=1;j<=W;j++)
{
if (i%h==0&&j%w==0) printf("%d",-y);
else printf("%d",x);
if (j<W) printf(" ");
else printf("\n");
}
}
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int m,n,A,B,f[505][505];
int main()
{
cin>>m>>n>>A>>B;
if(m%A==0&&n%B==0)
{
cout<<"No";
return 0;
}
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
if(i%A==0&&j%B==0)
f[i][j]=-(1000*(A*B-1)+1);
else
f[i][j]=1000;
cout<<"Yes\n";
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
cout<<f[i][j]<<' ';
cout<<"\n";
}
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
int H,W,h,w;
int mass[501][501];
int main() {
cin >> H >> W >> h >> w;
if(H % h == 0 && W % w == 0) {
cout << "No" <<endl;
return 0;
}
cout <<"Yes"<<endl;
for(int i = 1; i <= H; i++) {
for(int j = 1; j <= W; j++) {
if(i % h == 0 && j % w == 0) {
mass[i][j] = -MAX * (w*h-1) - 1;
}
else mass[i][j] = MAX;
}
}
for(int i = 1; i <= H; i++) {
for(int j = 1; j <= W; j++) {
cout << mass[i][j] <<" ";
}
cout <<endl;
}
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #設定
import sys
input = sys.stdin.buffer.readline
#ライブラリインポート
from collections import defaultdict
#入力受け取り
def getlist():
return list(map(int, input().split()))
#処理内容
def main():
H, W, h, w = getlist()
L = [[1] * W for i in range(H)]
if h * w != 1:
x = (10 ** 9 - 1) // (h * w - 1)
for i in range(H):
for j in range(W):
L[i][j] = x
for i in range(H // h):
for j in range(W // w):
L[(i + 1) * h - 1][(j + 1) * w - 1] = - 10 ** 9
s = 0
for i in range(H):
for j in range(W):
s += L[i][j]
if s < 0:
print("No")
return
print("Yes")
for i in range(H):
print(" ".join(list(map(str, L[i]))))
if __name__ == '__main__':
main() | PYTHON3 |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int main(){
int H, W, h, w;
cin>>H>>W>>h>>w;
if(H%h==0 && W%w==0) {
cout<<"No";
return 0;
}
cout<<"Yes"<<'\n';
int pval=(W/w)*(H/h)*2;
int nval=-pval*(w*h-1)-1;
for(int i=1; i<=H; i++) {
for(int j=1; j<=W; j++) {
if(j%w==0 && i%h==0) {
cout<<nval;
} else {
cout<<pval;
}
if(j!=W) {
cout<<" ";
}
}
cout<<'\n';
}
return 0;
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | H, W, h, w = map(int, input().split())
def solve(H, W, h, w):
if (W % w == 0 and H % h == 0):
return False, []
if (W % w == 0):
return True, solve_vertical(H, W, h, w)
return True, solve_horizontal(H, W, h, w)
def solve_horizontal(H, W, h, w):
return [solve_horizontal_core(W, w)] * H
def solve_horizontal_core(W, w):
m, r = divmod(W, w)
row = [0] * W
row[0] = m + 1
row[w - 1] = - m - 2
for i in range(w, W):
row[i] = row[i - w]
return row
def solve_vertical(H, W, h, w):
col = solve_horizontal_core(H, h)
m = []
for c in col:
m.append([c] * W)
return m
yesno, mat = solve(H, W, h, w)
if yesno:
print('Yes')
for row in mat:
print(*row)
else:
print('No')
| PYTHON3 |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | H, W, h, w =list(map(int,input().split()))
INF = 10**10
mat = [[1000 for _ in range(H)] for _ in range(W)]
val =-h*w*1000+ 999
for h_new in range(h-1,H,h):
for w_new in range(w-1,W,w):
mat[w_new][h_new] = val
if (sum(sum(mat[j][i] for i in range(H)) for j in range(W)))< 0:
print("No")
else:
print("Yes")
for i in range(H):
for j in range(W):
print(mat[j][i], end =" ")
print("") | PYTHON3 |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=505;
const int d=1000;
int n,m,h,w,x,y,v;
int a[N][N];
int main(){
int i,j;
scanf("%d%d%d%d",&n,&m,&h,&w);
x=n/h; y=m/w; v=-h*w;
if (n%h==0&&m%w==0){
printf("No\n");
return 0;
}
printf("Yes\n");
for (i=1;i<=x;i++)
for (j=1;j<=y;j++) a[i*h][j*w]=v*d+d-1;
for (i=1;i<=n;i++){
for (j=1;j<=m;j++)
if (a[i][j]) printf("%d ",a[i][j]);
else printf("%d ",d);
printf("\n");
}
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | from sys import exit
H, W, h, w = map(int, input().split())
if not H % h and not W % w:
print('No')
exit()
print('Yes')
G = [[1000]*W for _ in range(H)]
if H % h:
for i in range(H//h):
t = (i+1)*h - 1
for j in range(W):
G[t][j] = -(h-1)*1000 - 1
else:
for i in range(W//w):
t = (i+1)*w - 1
for j in range(H):
G[j][t] = -(w-1)*1000 - 1
for g in G:
print(*g) | PYTHON3 |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | H,W,h,w=map(int,input().split())
p=(10**9-1)//(max(h*w-1,1))
m=p*(h*w-1)+1
vec=[[p for i in range(W+1)] for j in range(H+1)]
sm=0
for i in range(1,H+1):
for j in range(1,W+1):
if i%h==0 and j%w==0:
vec[i][j]=-m
sm+=vec[i][j]
if sm<=0:
print("No")
else:
print("Yes")
for i in range(1,H+1):
for j in range(1,W+1):
print(vec[i][j],end=" \n"[j==W]) | PYTHON3 |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <iostream>
using namespace std;
const int bignum = 100000000;
int main()
{
int H, W, h, w;
cin >> H >> W >> h >> w;
if(H % h == 0 && W % w == 0){
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(i % h == 0 && j % w == 0) cout << bignum + 1 << " ";
else if((i + 1) % h == 0 && (j + 1) % w == 0) cout << -h * w - bignum << " ";
else cout << 1 << " ";
}
cout << endl;
}
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | import java.io.*;
import java.util.*;
public class Main {
void go(int R, int C, int r, int c) {
int cx = 0;
int cy = R * C;
for (int i = r - 1; i < R; i += r) {
for (int j = c - 1; j < C; j += c) {
cx++;
cy--;
}
}
// x > (rc - 1) y
// cx * x < cy * y
if ((r * c - 1) * cx >= cy) {
throw new AssertionError();
}
int x = 2 * cx * (r * c - 1) + 1;
int y = 2 * cx;
out.println("Yes");
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
if (i % r == r - 1 && j % c == c - 1) {
out.print((-x) + " ");
} else {
out.print(y + " ");
}
}
out.println();
}
}
void submit() {
int R = nextInt();
int C = nextInt();
int r = nextInt();
int c = nextInt();
if (R % r == 0 && C % c == 0) {
out.println("No");
return;
}
go(R, C, r, c);
}
void preCalc() {
}
void stress() {
for (int tst = 0;; tst++) {
int R = rand(1, 500);
int C = rand(1, 500);
int r = rand(1, R);
int c = rand(1, C);
if (R % r == 0 && C % c == 0) {
continue;
}
go(R, C, r, c);
System.err.println(tst);
}
}
void test() {
int R = 500;
int C = 500;
int r = 500;
int c = 499;
go(R, C, r, c);
}
Main() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
preCalc();
submit();
// stress();
// test();
out.close();
}
static final Random rng = new Random();
static int rand(int l, int r) {
return l + rng.nextInt(r - l + 1);
}
public static void main(String[] args) throws IOException {
new Main();
}
BufferedReader br;
PrintWriter out;
StringTokenizer st;
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
| JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<cstdio>
#include<algorithm>
using namespace std;
#define INF 1000000000
int N,M,n,m;
int main()
{
scanf("%d%d%d%d",&N,&M,&n,&m);
if(N%n==0&&M%m==0)
{
printf("No\n");
return 0;
}
int k=(INF-1)/(n*m-1);
int l=k*(n*m-1)+1;
l=-l;
printf("Yes\n");
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
if(i%n==0&&j%m==0)
printf("%d ",l);
else printf("%d ",k);
printf("\n");
}
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class Main {
static PrintWriter out;
static InputReader ir;
static void solve() {
int H = ir.nextInt();
int W = ir.nextInt();
int h = ir.nextInt();
int w = ir.nextInt();
if (H % h == 0 && W % w == 0) {
out.println("No");
} else if (H % h == 0) {
int ct = (W / w) * (H / h);
int re = H / h;
int num = ct / re + 1;
int[][] res = new int[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (j % w < W % w) {
res[i][j] = i % h == 0 && j % w == 0 ? num : 0;
} else {
res[i][j] = i % h == 0 && j % w == W % w ? -num - 1 : 0;
}
}
}
out.println("Yes");
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
out.print(res[i][j] + " ");
}
out.println();
}
} else {
int ct = (W / w) * (H / h);
int re = W / w;
int num = ct / re + 1;
int[][] res = new int[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
res[i][j] = (j % w == w - 1 && i % h == 0) ? num
: (j % w == w - 1 && i % h == h - 1) ? -num - 1 : 0;
}
}
out.println("Yes");
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
out.print(res[i][j] + " ");
}
out.println();
}
}
}
public static void main(String[] args) {
ir = new InputReader(System.in);
out = new PrintWriter(System.out);
solve();
out.flush();
}
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
static void tr(Object... o) {
out.println(Arrays.deepToString(o));
}
}
| JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const lim=100000000;
typedef long long ll;
ll s;
int a[601][601],i,j,k,n,m,x,y;
int main(){
scanf("%d%d%d%d",&n,&m,&x,&y);
if (x==1&&y==1){
puts("No");return 0;
}
for (i=1,s=0;i<=n;i++)
for (j=1;j<=m;j++){
if (i%x==0&&j%y==0) a[i][j]=-lim;else{
a[i][j]=(lim-1)/(x*y-1);
if (i%x==1&&j%y==1) a[i][j]+=(lim-1)%(x*y-1);
}
s+=a[i][j];
}
if (s>0){
puts("Yes");
for (i=1;i<=n;i++)
for (j=1;j<=m;j++) printf("%d%c",a[i][j],(j==m)?'\n':' ');
}else puts("No");
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<cstdio>
using namespace std;
int H,W,h,w;
void structure(int id){
switch(id){
case 1:{
const unsigned int HLJ=(19260817)*(25252)*(0x98e);
int HLL=HLJ%998244853;
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
printf("%d%c",
((i%h)||(j%w))?
(
(((i+1)%h)||((j+1)%w))
?(0)
:(-HLL-1)
):HLL,(j+1)!=W?' ':'\n');
}
}
}break;
case 2:{
}break;
case 3:{
}break;
}
}
int main(){
scanf("%d%d%d%d",&H,&W,&h,&w);
if(H%h||W%w)puts("Yes"),structure(1);
else puts("No");
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int main(){
int H, W, h, w;
cin >> H >> W >> h >> w;
if(H % h == 0 && W % w == 0){
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
for(int i = 1; i <= H; i++){
for(int j = 1; j <= W; j++){
if(i % h == 0 && j % w == 0){
cout << -((h * w - 1) * 1000 + 1);
}else{
cout << 1000;
}
if(j != W){
cout << " ";
}
}
cout << endl;
}
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
const int maxn=500+7;
int n,m,H,W;
int main(){
std::cin>>n>>m>>H>>W;
bool h=(n%H>0),w=(m%W>0);
if(!h && !w)return puts("No"),0;
puts("Yes");
if(h){
int k=(n+H-1)/H,x=n*maxn+1,y=k*maxn;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
printf("%d%c",i%H==1 ? x-y : -y," \n"[j==m]);
}else {
int k=(m+W-1)/W,x=m*maxn+1,y=k*maxn;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
printf("%d%c",j%W==1 ? x-y : -y," \n"[j==m]);
}return 0;
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <iostream>
#include <cstdio>
#define N 505
using namespace std;
int H, W, h, w, a[N][N];
int main()
{
int i, j;
cin >> H >> W >> h >> w;
if (H % h == 0 && W % w == 0) {cout << "No"; return 0;}
cout << "Yes" << endl;
if (H % h) {
for (i = 0; i < H; i++) {
for (j = 0; j < W; j++) a[i][j] = i % h ? -1000 : 1000 * h - 1001;
}
} else {
for (i = 0; i < W; i++) {
for (j = 0; j < H; j++) a[j][i] = i % w ? -1000 : 1000 * w - 1001;
}
}
for (i = 0; i < H; i++, puts("")) for (j = 0; j < W; j++) printf("%d ", a[i][j]);
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,N,M,ans[600][600];
int main(){
scanf("%d%d%d%d",&n,&m,&N,&M);
if (n%N==0&&m%M==0){
printf("No\n"); return 0;
}
int a=n%N,b=m%M,tot=0; ans[1][1]=1e8; tot=1e8;
ans[N][M]=-tot-1; printf("Yes\n");
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++) printf("%d ",ans[(i-1)%N+1][(j-1)%M+1]); printf("\n");}
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | import java.io.*;
import java.util.*;
public class Main {
private static List<String> solve(int hL, int wL, int hS, int wS) {
long limit = 1_000_000_000;
long positiveElement = (limit - 1) / (hS * wS);
long negativeElement = -1 * (positiveElement * (hS * wS - 1) + 1);
long countH = hL / hS;
long countW = wL / wS;
long restH = hL % hS;
long restW = wL % wS;
long count = countH * countW;
long rest = restH * wL + restW * hL - restH * restW;
if (positiveElement * rest - count <= 0) {
return Arrays.asList("No");
}
List<String> output = new ArrayList<>();
output.add("Yes");
for (int i = 0; i < hL; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < wL; j++) {
if (i % hS == hS - 1 && j % wS == wS - 1) {
sb.append(negativeElement);
} else {
sb.append(positiveElement);
}
sb.append(" ");
}
output.add(sb.toString());
}
return output;
}
private static void execute(ContestReader reader, PrintWriter out) {
int hL = reader.nextInt();
int wL = reader.nextInt();
int hS = reader.nextInt();
int wS = reader.nextInt();
for (String line : solve(hL, wL, hS, wS)) {
out.println(line);
}
}
public static void main(String[] args) {
ContestReader reader = new ContestReader(System.in);
PrintWriter out = new PrintWriter(System.out);
execute(reader, out);
out.flush();
}
}
class ContestReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
ContestReader(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new java.util.StringTokenizer(reader.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String[] nextArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++) {
array[i] = next();
}
return array;
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++) {
array[i] = nextLong();
}
return array;
}
public double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++) {
array[i] = nextDouble();
}
return array;
}
public int[][] nextIntMatrix(int n, int m) {
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = nextInt();
}
}
return matrix;
}
public long[][] nextLongMatrix(int n, int m) {
long[][] matrix = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = nextLong();
}
}
return matrix;
}
public double[][] nextDoubleMatrix(int n, int m) {
double[][] matrix = new double[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j] = nextDouble();
}
}
return matrix;
}
}
class ModCalculator {
private final long mod;
ModCalculator(long mod) {
this.mod = mod;
}
public long add(long a, long b) {
return (a + b) % mod;
}
public long sub(long a, long b) {
return (a - b + mod) % mod;
}
public long mul(long a, long b) {
return (a * b) % mod;
}
public long pow(long a, long b) {
if (b == 0) {
return 1;
}
long v = pow(mul(a, a), b / 2);
if (b % 2 == 1) {
return mul(v, a);
} else {
return v;
}
}
public long inverse(long a) {
return pow(a, mod - 2);
}
public long div(long a, long b) {
return mul(a, inverse(b));
}
}
class ModCombinationCache {
private final ModCalculator modCalculator;
private final List<Long> factorialCache;
private final List<Long> factorialInverseCache;
public ModCombinationCache(ModCalculator modCalculator) {
this.modCalculator = modCalculator;
factorialCache = new ArrayList<>();
factorialCache.add(1L);
factorialInverseCache = new ArrayList<>();
factorialInverseCache.add(1L);
}
private void resize(int n) {
for (int i = factorialCache.size() - 1; i < n; i++) {
long v = modCalculator.mul(factorialCache.get(i), i + 1);
factorialCache.add(v);
factorialInverseCache.add(modCalculator.inverse(v));
}
}
public long getF(int n) {
resize(n);
return factorialCache.get(n);
}
public long getP(int n, int r) {
resize(n);
return modCalculator.mul(factorialCache.get(n), factorialInverseCache.get(n - r));
}
public long getC(int n, int k) {
resize(n);
return modCalculator.mul(factorialCache.get(n), modCalculator.mul(factorialInverseCache.get(k), factorialInverseCache.get(n-k)));
}
}
class Algorithm {
private static void swap(Object[] list, int a, int b) {
Object tmp = list[a];
list[a] = list[b];
list[b] = tmp;
}
public static <T extends Comparable<? super T>> boolean nextPermutation(T[] ts) {
int rightMostAscendingOrderIndex = ts.length - 2;
while (rightMostAscendingOrderIndex >= 0 &&
ts[rightMostAscendingOrderIndex].compareTo(ts[rightMostAscendingOrderIndex + 1]) >= 0) {
rightMostAscendingOrderIndex--;
}
if (rightMostAscendingOrderIndex < 0) {
return false;
}
int rightMostGreatorIndex = ts.length - 1;
while (ts[rightMostAscendingOrderIndex].compareTo(ts[rightMostGreatorIndex]) >= 0) {
rightMostGreatorIndex--;
}
swap(ts, rightMostAscendingOrderIndex, rightMostGreatorIndex);
for (int i = 0; i < (ts.length - rightMostAscendingOrderIndex - 1) / 2; i++) {
swap(ts, rightMostAscendingOrderIndex + 1 + i, ts.length - 1 - i);
}
return true;
}
public static void shuffle(int[] array) {
Random random = new Random();
int n = array.length;
for (int i = 0; i < n; i++) {
int randomIndex = i + random.nextInt(n - i);
int temp = array[i];
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
}
public static void shuffle(long[] array) {
Random random = new Random();
int n = array.length;
for (int i = 0; i < n; i++) {
int randomIndex = i + random.nextInt(n - i);
long temp = array[i];
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
}
public static void sort(int[] array) {
shuffle(array);
Arrays.sort(array);
}
public static void sort(long[] array) {
shuffle(array);
Arrays.sort(array);
}
}
class UnionFind {
int[] parents;
int[] ranks;
UnionFind(int n) {
parents = new int[n];
ranks = new int[n];
for (int i = 0; i < n; i++) {
parents[i] = i;
}
}
public int getRoot(int index) {
if (parents[index] == index) {
return index;
} else {
parents[index] = getRoot(parents[index]);
return parents[index];
}
}
public boolean sameGroup(int a, int b) {
return getRoot(a) == getRoot(b);
}
public void merge(int a, int b) {
int rootA = getRoot(a);
int rootB = getRoot(b);
if (rootA == rootB) {
return;
}
if (ranks[rootA] < ranks[rootB]) {
parents[rootA] = rootB;
} else if (ranks[rootB] < ranks[rootA]) {
parents[rootB] = rootA;
} else {
parents[rootA] = rootB;
ranks[rootB]++;
}
}
}
| JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<stdio.h>
int main(){
int H,W,h,w,i=1,j;
scanf("%d%d%d%d",&H,&W,&h,&w);
if(H%h||W%w){
puts("Yes");
for(;i<=H;++i,puts("")) for(j=1;j<=W;++j)
printf("%d ",(i%h||j%w)?1000:-1000*(h*w-1)-1);
}else puts("No");
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=505;
const int d=500;
int n,m,h,w,x,y,v;
int a[N][N];
int main(){
int i,j;
scanf("%d%d%d%d",&n,&m,&h,&w);
x=n/h; y=m/w; v=-h*w;
if (n%h==0&&m%w==0){
printf("No\n");
return 0;
}
printf("Yes\n");
for (i=1;i<=x;i++)
for (j=1;j<=y;j++) a[i*h][j*w]=v*d+d-1;
for (i=1;i<=n;i++){
for (j=1;j<=m;j++)
if (a[i][j]) printf("%d ",a[i][j]);
else printf("%d ",d);
printf("\n");
}
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int H,W,h,w,a[505][505];
void work()
{
scanf("%d %d %d %d",&H,&W,&h,&w);
if (H%h)
{
int x=H/h+1,y=-x*(h-1)-1;
for (int i=1; i<=H; i++)
for (int j=1; j<=W; j++)
a[i][j]+=(i%h?x:y);
}
if (W%w)
{
int x=W/w+1,y=-x*(w-1)-1;
for (int i=1; i<=H; i++)
for (int j=1; j<=W; j++)
a[i][j]+=(j%w?x:y);
}
if (!a[1][1]) puts("No"),exit(0);
puts("Yes");
for (int i=1; i<=H; i++,puts(""))
for (int j=1; j<=W; j++)
printf("%d ",a[i][j]);
}
int main()
{
work();
return 0;
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int H, W, h, w;
cin >> H >> W >> h >> w;
if (H % h == 0 && W % w == 0) {
cout << "No\n";
return 0;
}
cout << "Yes\n";
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (i % h == h - 1 && j % w == w - 1) {
cout << -2500 * (h * w - 1) - 1 << " ";
} else {
cout << 2500 << " ";
}
}
cout << "\n";
}
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <cstdio>
#include <cstring>
#define MAXN 510
#define LL long long
int W,H,w,h;
LL a[MAXN][MAXN];
int main(){
scanf("%d%d%d%d",&H,&W,&h,&w);
if(w*h==1){
puts("No");
return 0;
}
LL x=999999999/(h*w-1);
LL s=0;
for(int i=1;i<=H;i++)
for(int j=1;j<=W;j++){
if(i%h || j%w) a[i][j]=x;
else a[i][j]=-(x*(w*h-1)+1);
s+=a[i][j];
}
if(s<=0) puts("No");
else{
puts("Yes");
for(int i=1;i<=H;i++){
for(int j=1;j<=W;j++)
printf("%lld ",a[i][j]);
puts("");
}
}
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int main() {
int H, W, h, w, Swap = 0;
cin >> H >> W >> h >> w;
if(H % h == 0 && W % w == 0) {
puts("No");
} else {
puts("Yes");
int val = (h * w - 1) * 2333 + 1;
for(int i = 1; i <= H; i++) {
for(int j = 1; j <= W; j++) {
if(i % h == 0 && j % w == 0)
printf("%d ", -val);
else
printf("2333 ");
}
puts("");
}
}
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <cstdio>
const int N = 500 + 10;
int n, m, x, y, a[N][N];
int main() {
scanf("%d%d%d%d", &n, &m, &x, &y);
if (n % x == 0 && m % y == 0) {
puts("No");
return 0;
}
puts("Yes");
a[0][0] = 999999999;
a[x - 1][y - 1] = -1000000000;
for (int i = 0; i < n; ++i, putchar('\n'))
for (int j = 0; j < m; ++j)
printf("%d ", a[i % x][j % y]);
return 0;
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int main()
{
int H,W,h,w;
scanf("%d%d%d%d",&H,&W,&h,&w);
if (H%h==0 && W%w==0)
{
printf("No");
return 0;
}
printf("Yes\n");
for (int i=0;i<H;i++)
{
for (int j=0;j<W;j++)
{
if (i%h==0 && j%w==0)
printf("%d ",(int)1e9-1);
else if (i%h==h-1 && j%w==w-1)
printf("%d ",-(int)1e9);
else
printf("0 ");
}
printf("\n");
}
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
H, W, h, w = map(int, input().split())
v = [[0]*W for _ in range(H)]
if H%h==0 and W%w==0:
print("No")
else:
for i in range(H//h):
for j in range(W//w):
v[(i+1)*h-1][(j+1)*w-1] = -10**9
for i in range(H//h+1):
for j in range(W//w+1):
if i*h<H and j*w<W:
v[(i)*h][(j)*w] = 10**9-1
ss = sum(sum(vv) for vv in v)
if ss>0:
print("Yes")
write("\n".join(" ".join(map(str, vv)) for vv in v))
else:
print("No") | PYTHON3 |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | // package atcoder.agc.agc016;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int r = in.nextInt();
int c = in.nextInt();
int h = in.nextInt();
int w = in.nextInt();
if (r % h == 0 && c % w == 0) {
out.println("No");
out.flush();
return;
}
int mcnt = (r / h) * (c / w);
int space = r * c - ((r / h) * h) * ((c / w) * w);
long plusNumber = ((mcnt + space) / space) + 1;
long minusNumber = -(plusNumber * (h * w - 1) + 1);
out.println("Yes");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
long num = ((i+1) % h == 0 && (j+1) % w == 0) ? minusNumber : plusNumber;
if (j >= 1) {
out.print(' ');
}
out.print(num);
}
out.println();
}
out.flush();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int[] nextInts(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
private int[][] nextIntTable(int n, int m) {
int[][] ret = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ret[i][j] = nextInt();
}
}
return ret;
}
private long[] nextLongs(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++) {
ret[i] = nextLong();
}
return ret;
}
private long[][] nextLongTable(int n, int m) {
long[][] ret = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ret[i][j] = nextLong();
}
}
return ret;
}
private double[] nextDoubles(int n) {
double[] ret = new double[n];
for (int i = 0; i < n; i++) {
ret[i] = nextDouble();
}
return ret;
}
private int next() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public char nextChar() {
int c = next();
while (isSpaceChar(c))
c = next();
if ('a' <= c && c <= 'z') {
return (char) c;
}
if ('A' <= c && c <= 'Z') {
return (char) c;
}
throw new InputMismatchException();
}
public String nextToken() {
int c = next();
while (isSpaceChar(c))
c = next();
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = next();
} while (!isSpaceChar(c));
return res.toString();
}
public int nextInt() {
int c = next();
while (isSpaceChar(c))
c = next();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c-'0';
c = next();
} while (!isSpaceChar(c));
return res*sgn;
}
public long nextLong() {
int c = next();
while (isSpaceChar(c))
c = next();
long sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c-'0';
c = next();
} while (!isSpaceChar(c));
return res*sgn;
}
public double nextDouble() {
return Double.valueOf(nextToken());
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
}
| JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
const int N=550;
int n,m,h,w,a[N][N];
int main(){
cin>>n>>m>>h>>w;
if (n%h==0&&m%w==0) puts("No");
else{
puts("Yes");
for (int i=1; i<=n; ++i){
for (int j=1; j<=m; ++j){
if (i%h==0&&j%w==0)
a[i][j]=-(h*w-1)*2000-1;
else
a[i][j]=2000;
printf("%d ",a[i][j]);
}
printf("\n");
}
}
return 0;
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | H, W, h, w = map(int, input().split())
if H % h == 0 and W % w == 0:
print('No')
exit()
rem = H * W - (H - H % h) * (W - W % w)
x = (H // h * W // w) // rem + 1
a = [[x] * W for _ in range(H)]
for i in range(h-1, H, h):
for j in range(w-1, W, w):
a[i][j] = -(h * w - 1) * x - 1
print('Yes')
for row in a:
print(*row)
| PYTHON3 |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int main() {
long hh,ww,h,w,a=0;
cin >> hh >> ww >> h >> w;
vector<vector<long>> ans(hh,vector<long>(ww));
for(long i=0;i<hh;i++) {
for(long j=0;j<ww;j++) {
if(i%h+j%w==0) ans[i][j]=999999999,a+=999999999;
else if((i+1)%h+(j+1)%w==0) ans[i][j]=-1000000000,a-=1000000000;
}
}
if(a>0&&h*w>1) {
cout << "Yes" << endl;
for(long i=0;i<hh;i++) {
for(long j=0;j<ww;j++) cout << ans[i][j] << " ";
cout << endl;
}
} else {
cout << "No" << endl;
}
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<bits/stdc++.h>
int n,m,w,h,ans;
#define x *
int main(){
scanf("%d%d%d%d",&n,&m,&w,&h);
if(n%w==0&&m%h==0)return puts("No"),0;
puts("Yes");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)printf("%d%c",((i%w)||(j%h))?-1926:1926*(w x h-1)-1,j==m-1?'\n':' ');
}
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=505,INF=1e9;
int M[MAXN][MAXN];
int H,W,h,w;
int main()
{
scanf("%d %d %d %d",&H,&W,&h,&w);
if(H%h==0&&W%w==0)
{
printf("No\n");
return 0;
}
printf("Yes\n");
int k=(INF-1)/(h*w-1),f=0;
f=k*(h*w-1)+1;
f=-f;
for(int i=1;i<=H;i++)
{
for(int j=1;j<=W;j++)
{
if(i%h==0&&j%w==0)
printf("%d ",f);
else
printf("%d ",k);
}
printf("\n");
}
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Pavel Mavrin
*/
public class Main {
private void solve() throws IOException {
int H = nextInt();
int W = nextInt();
int h = nextInt();
int w = nextInt();
if (H % h == 0 && W % w == 0) {
out.println("No");
} else {
out.println("Yes");
int s = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
int c = 1000;
if (i % h == h - 1 && j % w == w - 1) {
c = -1000 * (h * w - 1) - 1;
}
s += c;
out.print(c + " ");
}
out.println();
}
System.err.println(s);
}
}
// ------------------
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
PrintWriter out = new PrintWriter(System.out);
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static void main(String[] args) throws IOException {
new Main().run();
}
private void run() throws IOException {
solve();
out.close();
}
}
| JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
void fail(){
cout << "No" << endl;
exit(0);
}
int main(){
int H, W, h, w;
cin >> H >> W >> h >> w;
if(H%h==0 && W%w==0) fail();
int MX = 1e9-1;
int a = MX/(h*w-1);
cout << "Yes" << endl;
for(int i=0; i<H; i++) for(int j=0; j<W; j++){
int v = a;
if(i%h==h-1 && j%w==w-1) v = -a*(h*w-1)-1;
cout << v << " \n"[j==W-1];
}
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
int N,M,n,m;
int main()
{int i,j;
scanf("%d%d%d%d",&N,&M,&n,&m);
if(N%n==0 && M%m==0)
{ printf("No\n");
return 0;
}
printf("Yes\n");
for(i=1;i<=N;i++)
for(j=1;j<=M;j++)
{ if((n==1 || i%n==1) && (m==1 || j%m==1))
printf("%d",inf-1);
else if((n==1 || i%n==0) && (m==1 || j%m==0))
printf("%d",-inf);
else
printf("%d",0);
printf("%c",j!=M?' ':'\n');
}
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
ll H,W,h,w; cin>>H>>W>>h>>w;
ll max=1000000000/(h*w);
ll hu=(H/h)*(W/w);
ll sei=H*W-hu;
if(max*sei-(max*(h*w-1)+1)*hu<=0){cout<<"No"<<endl;
return 0;}
cout<<"Yes"<<endl;
rep(i, H){
rep(j, W){
if((i+1)%h==0 && (j+1)%w==0) cout<<(-max*(h*w-1)-1);
else cout<<max;
if(j==W-1)cout<<endl;
else cout<<" ";
}
}
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;
public class Main {
private int LARGE = 100000000;
private int H, W, h, w;
private int[][] solve1() {
int[][] matrix = new int[H][W];
for (int[] row : matrix) {
Arrays.fill(row, 1);
}
for (int i = h - 1; i < H; i += h) {
for (int j = 0; j < W; j += w) {
matrix[i][j] = LARGE + 1;
}
}
for (int i = h - 1; i < H; i += h) {
for (int j = w - 1; j < W; j += w) {
matrix[i][j] = -h * w - LARGE;
}
}
return matrix;
}
private int[][] solve2() {
int hw = 10000000;
int[][] matrix = new int[H][W];
for (int i = h - 1; i < H; i += h) {
for (int j = w - 1; j < W; j += w) {
matrix[i][j] = -hw;
}
}
for (int i = 0; i < H; i += h) {
for (int j = 0; j < W; j += w) {
matrix[i][j] = hw - 1;
}
}
return matrix;
}
private void solve(FastScanner in, PrintWriter out) {
H = in.nextInt();
W = in.nextInt();
h = in.nextInt();
w = in.nextInt();
if (H % h == 0 && W % w == 0) {
out.println("No");
return;
}
int[][] matrix = solve1();
if (check(matrix)) {
print(out, matrix);
return;
}
matrix = solve2();
if (check(matrix)) {
print(out, matrix);
return;
}
out.println("No");
}
private boolean check(int[][] matrix) {
long sum = 0;
for (int[] row : matrix) {
for (int x : row) {
sum += x;
}
}
return sum > 0;
}
private void print(PrintWriter out, int[][] matrix) {
out.println("Yes");
for (int[] row : matrix) {
for (int x : row) {
out.print(x + " ");
}
out.println();
}
}
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out);
new Main().solve(new FastScanner(), out);
out.close();
}
private static class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int bufferLength = 0;
private boolean hasNextByte() {
if (ptr < bufferLength) {
return true;
} else {
ptr = 0;
try {
bufferLength = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (bufferLength <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) {
return buffer[ptr++];
} else {
return -1;
}
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
private void skipUnprintable() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
ptr++;
}
}
boolean hasNext() {
skipUnprintable();
return hasNextByte();
}
public String next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
long nextLong() {
if (!hasNext()) {
throw new NoSuchElementException();
}
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++) {
array[i] = nextDouble();
}
return array;
}
double[][] nextDoubleMap(int n, int m) {
double[][] map = new double[n][];
for (int i = 0; i < n; i++) {
map[i] = nextDoubleArray(m);
}
return map;
}
public int nextInt() {
return (int) nextLong();
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++) {
array[i] = nextLong();
}
return array;
}
public String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++) {
array[i] = next();
}
return array;
}
public char[][] nextCharMap(int n) {
char[][] array = new char[n][];
for (int i = 0; i < n; i++) {
array[i] = next().toCharArray();
}
return array;
}
public int[][] nextIntMap(int n, int m) {
int[][] map = new int[n][];
for (int i = 0; i < n; i++) {
map[i] = nextIntArray(m);
}
return map;
}
}
} | JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
#define sz(c) int(c.size())
#define rep(i,a,b) for (int i=a; i<(b); ++i)
#define per(i,a,b) for (int i=(b)-1; i>=(a); --i)
using namespace std;
using ll = long long;
int H,W,h,w;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout<<fixed<<setprecision(10);
cin>>H>>W>>h>>w;
if (H%h==0 && W%w==0) {
cout<<"No\n";
return 0;
}
cout<<"Yes\n";
rep(i,0,H) rep(j,0,W) {
if ((i+1)%h==0 && (j+1)%w==0) cout<<-4000*(h*w-1)-1; else cout<<4000;
cout<<" \n"[j+1==W];
}
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<bits/stdc++.h>
#define N 100005
using namespace std;
int H,W,h,w;
int main(){
scanf("%d%d%d%d",&H,&W,&h,&w);
if(H%h==0&&W%w==0) return puts("No"),0;
int val=-((h*w-1)*1000)-1;
puts("Yes");
for(int i=1;i<=H;i++,puts(""))
for(int j=1;j<=W;j++)
if(i%h==0&&j%w==0) printf("%d ",val);
else printf("1000 ");
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int H = sc.nextInt();
int W = sc.nextInt();
int h = sc.nextInt();
int w = sc.nextInt();
int c = W / w;
int d = H / h;
int resX = W % w;
sc.close();
int resY = H % h;
if (resX == 0 && resY == 0) {
System.out.println("No");
return;
}
// 余りの数
int edge = resX * H + resY * W - resX * resY;
int a = 0;
int anum = h * w - 1;
while (a * edge <= c * d) {
a++;
if (a > 1000000000) {
System.out.println("No");
return;
}
if (a * anum + 1 > 1000000000) {
System.out.println("No");
return;
}
}
int b = a * anum + 1;
b *= -1;
System.out.println("Yes");
for (int j = 1; j < H + 1; j++) {
if (j % h != 0) {
for (int i = 1; i < W + 1; i++) {
System.out.print(a);
if (i != W) {
System.out.print(" ");
}
}
System.out.println("");
continue;
}
for (int i = 1; i < W + 1; i++) {
if (i % w == 0) {
System.out.print(b);
} else {
System.out.print(a);
}
if (i != W) {
System.out.print(" ");
}
}
System.out.println("");
}
}
}
| JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int main(){
long long H,W,h,w,sum=0;
cin >> H >> W >> h >> w;
int ans[H][W];
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
ans[i][j]=1000;
sum+=1000;
}
}
for(int i=h-1;i<H;i+=h){
for(int j=w-1;j<W;j+=w){
ans[i][j]=-(h*w-1)*1000-1;
sum-=1000;
sum+=ans[i][j];
}
}
if(sum<=0){
cout << "No\n";
return 0;
}
cout << "Yes\n";
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
cout << ans[i][j] << ((j==W-1)?'\n':' ');
}
}
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <iostream>
using namespace std;
int main(void) {
int H, W, h, w;
cin >> H >> W >> h >> w;
if ((W%w>0)||(H%h>0)) {
cout << "Yes" << endl;
for (int y = 1; y <= H; y++) {
for (int x = 1; x <= W; x++) {
if ((x%w==0) && (y%h==0)) {
cout << -((h*w-1)*1000+1) << " ";
} else {
cout << 1000 << " ";
}
}
cout << endl;
}
} else {
cout << "No" << endl;
}
return 0;
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 |
H,W,h,w = map(int,input().split())
lis = [[600] * W for i in range(H)]
for i in range(H):
for j in range(W):
if i % h == h-1 and j % w == w-1:
lis[i][j] = -600 * (h * w - 1) - 1
s = 0
for i in range(H):
s += sum(lis[i])
if s <= 0:
print ("No")
else:
print ("Yes")
for i in lis:
print (" ".join(map(str,i)))
| PYTHON3 |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include<cstdio>
int main(){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a%c == 0 && b%d == 0){
printf("No\n");
}else{
int mn = (a/c) * (b/d);
int gae = a * b - (mn * c * d);
int pls = (mn + gae) / gae;
printf("Yes\n");
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
if(i%c == (c-1) && j%d == (d-1)){
printf("%d ",-((c*d-1)*pls+1));
}else{
printf("%d ",pls);
}
}
printf("\n");
}
}
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int H,W,w,h;
const int INF =2333;
int main()
{
scanf("%d%d%d%d",&H,&W,&h,&w);
if (H%h==0&&W%w==0) return printf("No"),0;
printf("Yes\n");
for (int i=1;i<=H;++i)
for (int j=1;j<=W;++j)
printf("%d%c",(i%h==0&&j%w==0?(1-h*w)*INF-1:INF),j==W?'\n':' ');
return 0;
} | CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int main()
{
int H,W,h,w;
scanf("%d%d%d%d",&H,&W,&h,&w);
int X=100000;
if(H%h!=0){
puts("Yes");
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
printf("%d ",i%h==0?X:i%h==h-1?-X-1:0);
}
putchar('\n');
}
}
else if(W%w!=0){
puts("Yes");
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
printf("%d ",j%w==0?X:j%w==w-1?-X-1:0);
}
putchar('\n');
}
}
else{
puts("No");
}
return 0;
}
| CPP |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public void solve() throws IOException {
int H = nextInt(), W = nextInt(), h = nextInt(), w = nextInt();
if(H % h == 0 && W % w == 0){
out.print("No");
return;
}else out.println("Yes");
int min = ((h * w - 1) * 4000 + 1) * (-1);
int res[][] = new int[H][W];
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
res[i][j] = 4000;
}
}
for(int i = 0; i < H / h; i++){
for(int j = 0; j < W / w; j++){
res[(i + 1) * h - 1][(j + 1) * w - 1] = min;
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
out.print(res[i][j] + " ");
}
out.println();
}
}
BufferedReader br;
StringTokenizer sc;
PrintWriter out;
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new Main().run();
}
void run() throws IOException {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// br = new BufferedReader(new FileReader("queue.in"));
// out = new PrintWriter(new File("queue.out"));
solve();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
String nextToken() throws IOException {
while (sc == null || !sc.hasMoreTokens()) {
try {
sc = new StringTokenizer(br.readLine());
} catch (Exception e) {
return null;
}
}
return sc.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
} | JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | /**
* Created by yoshiwaratomohiro on 2017/06/18.
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int H=scan.nextInt();
int W=scan.nextInt();
int h=scan.nextInt();
int w=scan.nextInt();
int area=h*w;
int[][] box = new int[H][W];
long sum=0;
int INF=1000000000;
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
if(i%h==h-1&&j%w==w-1){
box[i][j]=INF*-1;
}else{
if(i%h==0&&j%w==0){
box[i][j]=INF-area+1;
}else{
box[i][j]=1;
}
}
sum+=box[i][j];
}
}
if(sum>0){
System.out.println("Yes");
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
System.out.print(box[i][j]);
if(j<W-1){
System.out.print(" ");
}
}
System.out.println("");
}
}else{
System.out.println("No");
}
}
}
| JAVA |
p03689 AtCoder Grand Contest 016 - +/- Rectangle | You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:
* The matrix has H rows and W columns.
* Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).
* The sum of all the elements of the matrix is positive.
* The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.
Constraints
* 1 ≤ h ≤ H ≤ 500
* 1 ≤ w ≤ W ≤ 500
Input
Input is given from Standard Input in the following format:
H W h w
Output
If there does not exist a matrix that satisfies all of the conditions, print `No`.
Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format:
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}
Here, a_{ij} represents the (i,\ j) element of the matrix.
Examples
Input
3 3 2 2
Output
Yes
1 1 1
1 -4 1
1 1 1
Input
2 4 1 2
Output
No
Input
3 4 2 3
Output
Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7 | 6 | 0 | H,W,h,w=map(int,input().split())
if H%h==0 and W%w==0:
print("No")
quit()
else:
print("Yes")
m=-10**9
p=(-m-1)//(h*w-1)
ans=[[m if i%h==0 and j%w==0 else p for j in range(1,W+1)] for i in range(1,H+1)]
for i in ans:
print(" ".join(map(str,i))) | PYTHON3 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.