A - Squats
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
string a;
cin>>n>>a;
int len=a.size();
int d=0,u=0;
for(int i=0;i<len;i++)if(a[i]=='x')d++;else u++;
//cout<<d<<u;
if(d==u){cout<<0<<endl<<a<<endl;}
else
{
int ans=0;
if(d<u)
{
int cnt=len/2-d,i=0;
ans=cnt;
while(cnt)
{
if(a[i]=='X')a[i]='x',cnt--;
i++;
}
}
else
{
int cnt=len/2-u,i=0;
ans=cnt;
while(cnt)
{
if(a[i]=='x')a[i]='X',cnt--;
i++;
}
}
cout<<ans<<endl<<a<<endl;
}
return 0;
}
B - Megacity
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int n,s,x,y,m;
const int maxn=1010;
struct node
{
int x,y,s;
double dis;
}a[maxn];
bool cmp(node c,node b)
{
if(c.dis==b.dis)return c.s>b.s;
return c.dis<b.dis;
}
double DIS(int x,int y)
{
return sqrt(x*x+y*y);
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&s);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].s);
a[i].dis=DIS(a[i].x,a[i].y);
}
sort(a+1,a+n+1,cmp);
int ans;
for(int i=1;i<=n;i++)
{
if(s>=1000000){ans=i;break;}
s+=a[i].s;
if(s>=1000000){ans=i;break;}
}
if(s<1000000)printf("-1\n");
else printf("%.7f\n",a[ans].dis);
return 0;
}
C - Magic Formulas
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000010;
int XOR[maxn];
int n,p,ans;
void make()
{
XOR[0]=0;
for(int i=1;i<=1000000;i++)
XOR[i]=XOR[i-1]^i;
}
int main()
{
make();
scanf("%d",&n);
ans=0;
for(int i=0;i<n;i++){scanf("%d",&p);ans^=p;}
for(int i=2;i<=n;i++)
{
int tmp=n%i;
int cnt=n/i;
if(cnt%2)ans^=XOR[i-1];
ans^=XOR[tmp];
}
printf("%d",ans);
return 0;
}
D
比赛的时候想到了二分,但没写完。。。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
int n, m, t, a[310][310];
int tp, tu, td;
int r[301][301], d[301][301], l[301][301], u[301][301];
void init()
{
memset(d, 0, sizeof(d));
memset(r, 0, sizeof(r));
memset(l, 0, sizeof(l));
memset(u, 0, sizeof(u));
for (int i=0; i<n; i++)
for (int j=0; j<m-1; j++)
if (a[i][j+1] > a[i][j]) r[i][j+1] = r[i][j] + tu;
else if (a[i][j+1] == a[i][j]) r[i][j+1] = r[i][j] + tp;
else r[i][j+1] = r[i][j] + td;
for (int i=0; i<n; i++)
for (int j=m-1; j>0; j--)
if (a[i][j-1] > a[i][j]) l[i][j-1] = l[i][j] + tu;
else if (a[i][j-1] == a[i][j]) l[i][j-1] = l[i][j] + tp;
else l[i][j-1] = l[i][j] + td;
for (int i=0; i<n-1; i++)
for (int j=0; j<m; j++)
if (a[i+1][j] > a[i][j]) d[i+1][j] = d[i][j] + tu;
else if (a[i+1][j] == a[i][j]) d[i+1][j] = d[i][j] + tp;
else d[i+1][j] = d[i][j] + td;
for (int i=n-1; i>0; i--)
for (int j=0; j<m; j++)
if (a[i-1][j] > a[i][j]) u[i-1][j] = u[i][j] + tu;
else if (a[i-1][j] == a[i][j]) u[i-1][j] = u[i][j] + tp;
else u[i-1][j] = u[i][j] + td;
}
int calc(int x1, int y1, int x2, int y2)
{
int temp = r[x1][y2] - r[x1][y1] + d[x2][y2] - d[x1][y2] +
l[x2][y1] - l[x2][y2] + u[x1][y1] - u[x2][y1];
return temp;
}
void solve()
{
cin >> n >> m >> t;
cin >> tp >> tu >> td;
for (int i=0; i<n; i++)
for (int j=0; j<m; j++) cin >> a[i][j];
init();
int sx = 0, sy = 0, ex = 2, ey = 2;
for (int i=0; i+2<n; i++)
for (int j=0; j+2<m; j++)
for (int p=i+2; p<n; p++) {
int lb = j+1, ub = m;
while (ub - lb > 1) {
int mid = (ub + lb) >> 1;
int temp = calc(i, j, p, mid);
if (temp > t) ub = mid; else lb = mid;
if (sx == -1 || abs(calc(sx,sy,ex,ey)-t) > abs(temp-t)) {
sx = i; sy = j; ex = p; ey = mid;
}
}
}
cout << sx+1 << ' ' << sy+1 << ' ' << ex+1 << ' ' << ey+1 << endl;
}
int main()
{
solve(); return 0;
}
也可以四重循环搞。。。
#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <bitset>
#include <climits>
#include <utility>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cctype>
#define REP(i,n) for( int (i)=0;(i)<(int)(n); ++(i))
#define REPR(i,n) for( int (i) = n; (i)>=0; --(i))
#define REPN(i,x,y) for( int i = x; (i) < (int)(y); (i)++ )
#define REPIT(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define ZERO(x) memset(x,0,sizeof(x))
#define NEG(x) memset(x,-1,sizeof(x))
#define SZ(x) (int)(x).size()
#define RI(n) scanf("%d",&(n))
#define RII(x,y) scanf("%d %d",&(x),&(y))
#define RS(x) scanf("%s",x)
#define OI(x) printf("%d\n",(x))
#define OII(x,y) printf("%d %d\n",(x),(y))
#define MP(x,y) make_pair((x),(y))
#define FT first
#define SD second
#define PB push_back
using namespace std;
typedef long long LL;
const int MOD = 1000000007;
const int maxn = 1111+10;
int n,m;
int tp,tu,td,tq;
int aa[4];
int G[333][333];
int t1[333][333],t2[333][333],t3[333][333],t4[333][333];
int tt1[333];
int main()
{
scanf("%d%d%d",&n,&m,&tq);
scanf("%d%d%d",&tp,&tu,&td);
REP(i,n)REP(j,m)RI(G[i][j]);
for(int i=0;i<n;++i)
for(int j=1;j<m;++j){
if(G[i][j] > G[i][j-1]){
t1[i][j] = tu + t1[i][j-1];
t3[i][j] = td + t3[i][j-1];
}
else if(G[i][j] == G[i][j-1]){
t1[i][j] = tp + t1[i][j-1];
t3[i][j] = tp + t3[i][j-1];
}
else{
t1[i][j] = td + t1[i][j-1];
t3[i][j] = tu + t3[i][j-1];
}
}
for(int i=1;i<n;++i)
for(int j=0;j<m;++j){
if(G[i][j] > G[i-1][j]){
t2[i][j] = tu + t2[i-1][j];
t4[i][j] = td + t4[i-1][j];
}
else if(G[i][j] == G[i-1][j]){
t2[i][j] = tp + t2[i-1][j];
t4[i][j] = tp + t4[i-1][j];
}
else{
t2[i][j] = td + t2[i-1][j];
t4[i][j] = tu + t4[i-1][j];
}
}
int ans = INT_MAX;
for(int y1=0;y1<n;++y1)
for(int y2 = y1+2;y2<n;++y2){
for(int x1 = 0;x1<m;++x1)
for(int x2 = x1+2;x2<m;++x2){
int ttt = (t1[y1][x2] - t1[y1][x1])+(t2[y2][x2]-t2[y1][x2])+(t3[y2][x2]-t3[y2][x1])+(t4[y2][x1]-t4[y1][x1]);
if(abs(ttt-tq)<ans){
aa[0] = y1;aa[1] = x1;aa[2] = y2;aa[3] = x2;
ans = abs(ttt-tq);
}
}
}
printf("%d %d %d %d\n",aa[0]+1,aa[1]+1,aa[2]+1,aa[3]+1,aa[4]+1);
return 0;
}