又要掉分了。。。
Sereja showed an interesting game to his friends. The game goes like that. Initially, there is a table with an empty cup and n water mugs on it. Then all players take turns to move. During a move, a player takes a non-empty mug of water and pours all water from it into the cup. If the cup overfills, then we assume that this player lost.
As soon as Sereja's friends heard of the game, they wanted to play it. Sereja, on the other hand, wanted to find out whether his friends can play the game in such a way that there are no losers. You are given the volumes of all mugs and the cup. Also, you know that Sereja has (n - 1) friends. Determine if Sereja's friends can play the game so that nobody loses.
The first line contains integers n and s (2 ≤ n ≤ 100; 1 ≤ s ≤ 1000) — the number of mugs and the volume of the cup. The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 10). Number ai means the volume of the i-th mug.
In a single line, print "YES" (without the quotes) if his friends can play in the described manner, and "NO" (without the quotes) otherwise.
3 4 1 1 1
YES
3 4 3 1 3
YES
3 4 4 4 4
NO
思路:去掉最大的然后看he是不是小于等于容积
#include<iostream>
using namespace std;
int main()
{
int n,s,a,sum=0;
int max1=-1;
cin>>n>>s;
for(int i=0;i<n;i++)
{
cin>>a;
sum+=a;
if(a>max1)max1=a;
}
if(sum-max1<=s)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
Let's assume that we are given a matrix b of size x × y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a 2x × y matrix c which has the following properties:
- the upper half of matrix c (rows with numbers from 1 to x) exactly matches b;
- the lower half of matrix c (rows with numbers from x + 1 to 2x) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows x and x + 1).
Sereja has an n × m matrix a. He wants to find such matrix b, that it can be transformed into matrix a, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain?
The first line contains two integers, n and m (1 ≤ n, m ≤ 100). Each of the next n lines contains m integers — the elements of matrix a. The i-th line contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 1) — the i-th row of the matrix a.
In the single line, print the answer to the problem — the minimum number of rows of matrix b.
4 3 0 0 1 1 1 0 1 1 0 0 0 1
2
3 3 0 0 0 0 0 0 0 0 0
3
8 1 0 1 1 0 0 1 1 0
2如果行数是奇数则使整个矩阵,然后每次折半,看能不能匹配
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=110;
int n,m;
int a[maxn][maxn];
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)cin>>a[i][j];
if(n%2){cout<<n<<endl;return 0;}
int ans=0;
bool f=false,flag;
while(true)
{
int p=n/2;
int tmp=p;
flag=true;
for(int i=1;i<=tmp;i++)
{
for(int j=1;j<=m;j++)
if(a[i][j]!=a[2*p-i+1][j]){flag=false;break;}
}
if(p%2&&!flag){ans=p*2;break;}
if(p%2){ans=p;break;}
if(flag)n=p;
else {ans=2*p;break;}
}
cout<<ans<<endl;
return 0;
}
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation:
A swap operation is the following sequence of actions:
- choose two indexes i, j (i ≠ j);
- perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp.
What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations?
The first line contains two integers n and k (1 ≤ n ≤ 200; 1 ≤ k ≤ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 ≤ a[i] ≤ 1000).
In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations.
10 2 10 -1 2 2 2 2 2 2 -1 10
32
5 10 -1 -1 -1 -1 -1
-1
题意 : 给了两个公式能够求出m(a),然后允许最多交换k次,求其中的最大的m(a)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=210;
const int INF=1000000000;
int n,cnt;
int a[maxn];
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&cnt);
int sum,ans=-INF;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
vector<int> x,y;
sum=0;
for(int k=i;k<=j;k++){x.push_back(a[k]);sum+=a[k];}
ans=max(ans,sum);
for(int k=1;k<=n;k++)
{
if(k<i||k>j)
y.push_back(a[k]);
}
sort(x.begin(),x.end());
sort(y.begin(),y.end(),greater<int>());
for(int k=1;k<=cnt&&k<=x.size()&&k<=y.size();k++)
{
if(x[k-1]<y[k-1])
{
sum-=x[k-1];
sum+=y[k-1];
ans=max(ans,sum);
}
}
}
}
printf("%d\n",ans);
return 0;
}