1.:模拟:
#include<bits/stdc++.h>
using namespace std;
int N,X;
typedef long long LL;
struct node{
LL chu;
LL pour;
}a[11000];
int main(){
scanf("%d%d",&N,&X);
for(int i=1;i<=N;i++){
scanf("%d%d",&a[i].chu,&a[i].pour);
}
LL last=0;
for(int i=1;i<=N;i++){
if(a[i].chu+a[i].pour+last>X){
last=(a[i].chu+a[i].pour+last-X);
}
else
last=0;
}
printf("%d\n",last);
}
/*
10 10
0 9962
1 2642
7 8773
9 523
6 3882
7 1093
0 3933
7 2276
6 4792
6 8904
*/
2.环状结构
题目太坑。
报完数字继续报而不是归1:
#include<bits/stdc++.h>
using namespace std;
struct node{
int id;
int next;
int pre;
}a[2100];
int N,M;
int cnt;
int main()
{
scanf("%d%d",&N,&M);
cnt=N;
for(int i=1;i<=N;i++){
a[i].id=i;
a[i].next=i+1;
if(i==N)
a[i].next=1;
a[i].pre=i-1;
if(i==1)
a[i].pre=N;
}
int now=1;int shu=1;
while(cnt>1){
if(shu%10==M || now%M==0 ){
a[a[now].pre].next=a[now].next;
a[a[now].next].pre=a[now].pre;
/// shu=1;
cnt--;
}
else
shu++;
now=a[now].next;
}
printf("%d\n",now);
}
3:01背包
蓝宝石一个,红宝石N个,要求从N个中找M个,使得M个的价值=2*(N-M)个的价值。
输出id最小的蓝宝石的价值。
但不知道为什么只过了90%样例:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int a[2000];
int N,M;
int flag;
LL dp[2000000];
int main(){
scanf("%d%d",&N,&M);
flag=0;
N=N+1;
for(int i=1;i<=N;i++){
scanf("%d",&a[i]);
}
int lan; int maxx=-1; LL sum=0;
for(int i=1;i<=N;i++){
if(flag==1)
break;
lan=a[i]; maxx=-1; sum=0;
for(int j=1;j<=N;j++){
if(j!=i){
sum+=a[j];
}
}
if(sum%3==0){
sum=sum/3;
for(int j=1;j<=N;j++){
if(j!=i){
for(int k=sum;k>=a[j];k--){
dp[k]=max(dp[k],dp[k-a[j]]+a[j]);
}
}
}
if(dp[sum]==sum)
{
printf("%d\n",lan);
flag=1;
break;
}
}
}
if(flag==0){
printf("NO\n");
}
}
/*
5 2
316 861 5 38 483 235
*/